foreach() ループ内で従属列を持たない大きな列だけを表示したい(例えば、コードでは大きな列 1 と大きな列 4 だけを表示する)し、従属列のある列は表示しないようにしたい。
cid=列ID、name=列名、parent_cid=サブ列の上位列ID
{"seller_cat":[
{"cid":"1","name":"Big Column One","parent_cid ":"0"},
{"cid":"2","name":"大きな列 2","parent_cid":"0"},
{"cid":"3","name": "サブ列 2","parent_cid":"2"},
{"cid":"4","name":"サブ列 3","parent_cid":"2"},
{"cid ": "5","name":"大きな列 3","parent_cid":"0"},
{"cid":"6","name":"サブ列 1","parent_cid":" 5" },
{"cid":"7","name":"Big Column Four","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 <5.3 の場合、create_function を使用して次のことを行うことができます匿名関数を作成しますが、create_function を使用します。 eval の使用と同様に、セキュリティ上のリスクがあり、パフォーマンスが低くなります。
rreee
クラスでもできます。
クラスメソッドは以下を参照してください。
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; }}
なぜ使用できないのですか?何かエラーを報告しましたか?コードを投稿して見てください。
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; }