php判断数组问题

php中文网
发布: 2016-06-23 13:49:04
原创
863人浏览过

我想在foreach()循环中只显示没有下级的大栏目(例如只显示代码中的大栏目一,大栏目四),有下级栏目的不显示 ,如何判断呢?

cid=栏目ID,name=栏目名称,parent_cid=子栏目的上级栏目ID

{"seller_cat":[
{"cid":"1","name":"大栏目一","parent_cid":"0"},
{"cid":"2","name":"大栏目二","parent_cid":"0"},
{"cid":"3","name":"子栏目二","parent_cid":"2"},
{"cid":"4","name":"子栏目三","parent_cid":"2"},
{"cid":"5","name":"大栏目三","parent_cid":"0"},
{"cid":"6","name":"子栏目一","parent_cid":"5"},
{"cid":"7","name":"大栏目四","parent_cid":"0"}
]}}


回复讨论(解决方案)

$s =<<< TXT{"seller_cat":[{"cid":"1","name":"大栏目一","parent_cid":"0"},{"cid":"2","name":"大栏目二","parent_cid":"0"},{"cid":"3","name":"子栏目二","parent_cid":"2"},{"cid":"4","name":"子栏目三","parent_cid":"2"},{"cid":"5","name":"大栏目三","parent_cid":"0"},{"cid":"6","name":"子栏目一","parent_cid":"5"},{"cid":"7","name":"大栏目四","parent_cid":"0"}]}TXT;$a = json_decode($s);foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, function($v) use ($t) { return $v->parent_cid == $t->cid;}))    echo $t->name;}
登录后复制
登录后复制
登录后复制
登录后复制
登录后复制

$s =<<< TXT{"seller_cat":[{"cid":"1","name":"大栏目一","parent_cid":"0"},{"cid":"2","name":"大栏目二","parent_cid":"0"},{"cid":"3","name":"子栏目二","parent_cid":"2"},{"cid":"4","name":"子栏目三","parent_cid":"2"},{"cid":"5","name":"大栏目三","parent_cid":"0"},{"cid":"6","name":"子栏目一","parent_cid":"5"},{"cid":"7","name":"大栏目四","parent_cid":"0"}]}TXT;$a = json_decode($s);foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, function($v) use ($t) { return $v->parent_cid == $t->cid;}))    echo $t->name;}
登录后复制
登录后复制
登录后复制
登录后复制
登录后复制



Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16

版主大大,你的代码出现这个错误啊


$s =<<< TXT{"seller_cat":[{"cid":"1","name":"大栏目一","parent_cid":"0"},{"cid":"2","name":"大栏目二","parent_cid":"0"},{"cid":"3","name":"子栏目二","parent_cid":"2"},{"cid":"4","name":"子栏目三","parent_cid":"2"},{"cid":"5","name":"大栏目三","parent_cid":"0"},{"cid":"6","name":"子栏目一","parent_cid":"5"},{"cid":"7","name":"大栏目四","parent_cid":"0"}]}TXT;$a = json_decode($s);foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, function($v) use ($t) { return $v->parent_cid == $t->cid;}))    echo $t->name;}
登录后复制
登录后复制
登录后复制
登录后复制
登录后复制



Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16

版主大大,你的代码出现这个错误啊


我改了下版主的代码

<?php$s ='{"seller_cat":[{"cid":"1","name":"大栏目一","parent_cid":"0"},{"cid":"2","name":"大栏目二","parent_cid":"0"},{"cid":"3","name":"子栏目二","parent_cid":"2"},{"cid":"4","name":"子栏目三","parent_cid":"2"},{"cid":"5","name":"大栏目三","parent_cid":"0"},{"cid":"6","name":"子栏目一","parent_cid":"5"},{"cid":"7","name":"大栏目四","parent_cid":"0"}]}';$a = json_decode($s,true);foreach($a['seller_cat'] as $t) {  if($t['parent_cid'] == 0 && ! array_filter($a['seller_cat'], function($v) use ($t) { return $v['parent_cid'] == $t['cid'];}))    echo $t['name'];}
登录后复制


