Sorting multidimentional Array Keys As Number - php

I'm having a problem to sort multidimensional array.
print_r($myarray); is giving following output:
Array
(
[0] => Array
(
[ID] => 10
[Code] => 12-111
[Name] => putup1
)
[1] => Array
(
[ID] => 11
[Code] => 12-21
[Name] => putup2
)
[2] => Array
(
[ID] => 12
[Code] => 12-1
[Name] => putup2
)
)
I took a reference from this link: Sort MultiDimensional Array and sorted using column Code. it given me output like this:
Array
(
[0] => Array
(
[ID] => 10
[Code] => 12-1
[Name] => putup1
)
[1] => Array
(
[ID] => 12
[Code] => 12-111
[Name] => putup2
)
[2] => Array
(
[ID] => 11
[Code] => 12-21
[Name] => putup2
)
)
And I expecting output like :
Array
(
[0] => Array
(
[ID] => 10
[Code] => 12-1
[Name] => putup1
)
[2] => Array
(
[ID] => 11
[Code] => 12-21
[Name] => putup2
)
[1] => Array
(
[ID] => 12
[Code] => 12-111
[Name] => putup2
)
)
Is it possible? Please suggest me...
Thank you :)

You are currently sorting on code, but as text and not as a number. To sort it as you want, you have to write a comparison function that indicates whether one code is bigger than another:
function sortByCode($a, $b) {
$aParts = explode('-', $a['Code']);
$bParts = explode('-', $b['Code']);
for ($i = 0; $i < count($aParts); $i++) {
if ($aParts[$i] < $bParts[$i]) {
return -1;
} else if ($aParts[$i] > $bParts[$i]) {
return 1;
}
}
return 0;
}
usort($myArray, 'sortByCode');

Related

sort an array with another array which has sorted keys

How to sort an 1st array with 2nd array which has sorted keys in 2nd array without using any loop.
1st Array.
$chunk = array(
[0] => Array
(
[id] => 212
[order] => 1
[title] => fdfdfdfdf
)
[1] => Array
(
[id] => 5
[order] => 2
[title] =>
)
[2] => Array
(
[id] => 781
[order] => 3
[title] =>
)
)
2nd array with sorted keys of 1st array.
$sort = array
(
[2] => 2
[0] => 0
[1] => 1
)
You could use array_map for that:
$arr = array_map(function($val) use($chunk){
return $chunk[$val];
}, $sort);
This is the output:
Array
(
[2] => Array
(
[id] => 781
[order] => 3
[title] =>
)
[0] => Array
(
[id] => 212
[order] => 1
[title] => fdfdfdfdf
)
[1] => Array
(
[id] => 5
[order] => 2
[title] =>
)
)
Now, if you want the keys to be 0,1,2..., you can use array_values, after mapping:
$arr = array_values($arr);
And the output:
Array
(
[0] => Array
(
[id] => 781
[order] => 3
[title] =>
)
[1] => Array
(
[id] => 212
[order] => 1
[title] => fdfdfdfdf
)
[2] => Array
(
[id] => 5
[order] => 2
[title] =>
)
)
Of course, there is no function for this. You'll have to do something similar
<?php
$chunk = [
// data
];
$sorted = [];
$sort = [
// ordered keys
];
foreach($sort as $keyToFind) {
foreach($chunk as $arrayElement) {
if($arrayElement['id'] == $keyToFind)) {
$sorted[$keyToFind] = $arrayElement;
}
}
}
As you can see, this is a bit time and ressources consumming because of the two imbricated foreaches. Let's hope your arrays are not so big

Merge Array by value PHP

I have a problem, I would like to merge arrays by value. Below is a entry example, the entry array have a 100 records
Array
(
[0] => Array
(
[id] => 1
[code] => dfrr5tv5t5vt5
[status] => online
)
[1] => Array
(
[id] => 2
[code] => e32e3e2e2323e23e
[status] => online
)
[2] => Array
(
[id] => 1
[desc] => Some_description
)
[3] => Array
(
[id] => 2
[desc] => Some_description_2
)
....
)
I would like to get the following result through merge array by [id]
Array
(
[0] => Array
(
[id] => 1
[code] => dfrr5tv5t5vt5
[status] => online
[desc] => Some_description
)
[1] => Array
(
[id] => 2
[code] => e32e3e2e2323e23e
[status] => online
[desc] => Some_description_2
)
....
)
Use assocative arrays. Use $row["id"] as associative index.
$result = [];
foreach ($arr as $row) {
$result[$row["id"]] = isset($result[$row["id"]]) ? array_merge($result[$row["id"]], $row) : $row;
}
$result = array_values($result); // optional, this removes associative keys

