php merging two arrays into one - php

I have two arrays that need to be merged together and trying to figure out the correct way of doing it.
this is the first array
Array
(
[IndividualOutmsg] => Array
(
[0] => Array
(
[user_id] => 3
[number] => 414566765
[msg] => some message
)
[1] => Array
(
[user_id] => 3
[number] => 410335509
[msg] => any message
)
)
)
this is the second array:
Array
(
[0] => Array
(
[0] => OK
[1] => 0
[2] => d142b46128b869d0
[3] => 6178977058476937
)
[1] => Array
(
[0] => OK
[1] => 0
[2] => 60f403f4e243e684
[3] => 6198708709873543
)
)
what i want to get is this:
Array
(
[IndividualOutmsg] => Array
(
[0] => Array
(
[user_id] => 3
[number] => 414566765
[msg] => some message
[sms_status] => OK
[error_code] => 0
[msg_id] => d142b46128b869d0
[msg_id_2] => 6178977058476937
)
[1] => Array
(
[user_id] => 3
[number] => 410335509
[msg] => any message
[sms_status] => OK
[error_code] => 0
[msg_id] => 60f403f4e243e684
[msg_id_2] => 6198708709873543
)
)
)

In that format, you really have to do a lot of the legwork yourself and can't just use array_merge to combine the arrays. It would have to be a more custom job, like so:
$count = count($second_array);
for($i=0; $i<$count; $i++){
$first_array['IndividualOutmsg'][$i]['sms_status'] = $second_array[0];
$first_array['IndividualOutmsg'][$i]['error_code'] = $second_array[1];
$first_array['IndividualOutmsg'][$i]['msg_id'] = $second_array[2];
$first_array['IndividualOutmsg'][$i]['msg_id2'] = $second_array[3];
}
If you were to output the second array with the associative keys set, it would be much easier to combine them using array_merge, provided the keys didn't conflict.
$count = count($second_array);
for($i=0; $i<$count; $i++){
$first_array['IndividualOutmsg'][$i] =
array_merge($first_array['IndividualOutmsg'][$i], $second_array[$i]);
}

http://au.php.net/manual/en/function.array-merge.php
Array merge might be what you're looking for...
Though you'll need to probably write a loop or function that can get to the right place in your multi-dimensional array, perform the merge and also change the relevant keys.

Related

How to merge three arrays according to common key in php

I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Rohit
)
[1] => stdClass Object
(
[id] => 2
[name] => Emop1
)
[2] => stdClass Object
(
[id] => 3
[name] => Pankaj
)
[3] => stdClass Object
(
[id] => 4
[name] => tejpal singh
)
)
second array $q1 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[mcollecton] => 100
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[mcollecton] => 1222
)
)
third array $q2 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[dcollecton] => 300
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[dcollecton] => 150
)
)
so far what I have tried
$new_array = array();
foreach($ids as $k) {
$q1n = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1) {
if($k->id==$k1->id){
$mc = array("mc"=>$k1->mcollecton);
array_merge($q1n,$mc);
}
}
foreach($q2 as $k1){
if($k->id==$k1->id){
$dc = array("dc"=>$k1->dcollecton);
array_merge($q1n,$dc);
}
}
$a = array_merge($q1n,$mc);
$av = array_merge($q1n,$dc);
array_push($new_array,$q1n);
}
but the output is coming as
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
)
[1] => Array
(
[id] => 2
[name] => Emop1
)
[2] => Array
(
[id] => 3
[name] => Pankaj
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
)
)
I want the output be like
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
[mcollection] => 100
[dcollection] => 300
)
[1] => Array
(
[id] => 2
[name] => Emop1
[mcollection] => 1222
[dcollection] => 150
)
[2] => Array
(
[id] => 3
[name] => Pankaj
[mcollection] => 0
[dcollection] => 0
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
[mcollection] => 0
[dcollection] => 0
)
)
So I have tried many times but the desired output is not coming . please help me out how to get the desired output.
It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.
There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.
foreach($first as $key1 => $value){
foreach($second as $key2 => $value2){
// match the ids and check if array key exists on first array
if($value['id'] === $value2['id'] && empty($first[$key2])){
$first[$key][$key2] = $value2;
}
}
}
EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.
Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:
foreach($ids as $k)
{
$thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1)
{
if($k->id == $k1->id && !empty($k1->mcollecton))
{
$thisArray['mc'] = $k1->mcollecton;
}
}
foreach($q2 as $k2)
{
if($k->id == $k2->id && !empty($k2->dcollecton))
{
$thisArray['dc'] = $k2->dcollecton;
}
}
}
// This should have both new collections fields on all array items
print_r($newArray)

