脚本宝典收集整理的这篇文章主要介绍了php – 将数组拆分为子数组,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$o = json_decode('[{"id":"1","color":"green"},{"id":"2",{"id":"3","color":"yellow"},{"id":"4","color":"green"}]');
根据每个项目的颜色属性,填充相应的子数组
$a = array("green","yellow","blue"); function isGreen($VAR){ return($var->color == "green"); } $greens = array_filter($o,"isGreen"); $yellows = array_filter($o,"isYellow"); // and all possible categories in $a..
$o = json_decode('[{"id":"1","color":"green"}]'); $greens = array_filter($o,function($item) { if ($item->color == 'green') { return true; } return false; });
或者,如果您想创建一些非常通用的东西,您可以执行以下操作:
function filterArray($array,$tyPE,$value) { $result = array(); foreach($array as $item) { if ($item->{$type} == $value) { $result[] = $item; } } return $result; } $o = json_decode('[{"id":"1","color":"green"}]'); $greens = filterArray($o,'color','green'); $yellows = filterArray($o,'yellow');
以上是脚本宝典为你收集整理的php – 将数组拆分为子数组全部内容,希望文章能够帮你解决php – 将数组拆分为子数组所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。