If I have an array which looks something like this:
Array
(
[0] => Array
(
[DATA] => Array
(
VALUE1 = 1
VALUE2 = 2
)
)
[1] => Array
(
[DATA] => Array
(
VALUE3 = 3
VALUE4 = 4
)
)
)
And would like to turn it into this:
Array
(
[0] => Array
(
[DATA] => Array
(
VALUE1 = 1
VALUE2 = 2
VALUE3 = 3
VALUE4 = 4
)
)
)
I basically want to merge all the identical keys which are at the same level.
What would be the best route to accomplish this?
Could the array_merge functions be of any use?
I Hope this makes any sort of sense and thanks in advance for any help i can get.
You can use array_merge_recursive to merge all the items in your original array together. And since that function takes a variable number of arguments, making it unwieldy when this number is unknown at compile time, you can use call_user_func_array for extra convenience:
$result = call_user_func_array('array_merge_recursive', $array);
The result will have the "top level" of your input pruned off (logical, since you are merging multiple items into one) but will keep all of the remaining structure.
See it in action.
Related
I have array came from my ajax
$contestant_name_arr = $_GET['contestant_name_arr'];
print_r($contestant_name_arr);
Whenever i try to get the value of each in loop i got error because instead of this
Array ( [0] => value1,value2 )
It should be look like this:
Array (
[0] => value1
[1] => value2
)
How do I separate like that in the example above.
Either devise your url query string to be:
http://yourhost.com?contestant_name_arr[0]=value&contestant_name_arr[1]=value2
Or just simply explode;
$contestant_name_arr = explode(',', $contestant_name_arr[0]);
I am trying to compare two different arrays and get the values that do not exist in 1 of the arrays. Here are my 2 arrays:
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52)
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52 [3] => 583cee91-1913-4e9d-b51d-e27083420001)
As you can see the second array has an additional value. I am trying to user array_diff like this:
$result = array_diff($array1,$array2);
print_r($result);
However the out of the array_diff is:
array()
Any ideas what is going on?
As people have suggested and i have already tested switching the arrays around, this is the output:
Array ( [0] => [1] => )
array_diff gives you the values from $array1 that are not in the other arrays. All the values of your first array are in the second. Sou change the order of your arrays and you should be fine.
See also here: http://php.net/manual/de/function.array-diff.php
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays2
Read array_diff
$result = array_diff($array2,$array1);
Try like this
I have 2 api end points that load JSON data...
1. subject matter experts
Array
(
[0] => Array
(
[ExpertiseId] => 1
[IndustryId] => 1
[PersonId] => 3
)
...
)
people database
Array
(
[0] => Array
(
[Id] => 1
[Name] => Joe
[Office] => New York
)
....
)
I'd like to pass both functions into an array, specify to merge on [matter.PersonId] => [people.Id] so the returned array would become
Array
(
[0] => Array
(
[ExpertiseId] => 1
[IndustryId] => 1
[PersonId] => 3
[Id] => 1
[Name] => Joe
[Office] => New York
)
...
)
You have to iterate over both arrays and reorganize the data in such a way that merging can happen. In practice that means rekeying the first array by PersonId and the second by Id; this is very easy to do with array_column:
$matter = array_column($matter, null, 'PersonId');
$people = array_column($people, null, 'Id');
At this point only a simple task is left: merging the items (arrays) that share the same key in both $matter and $people.
In a perfect world that would be an one-liner with array_merge_recursive, but that function does not actually merge arrays that have integer keys like these (the ids we used as keys are integers). So a little standard iteration is in order: pick one of the arrays and merge its contents with the other:
foreach ($people as $id => $data) {
// If there's a guarantee that both arrays will definitely have the same
// set of keys (ids) so that $matter[$id] is guaranteed to exist,
// you can also use the simpler: $matter[$id] += $data
$matter[$id] = isset($matter[id]) ? $matter[$id] + $data : $data;
}
...and now $matter has all the data merged together.
Try array_merge()
$newArray = array_merge(first_array, second_array);
I'm a bit fuzzy on how to work with multidimensional arrays in Ruby. How would I recreate this PHP code in Ruby?
$objs_array = array();
foreach($objs AS $obj) {
$objs_array[$obj->group_id][] = $obj;
}
}
print_r($objs_array);
The result would be:
Array
(
[123] => Array
(
[0] => Array
(
object1
)
[1] => Array
(
object2
)
)
[456] => Array
(
[0] => Array
(
object3
)
[1] => Array
(
object4
)
)
)
Thanks.
More than a multidimensional array, hash of arrays would fit better.
In php you only have the type array, but in ruby the class Hash is very useful
objs_hash = {}
objs.each do |obj|
objs_hash[obj.id] = obj
end
Thank you rhernado for sending me down the road of looking into hashes. I ended up finding a similar question that pointed out the group_by method:
Building Hash by grouping array of objects based on a property of the items - I ended up using a hash of an array of objects by using:
groups = objs.group_by { |obj| obj.group_id }
I have two arrays: Array ( [0] => 2 [1] => 3 ) and Array ( [0] => 2 ).
I want to get the value, which is not in second array. So I have used the array_diff function but my result will get Array ( [1] => 3 )
Actually this is the result. But a small problem here, its position is (key) 1. I want the result in to a new array starts from 0th position, i.e., Array ( [0] => 3 ).
How can I achieve this?
you can use array_values(array_diff($arr1, $arr2)); if order doesn't matter
You should run array_values() on the result and this would give you a new array with indexes starting at 0.
This is a known shortcoming of array_diff(), check the php docs.