i have question about php arrays. i have array like $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'));
i want to get only a,c,d element and create another multi dimensional array like $data1.
$data1=array(
array('a'=>'1','c'=>'3','d'=>'4'),
array('a'=>'5','c'=>'7','d'=>'8'),
array('a'=>'9','c'=>'11','d'=>'12')
);
as a next step i wanted to sort $data1 array by first value of d elements then by value of c elements and finally by a elements and get array like $data2.
$data2=array(
array('a'=>'9','c'=>'11','d'=>'12')
array('a'=>'5','c'=>'7','d'=>'8'),
array('a'=>'1','c'=>'3','d'=>'4')
);
i need little bit explained answer to each step. i'm stuck in here this problem. i want help.. and are there any php array function available for get $data1 array from $data easily?
This will work in your case..
$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);
This will print:
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