如何根据需要显示数组的值?

时间:2017-07-10 08:06:23

标签: php arrays

我的代码是这样的:

<?php 
    $data1 = array(1, 2, 3, 4, 5);
    $data2 = array(
                array('id' => 1, 'name' => 'chelsea.jpg'),
                array('id' => 2, 'name' => 'mu.jpg'),
            );
    foreach ($data1 as $key1 => $value1) {
        foreach ($data2 as $key2 => $value2) {
            if($value1 == $value2['id'])
                echo $value2['name'].'<br>';
            else
                echo 'No data'.'<br>';
        }
    }
?>

如果代码运行,结果如下:

  

chelsea.jpg

     

无数据

     

无数据

     

mu.jpg

     

无数据

     

无数据

     

无数据

     

无数据

     

无数据

     

无数据

这是假的

我想要这样的结果:

  

chelsea.jpg

     

mu.jpg

     

无数据

     

无数据

     

无数据

我该怎么做?

5 个答案:

答案 0 :(得分:4)

找到id时使用continue

$data1 = array(1, 2, 3, 4, 5);
$data2 = array(
            array('id' => 1, 'name' => 'chelsea.jpg'),
            array('id' => 2, 'name' => 'mu.jpg'),
        );
foreach ($data1 as $key1 => $value1) {
    foreach ($data2 as $key2 => $value2) {
        if($value1 == $value2['id']) {
            echo $value2['name'],"\n";
            continue 2;
        }
    }
    echo "No data\n";
}

<强>输出:

chelsea.jpg
mu.jpg
No data
No data
No data

答案 1 :(得分:2)

您可以转换第二个数组,使其由id列键入。这允许你做一个isset而不是任何其他类型的查找......

<?php
$data1 = array(1, 2, 3, 4, 5);
$data2 = array(
        array('id' => 1, 'name' => 'chelsea.jpg'),
        array('id' => 2, 'name' => 'mu.jpg'),
);
$data2 = array_column($data2, null, 'id' );
foreach ($data1 as $key1 => $value1) {
    if(isset($data2[$value1]))
        echo $data2[$value1]['name'].'<br>';
            else
        echo 'No data'.'<br>';
}
?>

更新:在考虑如何最小化代码之后,使用PHP7的null coalesce?操作者...

 <?php
    $data1 = array(1, 2, 3, 4, 5);
    $data2 = array(
            array('id' => 1, 'name' => 'chelsea.jpg'),
            array('id' => 2, 'name' => 'mu.jpg'),
    );
    $data2 = array_column($data2, null, 'id' );
    foreach ($data1 as $value1) {
        echo $data2[$value1]['name']??'No data'.'.<br>';
    }
    ?>

答案 2 :(得分:1)

您可以使用 in_array php函数执行此操作

使用: -

$data1 = array(1, 2, 3, 4, 5);
$data2 = array(
    array('id' => 1, 'name' => 'chelsea.jpg'),
    array('id' => 2, 'name' => 'mu.jpg'),
);
foreach ($data2 as $key2 => $value2) {
    if(in_array($value2['id'],$data1))
        echo $value2['name'].'<br>';
    else
        echo 'No data'.'<br>';
}

它将输出为

enter image description here

<强>修改: -

$data1 = array(1, 2, 3, 4, 5);
$data2 = array(
    array('id' => 1, 'name' => 'chelsea.jpg'),
    array('id' => 2, 'name' => 'mu.jpg'),
);
foreach ($data1 as $key1 => $value1) {
    $name='No data';
    foreach ($data2 as $key2 => $value2) {
        if($value1 == $value2['id'])
            $name= $value2['name'];
    }
    echo $name.'<br>';
}

输出为

enter image description here

答案 3 :(得分:0)

可能会有所帮助。

<?php 
    $data1 = array(1, 2, 3, 4, 5);
    $data2 = array(
                array('id' => 1, 'name' => 'chelsea.jpg'),
                array('id' => 2, 'name' => 'mu.jpg'),
            );
    foreach ($data1 as $key1 => $value1) {
        foreach ($data2 as $key2 => $value2) {
            if($value1 == $value2['id'])
                echo $value2['name'] . "<br/>";
            else
                $new_array[] = 'No data';
        }
    }

echo implode("\n", $new_array);

?>

答案 4 :(得分:0)

试试这个

<?php 
    $data1 = array(1, 2, 3, 4, 5);
    $data2 = array(
                array('id' => 1, 'name' => 'chelsea.jpg'),
                array('id' => 2, 'name' => 'mu.jpg'),
            );
    $isFound = false;
    foreach ($data1 as $key1 => $value1) {
        foreach ($data2 as $key2 => $value2) {
            if($value1 == $value2['id']){
                echo $value2['name'].'<br>';
                $isFound = true;
            }
        }
        if(!$isFound){
           echo "no data";
           $isFound = false; 
        }
    }
?>