amin
amin

Reputation: 31

how can i use more than one array in foreach()?

i want to send more than on array in foreach() . i know this way is false .whats the true method ?

$Fname = [1,2,3,4,5];
$Lname = [1,2,3,4,5];
$Addrs = [1,2,3,4,5];
$Mobile = [1,2,3,4,5];

$fields = array(
    'name'  => 'a',
    'type'  => 'b',
    'value' => 'n',
    'show'  => 'd',
);



foreach($fields as $key => $n) 
{
    echo " {$Fname[$key]} , {$Lname[$key]},{$Addrs[$key]} , {$Mobile[$key]},{$key} ,{$n} <br>";
}

Upvotes: 0

Views: 122

Answers (4)

chandni patel
chandni patel

Reputation: 1

Try this code

$arr1 = array("a" => 1, "b" => 2, "c" => 3); $arr2 = array("x" => 4, "y" => 5, "z" => 6);

foreach ($arr1 as $key => &$val) {} foreach ($arr2 as $key => $val) {}

var_dump($arr1); var_dump($arr2);

Upvotes: 0

Jeremy Jackson
Jeremy Jackson

Reputation: 2257

Instead of storing your data like that, it'd be easier to store it in an array of associative arrays:

$people = array(
    array(
        'firstName' => 'Bruce',
        'lastName' => 'Wayne',
        'address' => '123 East St. Gotham City, XX USA'
        'mobile' => '847-847-8475'
    ),
    array(
        'firstName' => 'Roland',
        'lastName' => 'Deschain',
        'address' => 'N/A'
        'mobile' => '191-919-1919'
    )
);

foreach($people as $person){
    echo $person['firstName'] + ', ' + $person['lastName'];
    echo $person['address'] + ', ' + $person['mobile'];
}

It's just a cleaner way to store/access your data, makes it easy to use one foreach as well.

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

i know this way is false...? What's false about it?

foreach() will iterate over an array, not over multiple arrays.... if you absolutely need to iterate over multiple arrays within the same foreach() loop, you can use SPL's MultipleIterator, but it adds a lot more complexity to your code, and the approach that you've taken is as good as any

Just make sure that your keys match up in all the arrays; if they don't then you will have problems

foreach(array_values($fields) as $key => $n) 
{
    $k = array_keys($fields)[$key];
    echo " {$Fname[$key]} , {$Lname[$key]},{$Addrs[$key]} , {$Mobile[$key]},{$k} ,{$n} <br>";
}

Upvotes: 1

fusion3k
fusion3k

Reputation: 11689

If all your arrays have the same number of rows, you can use a for loop instead of a foreach, in conjunction with next() and current() for associative array:

for( $i = 0; $i < count($Fname); $i++ )
{
    echo $Fname[$i]  . PHP_EOL;
    echo $Lname[$i]  . PHP_EOL;
    echo $Addrs[$i]  . PHP_EOL;
    echo $Mobile[$i] . PHP_EOL;
    echo current($fields) . PHP_EOL;
    next($fields);
}

The problem is that your arrays haven't same rows number... So you have to add some condition like this:

for( $i = 0; $i < count($Fname); $i++ )
{
    echo $Fname[$i]  . PHP_EOL;
    echo $Lname[$i]  . PHP_EOL;
    echo $Addrs[$i]  . PHP_EOL;
    echo $Mobile[$i] . PHP_EOL;
    if( isset(current($fields)) )
    {
        echo current($fields) . PHP_EOL;
        next($fields);
    }
}

Upvotes: 1

Related Questions