How to merge array using same id

I have two arrays and I want to combine them together
1) first look like this:
[11] => Array
(
[id] => 11
[name] => test
)
[12] => Array
(
[id] => 12
[name] => test1
)
2) second array look like this:
[0] => Array
(
[offer_id] => 11
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[1] => Array
(
[offer_id] => 12
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
I want this result. How is it possible can any one provide solution for same?
[11] => Array
(
[id] => 11
[name] => test
[countries] => Array
(
[SA] => Array
(
[id] => 682
)
)
)
[12] => Array
(
[id] => 12
[name] => test
[countries] => Array
(
[KW] => Array
(
[id] => 414
)
)
)
Thank you for the help!
Try this:
foreach ($array1 as &$arr1) {
$offer_id = $arr1['id']; // Search for this offer_id in array 2
$match = array_filter($array2, function($v) use ($offer_id){
return $v['offer_id'] == $offer_id; // Return matching offer id
});
$arr1['countries'] = current($match)['countries']; // Assign matched country to array
}

Sort multi-dimensional array by considering 2 different properties

I have an array for example
Array
(
[0] => Array
(
[id] => 6
[name] => ah
[order] => 4
)
[1] => Array
(
[id] => 5
[name] => hz
[order] =>
)
[2] => Array
(
[id] => 7
[name] => ch
[order] =>
)
[3] => Array
(
[id] => 5
[name] =>
[order] =>
)
[4] => Array
(
[id] => 4
[name] => zh
[order] => 1
)
)
It needs to be sorted first by "order", if order isn't available, it is sorted in alphabetical order with the "name" (but those arrays goes after all the arrays that have the sort order), and if no "order" and no "name", it goes at the end of the array.
So the above array would need to become:
Array
(
[0] => Array
(
[id] => 4
[name] => zh
[order] => 1
)
[1] => Array
(
[id] => 6
[name] => ah
[order] => 4
)
[2] => Array
(
[id] => 7
[name] => ch
[order] =>
)
[3] => Array
(
[id] => 5
[name] => hz
[order] =>
)
[4] => Array
(
[id] => 5
[name] =>
[order] =>
)
)
I've tried some for looping but nothing close to a solution.
Have you looked at array_multisort?
foreach ($data as $key => $row) {
$name[$key] = $row['name'];
$order[$key] = $row['order'];
}
array_multisort($name, SORT_DESC, $order, SORT_ASC, $data);
PHP array multisort
array_multisort() might not provide enough control to achieve the sorting you require.
As an alternative, you could use uasort() and provide a custom comparison function.
For example:
function compare($a, $b) {
if (empty($a['order']) && !empty($b['order'])) {
return 1;
}
if (!empty($a['order']) && empty($b['order'])) {
return -1;
}
if ($a['order'] == $b['order']) {
return strnatcmp($a['name'], $b['name']);
}
return ($a['order'] < $b['order']) ? -1 : 1;
}
uasort($arr, 'compare');

How to merge two 2D php arrays while I want to add up some values?

I have two php arrays like these:
Array
(
[0] => Array
(
[id] => 712
[count] => 5
)
[1] => Array
(
[id] => 5510
[count] => 3
)
)
Array
(
[0] => Array
(
[id] => 856
[count] => 7
)
[1] => Array
(
[id] => 5510
[count] => 10
)
)
Now I want to make the merge result like this:
Array
(
[0] => Array
(
[id] => 712
[count] => 5
)
[1] => Array
(
[id] => 856
[count] => 3
)
[2] => Array
(
[id] => 5510
[count] => 13
)
)
Just add the count up of those having the same id.
And of course the real array is much more complicated than the example above.
Can you show me a way to deal with this?
This should work for you
/**
* merge counts for arrays
* #param array $arrays,...
* #return array
*/
function merge_counts(){
$arrays = func_get_args();
$ret = array();
foreach($arrays as $arr){
foreach($arr as $item){
if(array_key_exists($k = $item['id'], $ret)){
$ret[$k]['count'] += $item['count'];
}
else {
$ret[$k] = $item;
}
}
}
return array_values($ret);
}
Usage
$result = merge_counts($one, $two);
print_r($result);
// alternatively...
// $result = merge_counts($one, $two, $three, ...);
Output
Array
(
[0] => Array
(
[id] => 712
[count] => 5
)
[1] => Array
(
[id] => 5510
[count] => 13
)
[2] => Array
(
[id] => 856
[count] => 7
)
)

Categories