Maison > Article > développement back-end > php判断数组问题
我想在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;}
$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;}
<?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;}
闭包从 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; }}
$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;}
为什么用不了? 报什么错误了么? 贴出你的代码看看。
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版主大人,问题已经解决了。。。