0

I wanted to echo two array at a time. Please check bellow there i am already having echo of $another_arr1 array and now i want to echo another $another_arr2 array. How can i do it?

$arr1 = array('a'=>'1');
$arr2 = array('a'=>'2');
$arr3 = array('a'=>'3');
$another_arr1 = array($arr1,$arr2,$arr3);
$arr4 = array('b'=>'4');
$arr5 = array('b'=>'5');
$arr6 = array('b'=>'6');
$another_arr2 = array($arr4,$arr5,$arr6);

foreach ($another_arr1 as $_another_arr1){
    foreach ($_another_arr1 as $another1){
        echo $another1.' ->'.' need to put $another_arr2 value here <br/>';
    }

}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Smith Kr
  • 25
  • 2
  • `foreach ($another_arr1 as $key=> $_another_arr1){ foreach ($_another_arr1 as $another1){ $another_arr2_value = (!empty($another_arr2[$key]['b']))? $another_arr2[$key]['b']:''; echo $another1.' ->'.' $another_arr2_value.'
    '; } }`
    – Alive to die - Anant Feb 01 '17 at 04:33
  • one correction in my comment . remove `'` from `.' $another_arr2_value.` – Alive to die - Anant Feb 01 '17 at 04:45

4 Answers4

0

You can use the index of first array to get to the second array the use index b to check the data.Try this:

$arr1 = array('a'=>'1');
$arr2 = array('a'=>'2');
$arr3 = array('a'=>'3');
$another_arr1 = array($arr1,$arr2,$arr3);
$arr4 = array('b'=>'4');
$arr5 = array('b'=>'5');
$arr6 = array('b'=>'6');
$another_arr2 = array($arr4,$arr5,$arr6);

foreach ($another_arr1 as $key=>$_another_arr1){
    foreach ($_another_arr1 as $another1){
    $another_arr2_value = (!empty($another_arr2[$key]['b']))? $another_arr2[$key]['b']:'';//check if value in second array exists 
    echo $another1.' ->'. $another_arr2_value."<br>";
    }

}

Demo

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

use the below code:

<?php
$arr1 = array('a'=>'1');
$arr2 = array('a'=>'2');
$arr3 = array('a'=>'3');
$another_arr1 = array($arr1,$arr2,$arr3);
$arr4 = array('b'=>'4');
$arr5 = array('b'=>'5');
$arr6 = array('b'=>'6');
$another_arr2 = array($arr4,$arr5,$arr6);

foreach ($another_arr1 as $key=>$value){
    foreach ($value as $key1=>$value1){
        echo $value1.' -> '.$another_arr2[$key]['b'].'<br/>';
    }
}

?>
Bhavik
  • 495
  • 2
  • 10
0

You can do it ion this way.

$arr1 = array('a' => '1');
$arr2 = array('a' => '2');
$arr3 = array('a' => '3');
$another_arr1 = array($arr1, $arr2, $arr3);
$arr4 = array('b' => '4');
$arr5 = array('b' => '5');
$arr6 = array('b' => '6');
$another_arr2 = array($arr4, $arr5, $arr6);

foreach ($another_arr1 as $kk => $_another_arr1) {
  foreach ($_another_arr1 as $another1) {
    echo $another1 . $another_arr2[$kk]['b'] . '<br/>';
  }
}
0

You can do it like below:-

foreach ($another_arr1 as $key=> $_another_arr1){ 
  foreach ($_another_arr1 as $another1){ 
      $another_arr2_value = (!empty($another_arr2[$key]['b']))? $another_arr2[$key]['b']:''; 
      echo $another1.' ->'. $another_arr2_value.'<br/>'; 
  } 
}

Output:- https://eval.in/728096

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98