php 多维数组筛选判断

2020-12-27 浏览:1260
php 多维数组筛选判断
评论:(0)复制地址

直接看代码

<?php
$arr = '[{"url":"gray-01.jpg","alt":"Gray"},{"url":"gray-02.jpg","alt":"Gray"},{"url":"gray-03.jpg","alt":"Gray"},{"url":"gray-04.jpg","alt":"Gray"},{"url":"gray-05.jpg","alt":"Gray"},{"url":"Yellow-01.jpg","alt":"Yellow"},{"url":"Yellow-02.jpg","alt":"Yellow"},{"url":"Yellow-03","alt":"Yellow"},{"url":"Yellow-04","alt":"Yellow"}]';
if($arr ==""){ #判断pictureurls字段为空
    $img1 = "null";
    $img2 = "null";
}else{
    $urls_data = json_decode( $arr, true );
    $i = 0;
    foreach ( $urls_data as $values ) {
      if ( $values[ 'alt' ] == "Yellow" ) { #判断数组存在alt颜色键值,输出对应前两条;else输出默认的前两条
        if($i==0){$img1 = $values['url'];}
        if($i==1){$img2 = $values['url'];}
        $i++;
      } else {
        $img1 = $urls_data[0]['url'];
        $img2 = $urls_data[1]['url'];
      }
    }
}
echo $img1 . "<br>";
echo $img2 . "<br>";
?>


评论:(0)复制地址

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。