Re: [請益] 陣列取值並計算數量
在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
→
10/13 04:02, , 1F
10/13 04:02, 1F
討論串 (同標題文章)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章