Php array judgment problems

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.