I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 7 months ago.
There are many references on S/O showing various methods to flatten a multidimensional recursive array (with more than two levels). I have been through dozens (and tried most) but I'm still running into an odd problem with every one I've tried. What I am getting as a result is:
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000043
[1] => 1000045
[2] => 1000050
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000030
[1] => 1000032
[2] => 1000058
[3] => 1000064
) ...
But what I'm expecting is a truly flattened single array:
Array
[0] => 1000043
[1] => 1000045
[2] => 1000050
[3] => 1000030
[4] => 1000032
[5] => 1000058
[6] => 1000064
)
The method I found on S/O is supposed to handle an "empty array" (which I assume is the problem) but I'm still getting the wrong output. Here is my code:
function array_flatten5(array $array)
{
$flat = array(); // initialize return array
$stack = array_values($array); // initialize stack
while($stack) // process stack until done
{
$value = array_shift($stack);
if (is_array($value)) // a value to further process
{
$stack = array_merge(array_values($value), $stack);
}
else // a value to take
{
$flat[] = $value;
}
}
return $flat;
}
Could someone point out what I missing here because I'm thinking it's something simple but at this point my eyes are crossed with the number of attempts I've made. Thank you for any help you can provide.
Here is the original array. It is 4-deep:
Array ( [0] => 1000043 [1] => 1000045 [2] => 1000050 ) Array ( [0] => 1000030 [1] => 1000032 [2] => 1000058 [3] => 1000064 ) Array ( [0] => 1000041 [1] => 1000059 [2] => 1000069 ) Array ( [0] => 1000021 [1] => 1000044 [2] => 1000049 [3] => 1000071 ) Array ( [0] => 1000009 [1] => 1000013 [2] => 1000015 [3] => 1000017 [4] => 1000053 ) Array ( [0] => 1000022 [1] => 1000034 [2] => 1000070 ) Array ( [0] => 1000038 [1] => 1000047 [2] => 1000055 [3] => 1000063 ) Array ( [0] => 1000019 [1] => 1000054 [2] => 1000060 [3] => 1000066 [4] => 1000068 ) Array ( [0] => 1000006 [1] => 1000014 [2] => 1000016 [3] => 1000072 ) Array ( [0] => 1000024 [1] => 1000025 [2] => 1000046 [3] => 1000061 [4] => 1000067 ) Array ( [0] => 1000028 [1] => 1000039 [2] => 1000048 ) Array ( [0] => 1000042 [1] => 1000057 ) Array ( [0] => 1000027 [1] => 1000033 [2] => 1000036 [3] => 1000037 ) Array ( [0] => 1000008 [1] => 1000010 [2] => 1000012 [3] => 1000018 ) Array ( [0] => 1000026 [1] => 1000062 [2] => 1000065 ) Array ( [0] => 1000020 [1] => 1000023 [2] => 1000031 [3] => 1000035 [4] => 1000040 ) Array ( [0] => 1000007 [1] => 1000011 [2] => 1000029 ) Array ( [0] => 1000051 [1] => 1000052 [2] => 1000056 ) Array ( [0] => 1000001 [1] => 1000002 [2] => 1000003 [3] => 1000004 [4] => 1000005 ) Array ( [0] => 1000073 )
And here is the outcome using the array_walk_recursive suggestion ...
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
[0] => 1000111
[1] => 1000113
[2] => 1000129
[3] => 1000134
)
Array
(
)
Array
(
)
Array
(
[0] => 1000012
[1] => 1000085
)
Array
(
) ...
You didn't prepare suitable array, but looking on this code you need probably just array_walk_recursive() function.
$array = [
[1, 2, 3, 4],
[[5, 6], [7, 8]],
[[[9], [10]], [11]]
];
$result = [];
array_walk_recursive($array, function ($tempV) use (&$result) {
$result[] = $tempV;
});
print_r($result);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
)
Hi I am working on some array operations.
I need to convert first value of array as key and second value of array as value.
I have one variable $testArray which stores array like below.
Array
(
[0] => Array
(
[0] => Color
[1] => White on Red
)
[1] => Array
(
[0] => Depicted Text
[1] => EMPTY
)
[2] => Array
(
[0] => Depth [Nom]
[1] => 0.004 in
)
[3] => Array
(
[0] => Language
[1] => English
)
[4] => Array
(
[0] => Length [Nom]
[1] => 10 in
)
[5] => Array
(
[0] => Material
[1] => Adhesive Vinyl
)
[6] => Array
(
[0] => Mounting
[1] => Surface
)
[7] => Array
(
[0] => Width [Nom]
[1] => 14 in
)
[8] => Array
(
[0] => Wt.
[1] => 0.056 lb
)
)
Expected output :
Array
(
[0] => Array
(
[Color] => White on Red
)
[1] => Array
(
[Depicted Text] => EMPTY
)
[2] => Array
(
[Depth [Nom]] => 0.004 in
)
[3] => Array
(
[Language] => English
)
[4] => Array
(
[Length [Nom]] => 10 in
)
[5] => Array
(
[Material] => Adhesive Vinyl
)
[6] => Array
(
[Mounting] => Surface
)
[7] => Array
(
[Width [Nom]] => 14 in
)
[8] => Array
(
[Wt.] => 0.056 lb
)
)
I have already tried with array function array_keys and array_values but it won't working
Simple solution using array_map function:
$result = array_map(function($v){
return [$v[0] => $v[1]];
}, $testArray);
Assuming that structure will always be the same, you could do this:
$output = array();
foreach($testArray as $v){
$output[] = array($v[0] => $v[1]);
}
See it in action here.
I'm trying to combine a single-dimensional array with a multi-dimensional array. I see that there is an array_combine function and an array_merge function, but they don't seem to give me the result I need. I have the following arrays:
$days = Array (
[0] => Array (
[0] => 3
[1] => 6
)
[1] => Array (
[0] => 6
[1] => 12
)
[2] => Array (
[0] => 2
[1] => 4
)
)
$names = Array (
[0] => Joe Smith
[1] => John Doe
[2] => Jack Frost
)
and this is the result I get when using array_merge($days,$names):
$result = Array (
[0] => Array (
[0] => 3
[1] => 6
)
[1] => Array (
[0] => 6
[1] => 12
)
[2] => Array (
[0] => 2
[1] => 4
)
[3] => Joe Smith
[4] => John Doe
[5] => Jack Frost
)
How do I get the following result:
$result = Array (
[0] => Array (
[0] => John Smith
[1] => Array (
[0] => 3
[1] => 6
)
)
[1] => Array (
[0] => John Doe
[1] => Array (
[0] => 6
[1] => 12
)
)
[2] => Array (
[0] => Jack Frost
[1] => Array (
[0] => 2
[1] => 4
)
)
)
Any ideas? Thanks
Using example #4 from the docs for array_map(), here's a cool way to do it:
$result = array_map(null, $names, $days);
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.
My array looks like this:
Array
(
[0] => Array
(
[0] => 1
[1] => 6
[2] => 4
[3] => 5
)
[1] => Array
(
[0] => 272.05
[1] => 63.54
[2] => 544.79
[3] => 190.62
)
[2] => Array
(
[0] => 2011-03-06 14:08:19
[1] => 2011-03-06 14:29:04
[2] => 2011-03-06 14:28:39
[3] => 2011-03-06 14:29:28
)
)
I want to sort by $myArray[1]. I have this usort function:
function sortAmount($a, $b) {
return strnatcmp($a[1], $b[1]);
}
It is called like this:
usort($myArray, "sortAmount");
However, the array does not change after calling usort. I want the numbers in $myArray[1] to be sorted in ascending order, and for the corresponding indexes in $myArray[0] and $myArray[2] to change with it.
I think you want array_multisort:
array_multisort($a[1], $a[0], $a[2]);
gives
Array
(
[0] => Array
(
[0] => 6
[1] => 5
[2] => 1
[3] => 4
)
[1] => Array
(
[0] => 63.54
[1] => 190.62
[2] => 272.05
[3] => 544.79
)
[2] => Array
(
[0] => 2011-03-06 14:29:04
[1] => 2011-03-06 14:29:28
[2] => 2011-03-06 14:08:19
[3] => 2011-03-06 14:28:39
)
)
Apart from that, why do you use strcmp to compare numbers?