从 2 个数组中回显 foreach

echo foreach from 2 arrays

我有 2 个数组,其中包含来自 mysql 查询的以下值

$Array1 =
(
[0] => john
[1] => nick
[2] => mike
[3] => eva
[4] => mary
) 

$Array2 =
(
[0] => Array ( [name] => john [color] =>black )
[1] => Array ( [name] => john [color] =>red )
[2] => Array ( [name] => mike [color] =>yellow )
[3] => Array ( [name] => eva [color] =>purple )
[4] => Array ( [name] => eva [color] =>green )
[5] => Array ( [name] => mary [color] =>orange )
[6] => Array ( [name] => mary [color] =>white )
[7] => Array ( [name] => george [color] =>purple )
[8] => Array ( [name] => phil [color] =>green )
[9] => Array ( [name] => toni [color] =>orange )
[10] => Array ( [name] => amanda [color] =>white )

) 

我想 echo $array1 中的每个人 $array2 中的所有颜色。

您可以尝试这样的操作:

foreach($Array1 as $value)
    foreach($Array2 as $v)
       echo $v['name'] === $value ? $v['color'] : '';

Example

使用下面的代码

<?php
$Array1 = array
(
0 => "john",
1 => "nick",
2 => "mike",
3 => "eva",
4 => "mary",
);

$Array2 =array
(
0 => Array ( "name" => "john" ,"color" =>"black" ),
1 => Array ( "name" => "john" ,"color" =>"red" ),
2 => Array ( "name" => "mike" ,"color" =>"yellow" ),
3 => Array ( "name" => "eva" ,"color" =>"purple" ),
4 => Array ( "name" => "eva" ,"color" =>"green" ),
5 => Array ( "name" => "mary" ,"color" =>"orange" ),
6 => Array ( "name" => "mary" ,"color" =>"white" ),
7 => Array ( "name" => "george" ,"color" =>"purple" ),
8 => Array ( "name" => "phil" ,"color" =>"green" ),
9 => Array ( "name"=> "toni" ,"color" =>"orange" ),
10 => Array ( "name" => "amanda" ,"color" =>"white"),

);
            foreach($Array1 as $t){
                foreach($Array2 as $p){
                   if($p['name'] == $t) {
            echo $p['color'];
            }
            }
}

希望对您有所帮助

$Array1 = array("john", "nick", "mike", "eva", "mary");

$Array2 = array(
        array ("name" => "john", "color" => "black" ),
        array ( "name" => "john", "color" => "red" ),
        array ( "name" => "mike", "color" => "yellow" ),
        array ( "name" => "eva", "color" => "purple" ),
        array ( "name" => "eva", "color" => "green" ),
        array ( "name" => "mary", "color" => "orange" ),
        array ( "name" => "mary", "color" => "white" ),
        array ( "name" => "george", "color" => "purple" ),
        array ( "name" => "phil", "color" => "green" ),
        array ( "name"=> "toni", "color" => "orange" ),
        array ( "name" => "amanda", "color" => "white")
);

foreach($Array1 as $val1)
    foreach($Array2 as $val2)
       echo $val2['name'] === $val1 ? $val2['color'] . "<br>" : '';