How to re-sort a multidimensional array? - php

How to sort the array alphabetically below using as the key criterion label? I tried using array_multisort, usort, rsort, and sort, but it did not work.
array(3) {
[0]=>
array(2) {
["id"]=>
string(1) "9"
["label"]=>
string(26) "ffffff"
}
[1]=>
array(2) {
["id"]=>
string(2) "10"
["label"]=>
string(25) "aaaaaaaaa"
}
[2]=>
array(2) {
["id"]=>
string(1) "6"
["label"]=>
string(5) "dddddd"
}
}

You can sort the array using both usort() and strcmp()
usort($arr, function($e1, $e2)
{
$cmp = strcmp($e1['label'], $e2['label']);
if($cmp == 0) { return 0; }
return $cmp > 0 ? 1 : -1;
});

Related

array_udiff seems not work

I'm trying to compare two array with array_udiff, but it's very weired. it seems array_udiff not get the right answer. Here is the live demo. The result should be an empty array, but leave one element unfiltered out.
<?php
$string = '{
"sakiniai": [
{
"Faktas": "A",
"value": "true"
},
{
"Faktas": "B",
"value": "true"
},
{
"Faktas": "A",
"value": "false"
}
]
}';
$sakiniais = json_decode($string, true)['sakiniai'];
$v = $sakiniais[0];
$arr[] = $v;
$v['value'] = $v['value'] == "true" ? "false" : "true";
$arr[] = $v;
var_dump($arr);
var_dump($sakiniais);
print_r(array_udiff($arr, $sakiniais, function($a, $b){
/*
var_dump($a);
var_dump($b);
var_dump($a == $b);
echo "\n\n\n";
*/
return $a == $b ? 0 : -1;}
));
the output
array(2) {
[0]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
[1]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
}
array(3) {
[0]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
[1]=>
array(2) {
["Faktas"]=>
string(1) "B"
["value"]=>
string(4) "true"
}
[2]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
}
Array
(
[1] => Array
(
[Faktas] => A
[value] => false
)
)
The problem is that array_udiff is not performing the comparison between all values, and this seems to be caused by your compare function.
array_udiff() expects that the callable function is a real compare function, but you are returning always 0 and -1, but never 1.
Before doing its job, array_udiff() tries to order both arrays and remove duplicates too. If it can't rely on your comparison function, it can't perform all the needed comparison and some values are "jumped".
Look at all comments in the documentation expecially napcoder comment
Note that the compare function is used also internally, to order the
arrays and choose which element compare against in the next round.
If your compare function is not really comparing (ie. returns 0 if
elements are equals, 1 otherwise), you will receive an unexpected
result.
This is demonstrated looking at your arrays
$arr
array(2) {
[0]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
[1]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
}
and $sakiniais
array(3) {
[0]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
[1]=>
array(2) {
["Faktas"]=>
string(1) "B"
["value"]=>
string(4) "true"
}
[2]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
}
You should expect all the combinations between the two arrays to be tested, but the tested combinations (below) doesn't include A-False vs. A-False
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
bool(false)
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
array(2) {
["Faktas"]=>
string(1) "B"
["value"]=>
string(4) "true"
}
bool(false)
array(2) {
["Faktas"]=>
string(1) "B"
["value"]=>
string(4) "true"
}
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
bool(false)
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
bool(true)
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
bool(false)
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
array(2) {
["Faktas"]=>
string(1) "B"
["value"]=>
string(4) "true"
}
bool(false)
In the correct answer at this post there are some other useful insights on how array_udiff works
You can change your return statement like this
if ($a < $b) {
return -1;
} elseif ($a > $b) {
return 1;
} else {
return 0;
}
(if you are wondering how two arrays can be compared for less/greater, have a look at this link, in the section "Comparison with Various Types" and the "Example #2 Transcription of standard array comparison" example)
Nothing strange, everything works as expected. Take a look at arrays comparing:
$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
so, $arr[0] == $sakiniai[0] is true, but $arr[1] == $sakiniai[2] is false and the output is $arr[1]
$arr
array(2) {
[0]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
[1]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
}
$sakiniai
array(3) {
[0]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(4) "true"
}
[1]=>
array(2) {
["Faktas"]=>
string(1) "B"
["value"]=>
string(4) "true"
}
[2]=>
array(2) {
["Faktas"]=>
string(1) "A"
["value"]=>
string(5) "false"
}
}
result
Array
(
[1] => Array
(
[Faktas] => A
[value] => false
)
)

