我对php数组有疑问。我有像$ data这样的数组。
$data=array(array('a'=>'1','b'=>'2','c'=>'3','d'=>'4'),array('a'=>'5','b'=>'6','c'=>'7','d'=>'8'),array('a'=>'9','b'=>'10','c'=>'11','d'=>'12'));
我想只得到a,c,d元素并创建另一个多维数组,如$ data1。
$data1=array(
array('a'=>'1','c'=>'3','d'=>'4'),
array('a'=>'5','c'=>'7','d'=>'8'),
array('a'=>'9','c'=>'11','d'=>'12')
);
作为下一步,我希望按d元素的第一个值,然后按c元素的值,最后通过元素和$ data2获取数组来对$ data1数组进行排序。
$data2=array(
array('a'=>'9','c'=>'11','d'=>'12')
array('a'=>'5','c'=>'7','d'=>'8'),
array('a'=>'1','c'=>'3','d'=>'4')
);
我需要解释每一步的答案。我陷入了这个问题。我想帮忙.. 是否有任何php数组函数可以轻松地从$ data获取$ data1数组?
答案 0 :(得分:1)
这适用于您的情况..
$data=array(array('a'=>'1','b'=>'2','c'=>'3','d'=>'4'),array('a'=>'5','b'=>'6','c'=>'7','d'=>'8'),array('a'=>'9','b'=>'10','c'=>'11','d'=>'12'));
$data1 = array();
//getting data1 by removing the b's
foreach($data as $d){
while(list($k,$v) = each($d)){
if($k == "b"){
unset($d[$k]);
}
}
array_push($data1, $d);
}
//Sorting the arrays
foreach($data1 as $key => $value){
$ds[$key] = $value["d"];
$cs[$key] = $value["c"];
$as[$key] = $value["a"];
}
array_multisort($ds, SORT_DESC, $cs, SORT_DESC, $as, SORT_DESC, $data1);
print_r($data1);
这将打印:
Array
(
[0] => Array
(
[a] => 9
[c] => 11
[d] => 12
)
[1] => Array
(
[a] => 5
[c] => 7
[d] => 8
)
[2] => Array
(
[a] => 1
[c] => 3
[d] => 4
)
)
DINS