Find if two different keys have the same value in 2 arrays PHP

My arrays are:
Array1
(
[0] => Array
(
[id] => 2
[name] => Melamine
[deleted] => 0
)
[1] => Array
(
[id] => 4
[name] => Vinyl
[deleted] => 0
)
[2] => Array
(
[id] => 5
[name] => Polyu
[deleted] => 0
)
)
Array2
(
[0] => Array
(
[productFinish] => 29
[type] => 2
)
[1] => Array
(
[productFinish] => 29
[type] => 4
)
)
So, i would like to return first array if id of 1st array matches with type of another array. In this case, first 2 indexes of first array must come out in return.
Thanks
You can use array_uintersect to get the results you want, supplying a callback function that compares the id value in array1 with the type value in array2:
$result = array_uintersect($array1, $array2, function ($a1, $a2) {
return ($a1['id'] ?? $a1['type']) - ($a2['type'] ?? $a2['id']);
});
print_r($result);
Note that because the callback is also called with values exclusively from $array1 or $array2 (for sorting), we have to allow for that in the comparison expression.
Output:
Array
(
[0] => Array
(
[id] => 2
[name] => Melamine
[deleted] => 0
)
[1] => Array
(
[id] => 4
[name] => Vinyl
[deleted] => 0
)
)
Demo on 3v4l.org
Ok, i got it with for loop.
$newTypeFilter = [];
for($i=0; $i < count($arra1); $i++){
for($j=0;$j<count($arra2); $j++){
if($arra1[$i]['id'] == $arra2[$j]['type']){
$newTypeFilter[] = $arra1[$i];
}
}
}
Any other answers will be appreciated. Thanks

php formatting array results

I have an array like the following. This is the results of a query on one of our servers.
Array
(
[count] => 1
[0] => Array
(
[name] => Array
(
[count] => 1
[0] => mac
)
[0] => name
[staffid] => Array
(
[count] => 1
[0] => 1234
)
[1] => staffid
[school] => Array
(
[count] => 1
[0] => western
)
[2] => school
[count] => 3
[dn] => cn=mac,cn=staff
)
)
How do I loop through this array and create a new array as follows.
Array
(
[name] => mac
[staffid] => 1234
[school] => western
)
I've tried a foreach loop echoing the key & values, but I'm not sure where to go from there. There will be more results returned as the query is expanded, but original array layout will be the same and the new layout needs to be the same format.
Any ideas ?
Thanks
Try this:
$result = array();
foreach($yourArray as $element){
for($i=0;$i<$element['count']; $i++){
unset($element[$element[$i]]['count']);
$result[$element[$i]] = implode(', ', $element[$element[$i]]);
}
}

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);

Merge a 3D Array with 2D Array Based on Shared Values

I need to merge a three-dimensional array with a two-dimensional array based on a shared value for 'id.'
In the example below, "George Washington" has an 'id' of 1. I need to append his 'value', which is found in the second array where 'id' is also 1.
The 'id' of "John Adams" is 2, which is not found in the second array. As a result, his 'value' needs to be set to 0 (or NULL).
The final result is a three-dimension array where 'value' has been added to each item in the original array.
Array #1
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
)
)
Array #2
Array
(
[0] => Array
(
[id] => 1
[value] => 12
)
[1] => Array
(
[id] => 5
[value] => 18
)
)
Desired Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => "George Washington"
)
[total] => 8
[average] => 2.5
[value] => 12
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => "John Adams"
)
[total] => 6
[average] => 3.0
[value] => 0
)
[2] => Array
(
[0] => Array
(
[id] => 5
[name] => "James Monroe"
)
[total] => 9
[average] => 2.0
[value] => 18
)
)
What I've tried so far:
I separated all of the 'id' values from the first array into a new array named $ids. Then while looping through the items in the second array, I check to see whether the 'id' from the second array is found in the $ids array.
But then I'm stuck because I don't know how to specify which item in the first array needs to receive the new 'value'. Also, this seems like a messy solution because of the overhead involved in creating the $ids array.
Didn't even realize you had tried to list the IDs and such before I was done writing this, but here you go anyway. Good luck!
$array1_size = count($array1);
$array2_size = count($array2);
// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
$id_list[] = $array1[$i][0]['id'];
$array1[$i]['value'] = NULL; // Adds a NULL to all value fields
}
// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
if (in_array($array2[$i]['id'], $id_list)) { // Checks if each ID exists in the ID list
$key = array_search($array2[$i]['id'], $id_list); // Gets the key of the matching ID
$array1[$key]['value'] = $array2[$i]['value']; // Adds the value
}
}
Edit: Values are now set to NULL by default and thus only gets changed later on if needed.

Categories