php判断数组问题 本帖最后由 hxlb520 于 2014-09-19 08:53:23 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $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;} Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊 Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊我改了下版主的代码<?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'];} Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊版主的代码其实没问题哈~可以跑得起。。你也看看我这个吧~~ 闭包从 php5.3 开始支持,现在都 php5.6 了所以我不能保护落后了 对于php < 5.3 可以用create_function创建匿名函数,不过用create_function 与用eval类似,有安全隐患,且性能低。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; }} 版主大人,用你这个方法可以正常使用了,不过我写漏代码了,你能再帮我看看吗?代码是这样的,前面的seller_cats我写漏了,加上去就用不了你提供的代码{"seller_cats":{"@attributes":{"list":"true"},"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"}]}} Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊版主的代码其实没问题哈~可以跑得起。。你也看看我这个吧~~我的PHP版本太低了,跑不起这个代码呀 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版主大人,问题已经解决了 跪求英文版的天气预报 一个PHPCHINA青岛 学员的愤怒和无奈,请求广大PHPer帮忙!!! 例如a.php是发邮件使用的,请问怎么样使用任务和计划 PHP正则运用 大家对smarty有什么看法 请问mysql一个表里面最多多少个列?急啊! 一个奇怪的问题 请问为什么提交的表单,而值却传不到下一页。 求助有关php读取文件的问题 请教:如何让多个php文件可以使用同一个变量? 如何将@die('导入数据失败。请确认文件是否正确并未被其它进程占用!'); 提示信息在两文件传递并继续执行另外的语句 关于PHP拆分字符串的方法
{"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;
}
Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊
Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊
我改了下版主的代码<?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'];
}
Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊
版主的代码其实没问题哈~可以跑得起。。你也看看我这个吧~~
所以我不能保护落后了
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;
}
}
版主大人,用你这个方法可以正常使用了,不过我写漏代码了,你能再帮我看看吗?
代码是这样的,前面的seller_cats我写漏了,加上去就用不了你提供的代码{"seller_cats":{"@attributes":{"list":"true"},"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"}
]}}
Parse error: syntax error, unexpected T_FUNCTION in D:\aaa\3.php on line 16版主大大,你的代码出现这个错误啊
版主的代码其实没问题哈~可以跑得起。。你也看看我这个吧~~
我的PHP版本太低了,跑不起这个代码呀
if($t->parent_cid == 0 && ! array_filter($a->seller_cats->seller_cat, array(new T($t->cid), 'filter')))
$ar[] = $t->name;
}