is this valid construction?

Mutikasa

Power Member
Joined
May 23, 2011
Messages
589
Reaction score
216
PHP:
foreach($many as $one) 
name[] = (($number[] = thisFun()) => thatFun());

so i can create two arrays at the same time
 
name should have a $ at the beginning. You should also declare $name before using $name[] as this can lead to issues with certain settings.
 
I dont think that it's valid.
But why dont you try it?
 
I did and It's not.
But I was thinking more conceptual, is such thing possible?
In the same time assign array and key in array using functions.
 
Could you try to be a bit more specific? The code you posted in the first thread doesnt tell me anything. What are you trying to do there?
 
yes
thisFun and thatFun are functions which return value.
$name[] and $number[] are arrays.
I want to use foreach loop
and in every loop want to assign value to arrays in that way that $number[] is the array of keys for the $name[] array and at the same time they get their values from thisFun for $number[] and thatFun for $name[].

The long code will be
PHP:
$i=0;
foreach($many as $one) {
$number[$i] = thisFun();
$name[$number[$i]] = thatFun();
$i++; }
 
Gotcha! you could do this using the the php implode() function, or the alias join().

Check it out on php.net.
 
how? implide() returns string, i need array
 
Ah, sorry..

Copied from php.net

Code:
Example #1 array_merge() PHP 5 example
<?php
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>
 
Back
Top