Php judges the array problem. I want to display only the large columns with no lower levels in the foreach () loop (for example, only the big column one in the code and the big column four in the code ), how can I judge if there are lower-level columns that are not displayed?
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 "}
]}
Reply to discussion (solution)
$ S = <TXT {"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"}]} TXT; $ a = json_decode ($ s ); foreach ($ a-> seller_cat as $ t) {if ($ t-> parent_ci D = 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": "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"}]} TXT; $ a = json_decode ($ s ); foreach ($ a-> seller_cat as $ t) {if ($ t-> parent_ci D = 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
This error occurs in your code.
$ S = <TXT {"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"}]} TXT; $ a = json_decode ($ s ); foreach ($ a-> seller_cat as $ t) {if ($ t-> parent_ci D = 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
This error occurs in your code.
I changed the moderator's code.
$ S = <TXT {"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"}]} TXT; $ a = json_decode ($ s ); foreach ($ a-> seller_cat as $ t) {if ($ t-> parent_ci D = 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
This error occurs in your code.
The moderator's code is actually okay ~ It can run easily .. Take a look at me ~~ The closure is supported since php5.3. now all php5.6 is supported.
So I cannot protect myself from falling behind
For php <5.3, you can use create_function to create anonymous functions. However, using create_function is similar to using eval, which poses a security risk and has low performance.
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;}
You can also use classes. 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; }}
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; }}
Moderator, you can use this method normally, but I have missed the code. can you help me see it again?
The code is like this. I have missed the previous seller_cats, and I cannot use the code you provided when I add it.
{"Seller_cats": {"@ attributes": {"list": "true"}, "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 "}
]}
$ S = <TXT {"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"}]} TXT; $ a = json_decode ($ s ); foreach ($ a-> seller_cat as $ t) {if ($ t-> parent_ci D = 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
This error occurs in your code.
The moderator's code is actually okay ~ It can run easily .. Take a look at me ~~
My PHP version is too low to run this code. Why not? Is there any error? Paste your code.
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;
}
Thank you, jordan102 and the xuzuning moderator. The problem has been solved...