Merge rows between two arrays of objects based on column value [duplicate]

This question already has answers here:
Merging and group two arrays containing objects based on one identifying column value
(4 answers)
Closed 5 months ago.
After I merged two arrays like this array_merge($array1, $array2);, it becomes like this:
array(10) {
[0]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "one"
}
[1]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "two"
}
[2]=>
object(stdClass) (2) {
["id"]=>
string(1) "3"
["text"]=>
string(4) "three"
}
[3]=>
object(stdClass) (2) {
["id"]=>
string(1) "4"
["text"]=>
string(8) "four"
}
[4]=>
object(stdClass) (2) {
["id"]=>
string(1) "5"
["text"]=>
string(3) "five"
}
[5]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["unit"]=>
string(1) "0"
}
[6]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["unit"]=>
int(0)
}
[7]=>
object(stdClass) (2) {
["id"]=>
string(1) "3"
["unit"]=>
int(0)
}
[8]=>
object(stdClass) (2) {
["id"]=>
string(1) "4"
["unit"]=>
string(1) "0"
}
[9]=>
object(stdClass) (2) {
["id"]=>
string(1) "5"
["unit"]=>
int(1)
}
}
Which means both arrays are literally merged. But what I wanted is since both arrays has common property called id and same value for it, it should become like:
array(2) {
[0]=>
object(stdClass) (2) {
["id"]=>
string(1) "1"
["text"]=>
string(5) "one"
["unit"]=>
int(0)
}
[1]=>
object(stdClass) (2) {
["id"]=>
string(1) "2"
["text"]=>
string(8) "two"
["unit"]=>
int(2)
}
}
Note that array1 has id, text while array2 has id and unit.
I did refer here as tried the first answer which suggest to use array_map(), but for me I'm getting error saying argument 1 is not an array.
Combine two arrays into a single array based on a common column value
EDIT:
tried this (doesn't work):
$array1 = array_walk($array1, function(&$value) { $value = (array) $value; })
$array2 = array_walk($array2, function(&$value) { $value = (array) $value; })
function modifyArray($a, $b)
{
if (!empty($a) && !empty($b)) {
return array_merge($a, $b);
} else if (!empty($a) && empty($b)) {
return $a;
} else if (empty($a) && !empty($b)) {
return $b;
}
}
$new = array_map("modifyArray", $array1, $array2);
var_dump($new);
Merging objects is noticeably more tedious than arrays. I'd be tempted to convert the array of objects to an array or arrays in my own project, but I won't for this solution.
Unlike arrays which can enjoy array_merge() or the union operator, objects need to be pushed in manually.
I am temporarily grouping data by using the id values as first level keys in the loop, then optionally sorting by those keys, then re-indexing the output to remove the temporary keys.
Code: (Demo):
$output = [];
foreach ($poorly_merged as $object) {
if (!isset($output[$object->id])) {
$output[$object->id] = $object;
} else {
foreach ($object as $property => $value) {
$output[$object->id]->{$property} = $value;
}
}
}
ksort($output); // optionally order by ids
var_export(array_values($output));
Or:
$output = [];
foreach ($poorly_merged as $object) {
if (!isset($output[$object->id])) {
$output[$object->id] = (object)[];
}
foreach ($object as $property => $value) {
$output[$object->id]->{$property} = $value;
}
}
ksort($output); // optionally order by ids
var_export(array_values($output));
Better practice would be not to merge your twp input arrays to form the $poorly_merged array. You could use iterate the second array of objects and add that data into the first -- this would be a more direct solution.

PHP sorting an array with uasort

So I sorted a multidimensional array with uasort in descending order. I did a var_dump($winrateArray) and it is sorted properly. The highest value is in the first returned array. However when I try a var_dump($winrateArray[0][3]) which is where I expect the highest value to be it isn't there. Instead it is in $winrateArray[1][3]. Am I using uasort properly?
Unsorted dump:
array(2) { [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } }
Sorted dump:
array(2) { [1]=> array(4) { [0]=> string(2) "31" [1]=> string(1) "1" [2]=> int(100) [3]=> int(101) } [0]=> array(4) { [0]=> string(2) "18" [1]=> string(1) "1" [2]=> int(0) [3]=> int(1) } }
Specific dump:
int(1)
.
$winrateArray[0][0] = '18';
$winrateArray[0][1] = '1';
$winrateArray[0][2] = 0;
$winrateArray[0][3] = 1;
$winrateArray[1][0] = '31';
$winrateArray[1][1] = '1'
$winrateArray[1][2] = 100;
$winrateArray[1][3] = 101;
var_dump($winrateArray);
function cmp($a, $b){
if ($a[3] == $b[3]){
return 0;
}
return ($a[3] < $b[3]) ? 1 : -1;
}
uasort($winrateArray, 'cmp');
var_dump($winrateArray);
var_dump($winrateArray[0][3]);
You can do:
$newArray = $winrateArray;
uasort($winrateArray, 'cmp');
$i = 0;
foreach($winrateArray as $key => $item) {
$newArray[$i][3] = $item[3];
$i++;
}
and use the new array after that. You will preserve keys and you will reorder and replace 3rd elements only

Adding an extra row to a PHP array with new values

I have this array called $items. My CMS created this array with three products and I just did a var_dump with the results below.
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
}
Is there a way I can inject another item, "ITEM_C" into this array so that it's now
array(3) { [0]=> array(11) { ["item_id"]=> string(4) "1320" ["name"]=> string(5) "ITEM_A" ["price"]=> string(6) "$5.00" }
[1]=> array(11) { ["item_id"]=> string(4) "1321" ["name"]=> string(5) "ITEM_B" ["price"]=> string(6) "$5.00" }
[2]=> array(11) { ["item_id"]=> string(4) "1323" ["name"]=> string(5) "ITEM_D" ["price"]=> string(6) "$5.00" }
[3]=> array(11) { ["item_id"]=> string(4) "1322" ["name"]=> string(5) "ITEM_C" ["price"]=> string(6) "$5.00" }
}
and then sort it by "name"? I basically want to always add one new row to this array which will always have different names and then sort it by the name. When I do the foreach php command I don't want it to be in the order of ITEM_A, ITEM_B, ITEM_D, ITEM_C. It needs to be ITEM_A, ITEM_B, ITEM_C, ITEM_D.
Appending to an array in PHP is easy as $array[] :
$myArray = array(1,2,3);
$myArray[] = 4; //now array(1,2,3,4)
So to add to your existing array, first create your new array element
$element = array("item_id" => 1322, "name" => "ITEM_C", /*etc.*/);
Then add it your array
$myArray[] = $element;
As for sorting, there are various sort functions depending on your exact needs. Since you're sorting according to a given array key, you'll probably need to call usort with a given function.
function nameCompare($a, $b)
{
$a = $a["name"];
$b = $b["name"];
return strcmp($a, $b);
}
usort($myArray, 'nameCompare');

Array search on a multidimensional array?

Here is the start of my array:
array(19) {
[0]=> array(3) {
["id"]=> string(2) "46"
["title"]=> string(7) "A"
["thumb"]=> string(68) "013de1e6ab2bfb5bf9fa7de648028a4aefea0ade816b935dd423ed1ce15818ba.jpg"
}
[1]=> array(3) {
["id"]=> string(2) "47"
["title"]=> string(7) "B"
["thumb"]=> string(68) "9df2be62d615f8a6ae9b7de36671c9907e4dadd3d9c3c5db1e21ac815cf098e6.jpg"
}
[2]=> array(3) {
["id"]=> string(2) "49"
["title"]=> string(6) "Look 7"
["thumb"]=> string(68) "0bfb2a6dd1142699ac113e4184364bdf5229517d98d0a428b62f6a72c8288dac.jpg"
}
}
How can I use array_search on this? I need to get the id of an element.
with use of === operator compared types have to be exactly same, in this code you have to search string or just use == instead ===.
function searchId($id, $array) {
foreach ($array as $key => $val) {
if ($val['id'] === $id) {
return $key;
}
}
return null;
}
$id = searchId('46', $yourarray);

Categories