Sort an array multidimension by key php [duplicate] - php

This question already has answers here:
How to Sort a Multi-dimensional Array by Value
(16 answers)
How can I sort arrays and data in PHP?
(14 answers)
Closed 6 years ago.
I have this array :
Array
(
[26] => Array
(
[total_auctions] => 1
[total_price] => 0
)
[24] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[25] => Array
(
[total_auctions] => 0
[total_price] => 0
)
)
I want to sort this array to be :
Array
(
[24] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[25] => Array
(
[total_auctions] => 0
[total_price] => 0
)
[26] => Array
(
[total_auctions] => 1
[total_price] => 0
)
)
I tried with array_multisort but not work. Can you help me please ? Thx in advance. I don't understand where is the problem, normally should work

Just use ksort() to sort any array keys
<?php
$arr = array(
'26' => array('total_auctions' => 1,'total_price' => 0),
'24' => array('total_auctions' => 0,'total_price' => 0),
'25' => array('total_auctions' => 0,'total_price' => 0)
);
ksort($arr);
print '<pre>';print_r($arr);
exit;
?>

Related

merge two equal structural multi-arrays with different data

i can't believe that i can't find a solution to this seemingly simple problem, and i tried everything.
i have two multi arrays:
the first one:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
)
the second one has the exact same structure, but with new data:
Array
(
[3] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
what i need mergin both multi-arrays is this result:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
[final_approved] =>
[final_id_note] => 19
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
)
but i can't get this result with any common php function:
array_merge:
Array
(
[0] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[1] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
[2] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
it replaces with new indexes.
array_push:
Array
(
[3] => Array
(
[1] => Array
(
[approved] => 1
[id_note] => 1
[surname] => Rawson
)
[2] => Array
(
[approved] =>
[id_note] => 18
[surname] => Vallejo
)
)
[4] => Array
(
[1] => Array
(
[school_id] => 1
)
[2] => Array
(
[available] => 1
)
)
[5] => Array
(
[3] => Array
(
[1] => Array
(
[final_approved] => 1
[final_id_note] => 1
)
[2] => Array
(
[final_approved] =>
[final_id_note] => 19
)
)
)
)
it literally pushes in with the second array with new indexes, which is correct of course lol
i guess that the answer is pretty simple, but i already googled and try everything that my newbie ability can.
can anyone give me some advice?
thanks!
There are recursive array functions in PHP. array_merge_recursive, despite the promising name, will not work here, as it will reindex the numeric keys and add overlapping the data to the end.
However, there is array_replace_recursive that does exactly what you want when you have a multidimensional array with significant keys. Here's your data (in a usable format!):
// The base data:
$a_one = [
3 => [
1 => [
'approved' => 1,
'id_note' => 1,
'surname' => 'Rawson',
],
2 => [
'approved' => 0,
'id_note' => 18,
'surname' => 'Vallejo',
],
],
4 => [
1 => [
'school_id' => 1,
],
2 => [
'available' => 1,
],
],
];
// Data to be added by matching keys:
$a_two = [
3 => [
1 => [
'final_approved' => 1,
'final_id_note' => 1,
],
2 => [
'final_approved' => 0,
'final_id_note' => 19,
],
],
];
Then let's mash them together:
$merged = array_replace_recursive($a_one, $a_two);
This results in the following "merged" array:
[
3 => [
1 => [
'approved' => 1,
'id_note' => 1,
'surname' => 'Rawson',
'final_approved' => 1,
'final_id_note' => 1,
],
2 => [
'approved' => 0,
'id_note' => 18,
'surname' => 'Vallejo',
'final_approved' => 0,
'final_id_note' => 19,
],
],
4 => [
1 => [
'school_id' => 1,
],
2 => [
'available' => 1,
],
],
]
In summary, the way this function behaves (from the manual, numbering added):
If a key from the first array exists in the second array, its value will be replaced by the value from the second array.
If the key exists in the second array, and not the first, it will be created in the first array.
If a key only exists in the first array, it will be left as is.
Or in a more compact statement:
key: scalar EXISTS in A, B => REPLACE from B to A
key: scalar EXISTS in B => CREATE in A
key: scalar EXISTS in A => LEAVE in A
Again, the manual says:
When the value in the first array is scalar, it will be replaced by the value in the second array, may it be scalar or array. When the value in the first array and the second array are both arrays, array_replace_recursive() will replace their respective value recursively.
What's important to note here is that array members with matching keys, that are arrays, will not be "replaced" in full. The "replacement" only applies to the final scalar values or "leaves" of the array (per the logic above). The arrays will be "merged", ie. non-matching keys with scalar values will be combined within each "final array" (or "leafy array" for lack of a better term).
N.B. If you're using this function to "merge" more complex arrays, you'll want to double-check that the outcome matches your expectations. I can't remember off the top of my head all the "quirks" that exist in using this function, but I assure you they are there... don't assume that it will create a seamless union of any and all datasets you may throw at it.

How to delete array of array if particular key doesn't exist in it?

I have collection of array and i want delete array of array where "answer_id" key is not exits.
my array look like this.
Array
(
[0] => Array
(
[question_no] => 1
[subject_id] => 1
[question_id] => 255
[currect_ans_id] => 2657
[time_taken] => 110
[is_visited] => 1
[is_saved] => 0
[answer_id] => 2659
)
[1] => Array
(
[question_no] => 2
[subject_id] => 1
[question_id] => 256
[currect_ans_id] => 2662
[time_taken] => 0
[is_visited] => 1
[is_saved] => 0
)
)
and want array like this(where answer_id key exits).
Array
(
[0] => Array
(
[question_no] => 1
[subject_id] => 1
[question_id] => 255
[currect_ans_id] => 2657
[time_taken] => 110
[is_visited] => 1
[is_saved] => 0
[answer_id] => 2659
)
)
You can use array_filter to remove entries which don't have an answer_id:
$output = array_filter($input, function ($a) { return isset($a['answer_id']); });
Demo on 3v4l.org

PHP - Sort Multidimensional Array by string [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
How to compare 2 strings alphabetically in PHP?
(5 answers)
Closed 5 years ago.
I have an array called "$myarray", which looks like this:
[0] => Array
(
[PrinterID] => 4
[PrinterName] => PRT04_GL
[UserID] => 1
[isDefaultPrinter] => 0
[isMapped] => 0
)
[1] => Array
(
[PrinterID] => 1
[PrinterName] => PRT01_Zentral
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[2] => Array
(
[PrinterID] => 2
[PrinterName] => PRT02_BH
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[3] => Array
(
[PrinterID] => 3
[PrinterName] => PRT03_EDV
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 1
)
I want to sort the data by ["PrinterName"]. The wanted result would be:
[0] => Array
(
[PrinterID] => 1
[PrinterName] => PRT01_Zentral
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[1] => Array
(
[PrinterID] => 2
[PrinterName] => PRT02_BH
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 0
)
[2] => Array
(
[PrinterID] => 3
[PrinterName] => PRT03_EDV
[UserID] =>
[isDefaultPrinter] => 0
[isMapped] => 1
)
[3] => Array
(
[PrinterID] => 4
[PrinterName] => PRT04_GL
[UserID] => 1
[isDefaultPrinter] => 0
[isMapped] => 0
)
Based on this example I tried the following:
uasort($myarray, 'sort_by_order');
function sort_by_order ($a, $b) {
return $a['PrinterName'] - $b['PrinterName'];
}
This does not work. I get errors like "A non-numeric value encountered ...", of course because my values are strings and the function is for numeric values.
How do I sort the array?
Thank you!
EDIT:
I think I did find the solution here. I used this and it seems to work:
uasort($myarray, 'sort_by_order');
function sort_by_order ( $a, $b ) { return strcmp($a['PrinterName'], $b['PrinterName']); }
Please try with $a['PrinterName'] > $b['PrinterName']
It will compare the values

Sort an associative array with custom order [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 5 years ago.
How can we sort an associative array with custom order?
My array look like this
Array
(
[pa_color] => Array
(
[name] => pa_color
[value] =>
[position] => 0
[is_visible] => 1
[is_variation] => 1
[is_taxonomy] => 1
)
[pa_dimension] => Array
(
[name] => pa_dimension
[value] =>
[position] => 1
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travel-duration] => Array
(
[name] => pa_travel-duration
[value] =>
[position] => 2
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travel-type] => Array
(
[name] => pa_travel-type
[value] =>
[position] => 3
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_travelling-with] => Array
(
[name] => pa_travelling-with
[value] =>
[position] => 4
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_volume] => Array
(
[name] => pa_volume
[value] =>
[position] => 5
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
[pa_weight] => Array
(
[name] => pa_weight
[value] =>
[position] => 6
[is_visible] => 1
[is_variation] => 0
[is_taxonomy] => 1
)
)
and i want this array is like pa_travel-duration first pa_volume second ?? I know there is a native php function usort but i could not understand this.
This will do the work, but im pretty sure there's much better ways to do it:
Code:
$array = array("pa_color" => "color",
"pa_dimension" => "dimension",
"pa_travel-duration" => "Random Stuff: " . rand(100,999),
"pa_volume" => "volumen"
);
$tmp = array("pa_travel-duration" => $array["pa_travel-duration"],
"pa_volume" => $array["pa_volume"],
);
unset($array["pa_travel-duration"], $array["pa_volume"]);
$array = array_merge($tmp,$array);
print_r($array);
Result:
Array
(
[pa_travel-duration] => Random Stuff: 127
[pa_volume] => volumen
[pa_color] => color
[pa_dimension] => dimension
)
Take care because if the array doesn't have the proper key it will throw an error, you need to add few checks there.
$sort_by = array('pa_travel-duration', 'pa_volume', 'pa_color','pa_dimension','pa_travel-type','pa_travelling-with','pa_weight');
$temp_arr = array();
foreach ($sort_by as $key) {
$temp_arr[$key] = $data[$key];
}
$data = $temp_arr;
echo '<pre>'; print_r($data);
Define your order in $sort_by array

Sort an array based on its sub-array [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
sorting array based on inner-array key-value
Array (
[0] => Array (
[id] => 2
[name] => 4
[closed] =>
[date] => 1319625994
[votes_up] => 0
[votes_down] => 0
[votes_pct_up] => 0
[votes_pct_down] => 0
[votes_balance] => 0
[votes_total] => 0 )
[1] => Array (
[id] => 3
[name] => 3
[closed] =>
[date] => 1319625994
[votes_up] => 0
[votes_down] => 0
[votes_pct_up] => 0
[votes_pct_down] => 0
[votes_balance] => 0
[votes_total] => 0 )
[2] => Array (
[id] => 4
[name] => 2
[closed] =>
[date] => 1319625995
[votes_up] => 1
[votes_down] => 0
[votes_pct_up] => 100
[votes_pct_down] => 0
[votes_balance] => 1
[votes_total] => 1 )
[3] => Array (
[id] => 1
[name] => 1
[closed] =>
[date] => 1319623450
[votes_up] => 2
[votes_down] => 0
[votes_pct_up] => 100
[votes_pct_down] => 0
[votes_balance] => 2
[votes_total] => 2 )
)
How do i sort these according to their [votes_balance] value?
Use the usort function of PHP with something like this:
function compare($a, $b) {
if($a['id'] == $b['id']) return 0;
return ($a['id'] < $b['id']) -1 : 1;
}

Categories