Php judges array problems. this post is Last edited by hxlb520 from 2014-09-1908: 53: 23 () in a loop, only large columns without lower levels are displayed (for example, only one column in the code is displayed, and four columns are displayed). how can we determine whether a lower-level column is not displayed? Cid topic ID, name topic name, parent_cid subtopic's parent php judges array problems
This post was last edited by hxlb520 at 08:53:23
In the foreach () loop, I want to show only the big columns with no lower levels (for example, only the big column 1 and the big Column 4 in the code), but the lower-level columns are not displayed, how to judge?
Cid = topic ID, name = topic name, parent_cid = parent topic ID of the subtopic
{"Seller_cat ":[
{"Cid": "1", "name": "topic 1", "parent_cid": "0 "},
{"Cid": "2", "name": "topic 2", "parent_cid": "0 "},
{"Cid": "3", "name": "subtopic 2", "parent_cid": "2 "},
{"Cid": "4", "name": "subtopic 3", "parent_cid": "2 "},
{"Cid": "5", "name": "topic 3", "parent_cid": "0 "},
{"Cid": "6", "name": "subtopic 1", "parent_cid": "5 "},
{"Cid": "7", "name": "topic 4", "parent_cid": "0 "}
]}
------ Solution ----------------------
For more information, see.
$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;
}
}
------ Solution ----------------------
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;
}