I've been trying this all day!
How would I convert the top multidimensional array into the bottom.
Array (
[0] => Array ( [id] => 34 [email] => a#example.com )
[1] => Array ( [id] => 34 [email] => b#example.com )
[2] => Array ( [id] => 33 [email] => c#example.com )
[3] => Array ( [id] => 33 [email] => d#example.com )
[4] => Array ( [id] => 33 [email] => e#example.com )
)
Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> a#example.com [1]=>b#example.com )
[1]=>Array ([id] => 33 [email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Many thanks.
$new_array = array();
foreach ($orig_array as $child) {
$new_array[$child['id']][] = $child['email'];
}
$final_array = array();
foreach($new_array as $child) {
$final_array[] = $child;
}
The first loop produces an array keyed off the id fields, and simply pushes each email address onto it. The second loop then takes that intermediate array and wraps another array around it for the 0,1,etc... keys.
Would not just using keys in order to store IDs be an easier way to do that? Like this:
Array (
[34]=>Array ([email] => Array ([0]=> a#example.com [1]=>b#example.com )
[33]=>Array ([email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Then grouping emails would become a trivial task.
Related
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)
I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.
You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);
You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);
So I got an array like:
Array
(
[0] => Array
(
[ids] => Array
(
[id] => id1
)
[name] => name1
[number] => 1
)
[1] => Array
(
[ids] => Array
(
[id] => id2
)
[name] => name2
[number] => 2
)
)
And I want to construct new multidimensional array based on the elements of it, but adding some new keys with empty values like(all the keys will have other names in new array, it's just simplified):
Array
(
[0] => Array
(
[id] => id1
[firstname] => name1
[lastname] =>
[somedata] =>
[somemoredata] =>
[ordernumber] => 1
)
[1] => Array
(
[id] => id2
[firstname] => name2
[lastname] =>
[somedata] =>
[somemoredata] =>
[ordernumber] => 2
)
)
How do I do it? Was thinking about array_push inside foreach loop, but it's not gonna do the job because of the empty keys I want and different order of elements. I also know how to access the nested value of [id] but still no idea about how to construct and move values to the new array for each element.
You can do it like below:-
$final_array = array();
foreach($array as $arr){
$final_array[] = array('id'=>$arr['ids']['id'],'firstname'=>$arr['name'],'lastname'=>'','somedata'=>'','somemoredata'=>'','ordernumber'=>$arr['number']);
}
print_r($final_array);
Output:-https://eval.in/831090
There is an array which is built looks like the following (with some more values which i left away for this example):
Array
(
[0] => Array
(
[id] => 44
[cars] => Array
(
[0] => Array
(
[id] => 38
)
[1] Array
(
[id] => 39
)
)
)
[1] => Array
(
[id] => 45
[cars] => Array
(
[0] => Array
(
[id] =>136
)
[1] =>Array
(
[id] =>137
)
[2] =>Array
(
[id] =>138
)
)
)
)
I want to build another array from the above in the following form:
Array
(
[0] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 38
)
[1] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 39
)
[2] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 136
)
[3] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 137
)
[4] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 138
)
)
I tried to achieve this with following function:
foreach($filterSortSaveArray as $filterSortSaveArray['cars'] => $value){
$id = $filterSortSaveArray['id'];
foreach($value['cars'] as $value => $car){
$field_values['car_filter_sort_id'] = $id;
$field_values['car_id'] = $car['id'];
}
}
But the the result differs from what I have expected. Any suggestions?
There are two big issues in your code. First, are you referencing undefined value with $filterSortSaveArray['cars'], since there is no 'cars' key in the first level of the original array. Second, by assigning values to $field_values['car_filter_sort_id'] and $field_values['car_id'] in the loop you are just overriding them in each iteration. You need to push the values into an array using []= operator (which is equivalent to applying array_push()).
Try this:
$result = [];
foreach($filterSortSaveArray as $k => $v) {
if (!is_array($v['cars']))
continue;
$id = $v['id'];
foreach ($v['cars'] as $i => $car){
$result[] = [
'car_filter_sort_id' => $id,
'car_id' => $car['id']
];
}
}
I have an array $people. When I do print_r($people), I get the following results:
[people] => Array
(
[500] => Array
(
[firstName] => Fred
[age] => 19
)
[501] => Array
(
[firstName] => Bob
[age] => 12
)
[502] => Array
(
[firstName] => Steve
[age] => 52
)
)
I want to change all the keys to look more "normal", starting at 0, then 1, 2 etc. How can I achieve this? To clarify, I want the resulting array to look like this:
[people] => Array
(
[0] => Array
(
[firstName] => Fred
[age] => 19
)
[1] => Array
(
[firstName] => Bob
[age] => 12
)
[2] => Array
(
[firstName] => Steve
[age] => 52
)
)
The built-in function array_values() will take only the values from an array, ignoring the keys and instead returning the array renumbered from zero.
$people = array_values($people);
Simply like this:
foreach ($people as $value) {
$people_new[] = $value;
}
Try this
$people['people'] = array_values($people['people']);
print_r($people);