Erlaunis
Erlaunis

Reputation: 1451

Loop through two arrays and display one data of each

I want to create a string composed by two data from two different arrays. I loop through both arrays with two foreach to get my data. But the problem is that I want so loop at the same time both of the foreach.

So for example (just for example, these arrays aren't the real), I have a first array :

array1 =('apple','orange','banana','lemon');

And a second array :

array2 =('juice','fruit','split','tree');

And I want to loop through these arrays and create a string like that :

"apple juice", "orange fruit", "banana split", "lemon tree"

With my function, I get :

"apple juice", "orange juice", "banana juice", "lemon juice"

I heard about recursive function but I don't know how to do it or if it's the best way.

So this is my code :

foreach ($xml as $table)  
                {
                    foreach ($table as $champs)  /
                    {
                        foreach($array as $ligne)
                        {
                            foreach($ligne as $elt)
                            {
                                if ($table['nom']=='traitement') 
                                {
                                    $stringUpdate .= '\''.$champs->nom.'=\''.$elt.'\','; 
                                    break 2;
                                }
                            }
                        }update_traitement($stringUpdate);
                        $stringUpdate='';
                    }
                }

Can someone help me please ? I really don't know how to do it.

EDIT :

Concretly, I have one array with data that I get from a xml file and an other array from an handsontable. So the second array is two-dimensional, that's why I need to use 4 foreach.

I need to loop through the rows and through the columns of the second array, and I think a simple foreach will not match.

Upvotes: -1

Views: 970

Answers (5)

Emil
Emil

Reputation: 1816

So the arrays could be two-dimensional? Is this code working for you?

function merge($array1, $array2) {


    foreach($array1 as $item) {

        if(is_array($item)) {

            foreach($item as $item2) {

                $newArray1[] = $item2;

            }

        } else {

            $newArray1[] = $item;

        }

    }

    foreach($array2 as $item) {

        if(is_array($item)) {

            foreach($item as $item2) {

                $newArray2[] = $item2;

            }

        } else {

            $newArray2[] = $item;

        }

    }

    if(count($newArray1) < count($newArray2)) {

        $stop = count($newArray1);

    } else {

        $stop = count($newArray2);

    }

    for($i=0;$i < $stop;$i++) {

        $result[] = $newArray1[$i] . " " . $newArray2[$i];

    }

    return $result;

}

$array1 = array('apple', 'orange', 'banana', 'lemon');
$array2 = array('juice', 'fruit', 'split', 'tree');


echo "<pre>";
print_r(merge($array1, $array2));

$array3 = array('apple', 'orange', 'banana');
$array4 = array('juice', 'fruit', 'split', 'tree');

print_r(merge($array3, $array4));

$array5 = array('foo' => array('apple', 'banana'), 'bar' => 'orange', 'test' => 'peach');
$array6 = array('juice', array('fruit', 'test'), 'hello');

print_r(merge($array5, $array6));
echo "</pre>";

Output:

enter image description here

Upvotes: 0

Emil
Emil

Reputation: 1816

Use a for loop and let $i increase by 1 each lap in the loop. Use $i to access the values in the respective array.

$array1 = array('apple', 'orange', 'banana', 'lemon');
$array2 = array('juice', 'fruit', 'split', 'tree');

for($i=0;$i < count($array1);$i++) {

$array3[] = $array1[$i] . " " . $array2[$i];

}

print_r($array3);

Output: Array ( [0] => apple juice [1] => orange fruit [2] => banana split [3] => lemon tree )

This will work if...

  • ...both arrays have the same keys.
  • ...both arrays have the same size.

UPDATE

Okay, so this will work even if the arrays dont hav ethe same keys and size.

function merge($array1, $array2) {


    foreach($array1 as $item) {

        $newArray1[] = $item;

    }

    foreach($array2 as $item) {

        $newArray2[] = $item;

    }

    if(count($newArray1) < count($newArray2)) {

        $stop = count($newArray1);

    } else {

        $stop = count($newArray2);

    }


    for($i=0;$i < $stop;$i++) {

        $result[] = $newArray1[$i] . " " . $newArray2[$i];

    }

    return $result;

}

$array1 = array('apple', 'orange', 'banana', 'lemon');
$array2 = array('juice', 'fruit', 'split', 'tree');


print_r(merge($array1, $array2));

$array3 = array('apple', 'orange', 'banana');
$array4 = array('juice', 'fruit', 'split', 'tree');

print_r(merge($array3, $array4));

$array5 = array('foo' => 'apple', 'bar' => 'orange', 'test' => 'banana');
$array6 = array('juice', 'fruit');

print_r(merge($array5, $array6));

Output:

Array ( [0] => apple juice [1] => orange fruit [2] => banana split [3] => lemon tree )

Array ( [0] => apple juice [1] => orange fruit [2] => banana split )

Array ( [0] => apple juice [1] => orange fruit )

Upvotes: 0

Pupil
Pupil

Reputation: 23958

    $array1 = array('apple','orange','banana','lemon');

    $array2 = array('juice','fruit','split','tree');

    $output = array();
    if (! empty($array1)) {
        $i=0;
        foreach ($array1 as $elem) {
            $output[] = $elem . ' ' . $array2[$i];
            ++$i;
        }
    }

    echo '<pre>';
    print_r($output);
    echo '</pre>';

Explanation:

You are looping through two arrays, but, the keys are same.

So, you can do it in a single loop.

Have a counter variable for loop and user this as key for second array.

Upvotes: 0

donald123
donald123

Reputation: 5749

when both arrays have the same keys and size you can handle it like this

 <?php 
     foreach($array1 as $key=$val) {
       echo $val.' '.$array2[$key];
     }

Upvotes: 0

Dhinju Divakaran
Dhinju Divakaran

Reputation: 903

You need only one foreach

$array1 =('apple','orange','banana','lemon');
$array2 =('juice','fruit','split','tree');
foreach($array1 as $i=>$value){
 echo $value." ".$array2[$i];
}

Upvotes: 1

Related Questions