I've got an array that looks like this:
[
{
"id": "1",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "SUPER REDUCES RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
{
"id": "2",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "REDUCED RATE",
"spec_children_first_col": "10% and 13%",
"spec_children_second_col": "food, passenger transport, accommodotion, newspaper, pharmaceutical products,\u2026.(10%); plants, antiques, firewood, cinema, theatre,\u2026(13%)",
"spec_children_third_col": ""
},
{
"id": "3",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "MEDIUM RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
{
"id": "4",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "STANDARD RATE",
"spec_children_first_col": "20%",
"spec_children_second_col": "other",
"spec_children_third_col": ""
},
{
"id": "5",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "ZERO RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
{
"id": "104",
"country_id": "2",
"spec_id": "1",
"spec_children_name": "REDUCED RATE",
"spec_children_first_col": "TEXT 547",
"spec_children_second_col": "TEXT 1000",
"spec_children_third_col": ""
}
]
What i want: I'd like to sort this array by 2 object key compare, if spec_children_name
and spec_id
. Finally, it should look like:
[
[
{
"id": "1",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "SUPER REDUCES RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
{
"id": "2",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "REDUCED RATE",
"spec_children_first_col": "10% and 13%",
"spec_children_second_col": "food, passenger transport, accommodotion, newspaper, pharmaceutical products,\u2026.(10%); plants, antiques, firewood, cinema, theatre,\u2026(13%)",
"spec_children_third_col": ""
},
,
{
"id": "104",
"country_id": "2",
"spec_id": "1",
"spec_children_name": "REDUCED RATE",
"spec_children_first_col": "TEXT 547",
"spec_children_second_col": "TEXT 1000",
"spec_children_third_col": ""
}
{
"id": "3",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "MEDIUM RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
{
"id": "4",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "STANDARD RATE",
"spec_children_first_col": "20%",
"spec_children_second_col": "other",
"spec_children_third_col": ""
},
{
"id": "5",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "ZERO RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
}
]
*Note the two objects with same keys (spec_id
and spec_children_name
) one after another.
What i've tried so far:
function array_sort($array, $on, $order=SORT_ASC){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $on) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
switch ($order) {
case SORT_ASC:
asort($sortable_array);
break;
case SORT_DESC:
arsort($sortable_array);
break;
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
Fn call: array_sort($array, 'spec_children_name', SORT_ASC);
Dynamic solution please, with key parameters to sort by
Another approach:
usort($country_specs_meta_results, function($a, $b){
$c .= $b['spec_id'] - $a['spec_id'];
$c .= strcmp($a['spec_children_name'], $b['spec_children_name']);
return $c;
});
EDIT: I've updated the arrays as solution above still wreck the output
EDIT 2: This is the output with the function array_sort modified by @Jeto:
{
"4": {
"id": "5",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "ZERO RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
"0": {
"id": "1",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "SUPER REDUCES RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
},
"3": {
"id": "4",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "STANDARD RATE",
"spec_children_first_col": "20%",
"spec_children_second_col": "other",
"spec_children_third_col": ""
},
"1": {
"id": "2",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "REDUCED RATE",
"spec_children_first_col": "10% and 13%",
"spec_children_second_col": "food, passenger transport, accommodotion, newspaper, pharmaceutical products,\u2026.(10%); plants, antiques, firewood, cinema, theatre,\u2026(13%)",
"spec_children_third_col": ""
},
"76": {
"id": "104",
"country_id": "2",
"spec_id": "1",
"spec_children_name": "REDUCED RATE",
"spec_children_first_col": "10% and 13% BG",
"spec_children_second_col": "TEXT FOR BG",
"spec_children_third_col": ""
},
"2": {
"id": "3",
"country_id": "1",
"spec_id": "1",
"spec_children_name": "MEDIUM RATE",
"spec_children_first_col": "",
"spec_children_second_col": "",
"spec_children_third_col": ""
}
}
So the order changed but not for the "spec_children_name": "REDUCED RATE"
only