一次回声两个数组

时间:2017-02-01 04:27:19

标签: php

我希望一次echo两个数组。请检查下面我已经有$another_arr1数组的回声,现在我想回显另一个$another_arr2数组。我该怎么办?

$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/>';
    }

}

4 个答案:

答案 0 :(得分:0)

您可以使用第一个数组的索引到达第二个数组,使用索引b来检查数据。试试这个:

$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

答案 1 :(得分:0)

使用以下代码:

<?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/>';
    }
}

?>

答案 2 :(得分:0)

你可以这样做。

$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/>';
  }
}

答案 3 :(得分:0)

你可以这样做: -

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/>'; 
  } 
}

输出: - https://eval.in/728096