I have a serialized array stored in a cookie, at one point in my application I need to merge this array with another array with the same design, so:
Array 1:
$arr1 = array(
"user_id" => 1,
"item_id" => 2,
"quant" => 3
);
I set this in a cookie with:
serialize($arr1);
Array 2:
$arr2 = array(
"user_id" => 5,
"item_id" => 5,
"quant" => 6
);
My attempt at merging the two arrays:
First I unserialize the first array value that is in my cookie, followed by a merge with the second array
$un_arr1 = unserialize($cookie_val);
$final = array_merge($arr2, $un_arr1);
This is returning me only the values from the first array though.
What I am trying to achieve:
Array
(
[0] => Array
(
[user_id] => 1
[item_id] => 2
[quant] => 3
)
[1] => Array
(
[user_id] => 4
[item_id] => 5
[quant] => 6
)
)
$final = array($array1, $array2);
You are looking for array_merge_recursive instead of array_merge.
<?php
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$result = array_merge_recursive($A, $B);
print_r($result);
See it in action.
Related
I have multiple arrays structured like these:
$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];
meaning:
every array has the same value for each key (eg: array1 has value "1" for every item in the array) but there are no arrays sharing the same value (eg: if array1 has value 1, then none of the other arrays has value = 1)
the arrays may or may not share the same keys
I need to combine these arrays in a way that the final result is something like this:
$result = [
"aaa" => [1,12],
"bbb" => [1,12,15],
"ccc" => [15],
];
meaning:
the final array must contain all the keys from the previous arrays
the value of the key is an array composed of all the values of the previous arrays that shared the same key
I know it's a bit messy, but I hope it is clear enough. I'm struggling to build the $result array. I tried merge, combine, intersect, but none of them seems to work. Is there a way to build the $result array without using a loop?
Thanks
Does it match your goal ?
<?php
$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];
$array = array_merge_recursive($array1, $array2, $array3);
print_r($array);
?>
outputs
Array
(
[aaa] => Array
(
[0] => 1
[1] => 12
)
[bbb] => Array
(
[0] => 1
[1] => 12
[2] => 15
)
[ccc] => 15
)
Merge all of array into an mergedArray. Then use 2 foreach to set it.
<?php
$array1 = ["aaa" => 1, "bbb" => 1];
$array2 = ["aaa" => 12, "bbb" => 12];
$array3 = ["bbb" => 15, "ccc" => 15];
$mergedArray = [$array1, $array2, $array3];
$result = [];
foreach ($mergedArray as $array) {
foreach ($array as $key => $item) {
$result[$key][] = $item;
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
exit;
?>
The result:
Array
(
[aaa] => Array
(
[0] => 1
[1] => 12
)
[bbb] => Array
(
[0] => 1
[1] => 12
[2] => 15
)
[ccc] => Array
(
[0] => 15
)
)
I have a page that has 4 widgets each getting data from the database. Each widgets gets data in different formats since i have a table widget, a statistics widget and a widget with just one value.
Supposing i have the results of queries returning data say in this format
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$test1 = array(
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44'
);
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
how can i pass the arrays as json encoded as one but retains their array structure on the client side for easier and faster creating on widget views?.
I have looked at array_merge and according to the docs, that's not what i am looking for.
how can i pass the arrays as json encoded as one but retains their array structure on the client side for easier and faster creating on widget views?.
You might arrange each array to be json encoded together like so:
$bundle = json_encode([$arr1, $arr2, $test1, $array1, $array2]);
Result:
[[1,3,5],[2,4,6],{"11":"11","22":"22","33":"33","44":"44"},{"0":"zero_a","2":"two_a","3":"three_a"},{"1":"one_b","3":"three_b","4":"four_b"}]
If I understood your issue correctly, you can assign your arrays to a multidimensional array.
Based in your example:
$dataArray = [];
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$dataArray['data_type_one'][] = $arr1;
$dataArray['data_type_one'][] = $arr2;
$test1 = array(
'11' => '11',
'22' => '22',
'33' => '33',
'44' => '44'
);
$dataArray['data_type_two'] = $test;
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$dataArray['data_type_three'][] = $array1;
$dataArray['data_type_three'][] = $array2;
You will end up with this array structure:
Array
(
[data_type_one] => Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 5
)
[1] => Array
(
[0] => 2
[1] => 4
[2] => 6
)
)
[data_type_two] => Array
(
[11] => 11
[22] => 22
[33] => 33
[44] => 44
)
[data_type_three] => Array
(
[0] => Array
(
[0] => zero_a
[2] => two_a
[3] => three_a
)
[1] => Array
(
[1] => one_b
[3] => three_b
[4] => four_b
)
)
)
Then, in your views, you can do whatever you need:
foreach ($arrayData['data_type_one'] as $someData) {
echo $someData[0] . ' ';
//will print 1 2
}
echo $dataArray['data_type_two']['11'];
//will print 11
I have two arrays:
$array1 = array(299945 => [13654 => [9917 => [0 => '0', 9 => '9', 33 => '33']]]);
$array2 = array(13654 => [9940 => [0 => '0']]);
Each of these are created from different DBQueries which create these results.
I know want to take the '9940' key from $array2 and push it into $array1 so that it will be another element in the 13654 array. Thus the final result would be:
$array1 = array(299945 =>[13654 => [9917 => [0 => '0', 9 => '9', 33 => '33'], 9940 => [0 => '0']]])
How can I do this?
There's a few ways of doing this, here's one that uses array_replace_recursive():
<?php
header('Content-type: text/plain');
$array1 = array(299945 => [13654 => [9917 => [0 => '0', 9 => '9', 33 => '33']]]);
$array2 = array(13654 => [9940 => [0 => '0']]);
$array3 = array_replace_recursive($array1, [key($array1) => $array2]);
print_r($array3);
Output:
Array
(
[299945] => Array
(
[13654] => Array
(
[9917] => Array
(
[0] => 0
[9] => 9
[33] => 33
)
[9940] => Array
(
[0] => 0
)
)
)
)
If you just want a union of two arrays, there is not much to it:
$array1 += $array2
You should probably think about more complex situation with duplicate keys and issues like that, so I usually find array_merge to be the better tool for adding two arrays together.
I have this code
$array = array('o' => 'one', 't' => 'three', 'f' => 'four');
array_push($array, array('s' => 'six'));
print_r($array);
that insert new key and value into the array , but when i print the new $array it returned this
Array ( [o] => one [t] => three [f] => four [0] => Array ( [s] => six ) )
i need to return like this
Array ( [o] => one [t] => three [f] => four [s] => six )
how to remove [0] => Array () from the array ?
array_push is intended for lists.
$arr = array(5, 6, 7);
array_push($arr, 8); // array(5, 6, 7, 8);
You can add elements to arrays in many ways, this is one:
$array = array('o' => 'one', 't' => 'three', 'f' => 'four');
$array["s"] = "six";
Here is another:
$array = array_merge($array, array("s" => "six"));
PHP treats lists like array(1, 2, 3); differently than associative arrays like array("foo" => "bar");. The differences are minor, but they show up with functions like array_push.
I have two arrays, I want to merge these two arrays into single array. Please view the detail below:
First Array:
Array
(
[0] => Array
(
[a] => 1
[b] => 2
[c] => 3
)
[1] => Array
(
[a] => 3
[b] => 2
[c] => 1
)
)
Second Array:
Array
(
[0] => Array
(
[d] => 4
[e] => 5
[f] => 6
)
[1] => Array
(
[d] => 6
[e] => 5
[f] => 4
)
)
I want this result. Does somebody know how to do this?
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
[2] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[3] => Array
(
[0] => 6
[1] => 5
[2] => 4
)
)
Hope you have understand the question.
Thank you in advance.
Try array_merge:
$result = array_merge($array1, $array2);
FIXED (again)
function array_merge_to_indexed () {
$result = array();
foreach (func_get_args() as $arg) {
foreach ($arg as $innerArr) {
$result[] = array_values($innerArr);
}
}
return $result;
}
Accepts an unlimited number of input arrays, merges all sub arrays into one container as indexed arrays, and returns the result.
EDIT 03/2014: Improved readability and efficiency
more simple and modern way is:
$merged = $array1 + ['apple' => 10, 'orange' => 20] + ['cherry' => 12, 'grape' => 32];
new array syntax from php 5.4
If you want to return the exact result you specify in your question then something like this will work
function array_merge_no_keys() {
$result = array();
$arrays = func_get_args();
foreach( $arrays as $array ) {
if( is_array( $array ) ) {
foreach( $array as $subArray ) {
$result[] = array_values( $subArray );
}
}
}
return $result;
}
As a purely native function solution, merge the arrays, then reindex each subarray.
Code: (Demo)
$a = [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 3, 'b' => 2, 'c' => 1],
];
$b = [
['d' => 4, 'e' => 5, 'f' => 6],
['d' => 6, 'e' => 5, 'f' => 4],
];
var_export(
array_map('array_values' array_merge($a, $b))
);
Output:
array (
0 =>
array (
0 => 1,
1 => 2,
2 => 3,
),
1 =>
array (
0 => 3,
1 => 2,
2 => 1,
),
2 =>
array (
0 => 4,
1 => 5,
2 => 6,
),
3 =>
array (
0 => 6,
1 => 5,
2 => 4,
),
)