$s =<<< TXT{"seller_cat":[{"cid":"1","name":"大栏目一","parent_cid":"0"},{"cid":"2","name":"大栏目二","parent_cid":"0"},{"cid":"3","name":"子栏目二","parent_cid":"2"},{"cid":"4","name":"子栏目三","parent_cid":"2"},{"cid":"5","name":"大栏目三","parent_cid":"0"},{"cid":"6","name":"子栏目一","parent_cid":"5"},{"cid":"7","name":"大栏目四","parent_cid":"0"}]}TXT;$a = json_decode($s);foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, function($v) use ($t) { return $v->parent_cid == $t->cid;}))    echo $t->name;}
登录后复制
登录后复制
登录后复制
登录后复制
登录后复制



Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16

版主大大,你的代码出现这个错误啊


版主的代码其实没问题哈~可以跑得起。。你也看看我这个吧~~

闭包从 php5.3 开始支持,现在都 php5.6 了
所以我不能保护落后了

对于php 

foreach($a->seller_cat as $t) {   $func = 'return $x->parent_cid == ' . $t->cid . ';';  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, create_function('$x', $func)))    echo $t->name;}
登录后复制

你也可以用类来做。

类方法,可以参考下。

$a = json_decode($s);$ar = array();foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, array(new T($t->cid), 'filter')))    $ar[] = $t->name;}print_r($ar);class T {        private $cid;        function __construct($cid) {                $this->cid = $cid;        }        function filter($v) {                return $v->parent_cid == $this->cid;        }}
登录后复制
登录后复制

类方法,可以参考下。

$a = json_decode($s);$ar = array();foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, array(new T($t->cid), 'filter')))    $ar[] = $t->name;}print_r($ar);class T {        private $cid;        function __construct($cid) {                $this->cid = $cid;        }        function filter($v) {                return $v->parent_cid == $this->cid;        }}
登录后复制
登录后复制



版主大人,用你这个方法可以正常使用了,不过我写漏代码了,你能再帮我看看吗?
代码是这样的,前面的seller_cats我写漏了,加上去就用不了你提供的代码

{"seller_cats":{"@attributes":{"list":"true"},"seller_cat":[
{"cid":"1","name":"大栏目一","parent_cid":"0"},
{"cid":"2","name":"大栏目二","parent_cid":"0"},
{"cid":"3","name":"子栏目二","parent_cid":"2"},
{"cid":"4","name":"子栏目三","parent_cid":"2"},
{"cid":"5","name":"大栏目三","parent_cid":"0"},
{"cid":"6","name":"子栏目一","parent_cid":"5"},
{"cid":"7","name":"大栏目四","parent_cid":"0"}
]}}



$s =<<< TXT{"seller_cat":[{"cid":"1","name":"大栏目一","parent_cid":"0"},{"cid":"2","name":"大栏目二","parent_cid":"0"},{"cid":"3","name":"子栏目二","parent_cid":"2"},{"cid":"4","name":"子栏目三","parent_cid":"2"},{"cid":"5","name":"大栏目三","parent_cid":"0"},{"cid":"6","name":"子栏目一","parent_cid":"5"},{"cid":"7","name":"大栏目四","parent_cid":"0"}]}TXT;$a = json_decode($s);foreach($a->seller_cat as $t) {  if($t->parent_cid == 0 && ! array_filter($a->seller_cat, function($v) use ($t) { return $v->parent_cid == $t->cid;}))    echo $t->name;}
登录后复制
登录后复制
登录后复制
登录后复制
登录后复制



Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16

版主大大,你的代码出现这个错误啊


版主的代码其实没问题哈~可以跑得起。。你也看看我这个吧~~


我的PHP版本太低了,跑不起这个代码呀

为什么用不了? 报什么错误了么? 贴出你的代码看看。

foreach($a->seller_cats->seller_cat as $t) {
  if($t->parent_cid == 0 && ! array_filter($a->seller_cats->seller_cat, array(new T($t->cid), 'filter')))
    $ar[] = $t->name;
}

谢谢jordan102和xuzuning版主大人,问题已经解决了。。。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号