直接看代码
<?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>"; ?>
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
相关文章
Jquery读取json文件并筛选条件的代码2020-12-19
php查询某个表的数据总量代码2020-02-29
php生成静态html分页实现方法2019-12-07
ajax 随机调用json数组显示2019-06-19
php+jquery+ajax+json的一个最简单实例2019-06-18
php 中返回json 数据实现callback2018-11-18