13 Replies - 3480 Views - Last Post: 13 November 2010 - 01:11 PM Rate Topic: -----

#1 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

concatenating vals in an array

Posted 11 November 2010 - 05:46 AM

Currently working on a problem where I want to do the following:

Let's say I have x amount of values, in this case let's keep it at four: 'a', 'b', 'c','d'. I insert them into an array as 'a', 'b','c', 'd'. For the next slot in the array, I take the concatenation of the first two values of a and b and they become 'ab' and I slot them into the array after 'd'. For the next slot after 'ab', I concatenate the hash values of c and d, so thats 'cd'. This is inserted after 'ab' in my array. The next slot of the array would then hold concatenation of 'ab' and 'cd' and this continues up the array for all values until I am left with one value/concatenation at the end.

So far I have only coded

<?php

$val1 = "a";
$val2 = "b";
$val3 = "c";
$val4 = "d";
$myarray = array();

array_push($myarray, $val1, $val2, $val3, $val4);
//print_r($myarray);
?>



I am trying to figure out a way to walk through the array in a loop and do the concatenations and I'm checking out http://www.php.net/m...n/ref.array.php but I am currently at a loss. I am not asking for the solutions, just general advice/pointers.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: concatenating vals in an array

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: concatenating vals in an array

Posted 11 November 2010 - 05:55 AM

$myarray[] = $val1;
$myarray[] = $val2;

Like that?
Was This Post Helpful? 0
  • +
  • -

#3 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: concatenating vals in an array

Posted 11 November 2010 - 06:10 AM

No, I want to initially insert those values into the array, then go through each value and concatenate in blocks of two: $myarray[0].$myarray[1], then $myarray[2].$myarray[3] and insert these concatenations in as new values in the same array.

The concatenation of $myarray[0].$myarray[1] would then be the value at $myarray[4];
The concatenation of $myarray[2].$myarray[3] would then be the value at $myarray[5];

Something like, but not quite:

foreach ($myarray as &$value) {
    $newVal = $value[0].$value[1]; //this would have to be dynamic 
                                   //but setting it to first and second 
                                   //indexes for now
	$myarray[] = $newVal;
}



I get this error though

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes)


Was This Post Helpful? 0
  • +
  • -

#4 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: concatenating vals in an array

Posted 11 November 2010 - 06:14 AM

Hi,

I threw this together and tested it, it seems to work, gotta run to work!

$val1 = "a";
$val2 = "b";
$val3 = "c";
$val4 = "d";
$myarray = array();

array_push($myarray, $val1, $val2, $val3, $val4);
//total number of characters
$total_chars = count($myarray);
//starting points
$first = 0;
$next = 1;
//while the last element is not equal to the length of the total number of characters
while(strlen($myarray[count($myarray)-1]) < $total_chars )
{
	//add the concatonated chars
	$myarray[] = $myarray[$first].$myarray[$next];
	//move up to the next two elements
	$first += 2;
	$next += 2;
}
print_r($myarray);



edit, note this will only work if this is an array of characters

This post has been edited by Jstall: 11 November 2010 - 06:15 AM

Was This Post Helpful? 1
  • +
  • -

#5 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: concatenating vals in an array

Posted 11 November 2010 - 06:19 AM

Thanks Jstall, I'll check it out now! Have a good day!
Was This Post Helpful? 0
  • +
  • -

#6 grimpirate   User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: concatenating vals in an array

Posted 11 November 2010 - 12:48 PM

This only works if you have an even number of entries. So you would need to check that first assuming some sort of user input. Then you would traverse the array by half each time using a while loop (done using some sort of incrementor that updates by a value of 2). Using the loop control variable you could continue looping until the array reaches a size of 1.
Was This Post Helpful? 2
  • +
  • -

#7 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: concatenating vals in an array

Posted 11 November 2010 - 02:51 PM

Thanks for your input but I am not quite sure how to do that in PHP :( Hopefully will figure it out.
Was This Post Helpful? 0
  • +
  • -

#8 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: concatenating vals in an array

Posted 12 November 2010 - 06:40 AM

Hiya,

It's true about that only working if it is an even number, although I'm not sure what was meant by looping until the array reached a size of one.


It would depend on how you wanted to handle the "hanging" element in the array. Would you want it to be added to the element before it? something like:
$val1 = "a";
$val2 = "b";
$val3 = "c";
$val4 = "d";
$val5 = "e";


And have it look like this at the end :
[0] => a [1] => b [2] => c [3] => d [4] => [e] [5] => ab [6] => cde [7] => abcde



Or you could do the same as if it were even and tack the odd value on at the end. How do you want to handle odd numbers?
Was This Post Helpful? 1
  • +
  • -

#9 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: concatenating vals in an array

Posted 12 November 2010 - 09:08 AM

From drawing out the structure above in binary-tree form, it seems to look better if I tack the odd value on at the end. So I would have like

[0] => a [1] => b [2] => c [3] => d [4] => [e] [5] => ab [6] => cd [7] => abcde



Would that work?
Was This Post Helpful? 0
  • +
  • -

#10 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: concatenating vals in an array

Posted 12 November 2010 - 09:15 AM

If I have odd values I am trying to do the following but it's not working out..
basically trying to traverse the array dividing by 2 each time

//starting points
$first = 0;
$second = 1;

//while the last element is not equal to the length of the total number of characters
while(strlen($myarray[count($myarray)]/2) < $total_chars )
{
	//add concatonated chars
	$myarray[] = $myarray[$first].$myarray[$second];
	
	//move to the next two elements
	$first = $first * 2;
	$second = $second * 2 + 1;
}





but I get

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes)



:/

This post has been edited by megglz: 12 November 2010 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#11 grimpirate   User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: concatenating vals in an array

Posted 12 November 2010 - 11:44 AM

I would like to ask the question of why you're doing it this way? Is this an exercise you're attempting to perform for your own education or do you simply need to concatenate all the letters. Because if so, the implode function will do what you are asking.
Was This Post Helpful? 0
  • +
  • -

#12 grimpirate   User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: concatenating vals in an array

Posted 12 November 2010 - 12:31 PM

Meh, I was bored. Here you are:
<?php

$foo = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');

$start = 0;
$end = count($foo);

$i = 0;
while($start != $end - 1){
	if($i % 2 == 1)
		$foo[count($foo) - 1] .= $foo[$start + $i];
	else
		$foo[] .= $foo[$start + $i];
	$i++;
	if(($start + $i) == $end){
		$i = 0;
		$start = $end;
		$end = count($foo);
	}
}

print_r($foo);

?>
This will work for both even and odd cases and will show you all the interim steps appended to the end of the array until the final result.
Was This Post Helpful? 2
  • +
  • -

#13 megglz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: concatenating vals in an array

Posted 13 November 2010 - 06:08 AM

Thanks a million! I wanted to do this for my own education!
Was This Post Helpful? 0
  • +
  • -

#14 grimpirate   User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: concatenating vals in an array

Posted 13 November 2010 - 01:11 PM

No problem, if you need help understanding how the code does what it does just let me know.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1