作者HYDRAGA (Ann)
看板PHP
标题Re: [请益] 阵列取值并计算数量
时间Sat Oct 13 02:47:11 2012
在
http://php.net/manual/en/function.array-filter.php 找到解答罗
用法如下
<?php
/*
* filtering an array
*/
function filter_by_value ($array, $index, $value){
if(is_array($array) && count($array)>0)
{
foreach(array_keys($array) as $key){
$temp[$key] = $array[$key][$index];
if ($temp[$key] == $value){
$newarray[$key] = $array[$key];
}
}
}
return $newarray;
}
?>
Example:
<?php
$results = array(
0 => array('key1' => '1', 'key2' => 2, 'key3' => 3),
1 => array('key1' => '12', 'key2' => 22, 'key3' => 32)
);
$nResults = filter_by_value($results, 'key2', '2');
?>
Output :
array(
0 => array('key1' => '1', 'key2' => 2, 'key3' => 3)
);
※ 引述《HYDRAGA (Ann)》之铭言:
: 各位前辈好
: 我在修改书上范本的页面时碰到了一个问题想请教前辈
: 就是...
: 以下这个阵列
: 我想取出GoodsKind=8的阵列存成新的阵列并计算数量
: 我要用哪个函数才对呢?
: $Goods=Array(
: [0]=>Array([GoodsID]=1 [GoodsUrl]=>http://xxx.xxx.xxx [GoodsKind]=>8)
: [1]=>Array([GoodsID]=2 [GoodsUrl]=>http://xxx.xxx.xxx [GoodsKind]=>12)
: [2]=>Array([GoodsID]=3 [GoodsUrl]=>http://xxx.xxx.xxx [GoodsKind]=>8)
: )
: 先感谢各位前辈的帮忙罗!
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.42.233.63
1F:→ MOONRAKER:找到了array_filter()却不用array_filter()写 还满鲜的 10/13 04:02