I am trying to sort a multidimensional array based on 1 field so when I run a foreach loop to loop through it will be sorted in the right order...
I have tried several sources but found very few results on multi-dimensional array sorting...
When I run the loop on the below array it shows id field order as 1|2 i want it to be 2|1 what would be the best way to achieve this?
Basically just sort it in descending order...
Array:
array(2) {
[0]=> array(1) {
[0]=> array(4) {
[0]=> string(1) "1"
["id"]=> string(1) "1"
[1]=> string(0) ""
["invoicenum"]=> string(0) ""
}
}
[1]=> array(1) {
[0]=> array(4) {
[0]=> string(1) "2"
["id"]=> string(1) "2"
[1]=> string(0) ""
["invoicenum"]=> string(0) ""
}
}
}
Have you try this?
array_multisort
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$mid[$key] = $row['id'];
}
// Use $data to sort by the common key
array_multisort($mid, SORT_DESC, $data);
?>
Related
I am trying to loop through array of arrays in php. Usually get stalked with complex array sometimes but I need your kind assistance with this.
var_dump($array) produced the array below:
$arrayVal = array(6) {
["item_id"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
["request_explanation"]=>
array(2) {
[0]=>
string(7) "Welcome"
[1]=>
string(11) "Hello World"
}
["quantity"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "4"
}
["unit_cost"]=>
array(2) {
[0]=>
string(1) "4"
[1]=>
string(1) "3"
}
["total_cost"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["supporting_document"]=>
string(0) ""
}
My database table:
I want to be able to save each of the value in that array into the table above. Thanks for helping me.
Use the indexes of one of the sub-arrays to access all the other sub-arrays:
foreach ($array['item_id'] as $i => $item_id) {
$request_explanation = $array['request_explanation'][$i];
$quantity = $array['quantity'][$i];
// repeat this for all the columns
// Now you can insert all these variables into the database
}
Use a loop to build 2 separate arrays:
foreach($array['ExpensesList'] as $index => $val){
$array1[$index] = $array['ExpensesList'][$index][0];
$array2[$index] = $array['ExpensesList'][$index][1];
}
Then insert each array into your database individually.
This will not work if any sub array contains an index at 2, so this is explicitly for the example structure you provided.
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.
I have two arrays, I would like to compare.
array1:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "dog"
["name"]=>
string(4) "alan"
}
["order"]=>
string(2) "12"
}
array2:
array(4) {
["123"]=>
array(5) {
["animal"]=>
string(2) "cat"
["name"]=>
string(4) "fred"
}
["345"]=>
array(5) {
["animal"]=>
string(3) "fox"
["name"]=>
string(4) "tom"
}
["order"]=>
string(2) "12"
}
I compare them with array_diff:
$result = array_diff($array1, $array2);
But if I var_dump $result, I get the following output:
array(0) {
}
Does anyone have an idea why?
For associative arrays you should use array_diff_assoc. Also see the user contributed notes for how to do this recursively, if you need to.
With the help of sinaza I found out that no difference was displayed, because array_diff works different with multidimensional arrays.
Here is the code, that worked for me:
foreach ($array1 as $k1 => $v1) {
if (array_diff($array2[$k1], $array1[$k1])){
$result[$k1] = array_diff($array2[$k1], $array1[$k1]);
}
}
Good day.
Code:
array(4) {
[0]=> array(1) {
[0]=> array(3) {
[0]=> string(11) "art_7880" [1]=> string(1) "1" [2]=> int(2950)
}
[1]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(2955)
}
[2]=> array(3) {
[0]=> string(8) "art_7880" [1]=> string(1) "1" [2]=> int(1335)
}
[3]=> array(3) {
[0]=> string(8) "art_7883" [1]=> string(1) "1" [2]=> int(4335)
}
}
I get array unique elements:
$arr_uniq = array();
foreach ($all_array as $keys => $elms ) {
if(!in_array($elms[0], $arr_uniq)) {
$arr_uniq[] = $elms[0];
}
}
Tell me pleasse how to get a count each unique element in the general array?
result should been next:
art_7880 - 3
art_7883 - 1
Assuming $all_array is subarray of your main array in your var_dump snipett, the general idea is
$result = array();
foreach ($all_array as $elms)
$result[$elms[0]]++;
array_count_values()
http://php.net/array_count_values
You should be able to easily apply this function.
i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}