I dynamically pulling results from a database and putting them in an array $myArray, which looks like below
Array (
[0] => Array (
[0] => Array (
[Fruit] => Apple
[Number] => 1
[Date] => 3117-01-41
[supplier] => Store 1
[description] => SAmple text for apple )
[1] => Array (
[Fruit] => Orange
[Number] => 1932
[Date] => 3117-01-41
[supplier] => Store 2
[description] => Sample text for Orange )
[2] => Array (
[Fruit] => Grape
[Number] => 22
[Date] => 3117-01-41
[supplier] => Store Street
[description] => Sample Text for Grape )
[3] => Array (
[Fruit] => Apple
[Number] => 23
[Date] => 3117-01-41
[supplier] => Store 9
[description] => This is text for a second apple )
[4] => Array (
[Fruit] => Apple
[Number] => 49
[Date] => 3117-01-41
[supplier] => Store 007
[description] => This is more text for some apples )
[5] => Array (
[Fruit] => Orange
[Number] => 1
[Date] => 3117-01-41
[supplier] => Store 7
[description] => This is for orange also )
)
)
The ideal would be to create a new array so the original stays untouched
$newArray = $myArray;
What i want to do is in the new array select a distinct field for example 'Fruit' it would display a distinct fruit but if other fields are different make a nested array within the new array for.
<!-- Desired output of $newArray -->
[0] => Array (
[Fruit] => Apple
[Number] => Array
(
[0] => 1
[1] => 23
[2] => 49
)
[Date] => Array
(
[0] => 3117-01-41
[1] => 3117-01-41
[2] => 3117-01-41
)
[supplier] => Array
(
[0] => Store 1
[1] => Store 9
[2] => Store 007
)
[description] => Array
(
[0] => SAmple text for apple
[1] => This is text for a second apple
[2] => This is more text for some apples
) )
[1] => Array (
[Fruit] => Orange
[Number] => Array
(
[0] => 1932
[1] => 1
)
[Date] => Array
(
[0] => 3117-01-41
[1] => 3117-01-41
[2] => 3117-01-41
)
[supplier] => Array
(
[0] => Store 2
[1] => Store 7
)
[description] => Array
(
[0] => Sample text for Orange
[1] => This is for orange also
) )
<!--Grape contents-->
First combine the array of same fruit. Now you will have same multiple fruits array under single array. Then just iterate that array & format the response.
$tempArray = [];
//creates the array of common fruit
foreach ($myArray[0] as $key => $value) {
$tempArray[$value['Fruit']][] = $value;
}
$newarray = [];
//iterate the new array of array of fruits
foreach ($tempArray as $key => $value) {
//here value will be common fruit array so iterate & format the array
$temp = [];
$temp['Fruit'] = $value[0]['Fruit'];
foreach ($value as $key => $val) {
$temp['Number'][] = $val['Number'];
$temp['Date'][] = $val['Date'];
$temp['supplier'][] = $val['supplier'];
$temp['description'][] = $val['description'];
}
$newarray [] = $temp;
}
var_dump($newarray);
Related
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
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.
This is my first array
Array
(
[0] => Array
(
[0] => 1
[1] => Elite
[2] => Air-Con Bus
[3] => Monday
)
[1] => Array
(
[0] => 4
[1] => KBZ
[2] => Airplane
[3] => Wednesday
)
[2] => Array
(
[0] => 5
[1] => Yoma
[2] => Cruise
[3] => Tuesday
)
)
I want to be inner array[0] to the outer array key. Like the following array: Can I or not? Please suggest me.
Array(
[1] => Array
(
[0] => 1
[1] => Elite
[2] => Air-Con Bus
[3] => Monday
)
[4] => Array
(
[0] => 4
[1] => KBZ
[2] => Airplane
[3] => Wednesday
)
[5] => Array
(
[0] => 5
[1] => Yoma
[2] => Cruise
[3] => Tuesday
)
)
$new_array = array();
foreach ($old_array as $el) {
$new_array[$el[0]] = $el;
}
one way:
foreach ($array as $a){
$new[$a[0]]=$a;
}
I have an array, seen below. My desired output is to group by store, and then concatenate (I think?) all of the related quantities and denominations into those arrays. I've shown what the desired output would be.
CURRENT ARRAY
Array
(
[denomination] => Array
(
[0] => 25
[1] => 50
[2] => 100
[3] => 200
)
[quantity] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 4
)
[store] => Array
(
[0] => candy store
[1] => candy store
[2] => book store
[3] => candy store
)
)
DESIRED OUTPUT
Array
(
[candy store] => Array
(
[0] => Array
(
[denomination] => Array
(
[0] => 25
[1] => 50
[2] => 200
)
)
[1] => Array
(
[quantity] => Array
(
[0] => 1
[1] => 1
[2] => 4
)
)
)
[book store] => Array
(
[0] => Array
(
[denomination] => Array
(
[0] => 100
)
)
[1] => Array
(
[quantity] => Array
(
[0] => 2
)
)
)
)
$result = array();
foreach ($array['store'] as $index => $type) {
$result[$type]['denomination'][] = $array['demoniation'][$index];
$result[$type]['quantity'][] = $array['quantity'][$index];
}
This is not exactly, what you specified as "desired output", but I don't see a reason, why one should put the denomination- and quantity-arrays into additional arrays.
However, if this has any reason, you can get it similar
$result = array();
foreach ($array['store'] as $index => $type) {
$result[$type][0]['denomination'][] = $array['demoniation'][$index];
$result[$type][1]['quantity'][] = $array['quantity'][$index];
}
I have a multidimensional array and am trying to group by a value of one the keys.
So
Array (
[0] => Array (
[name] => Edward Foo
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 10
[1] => 20
[2] => 50
)
)
[1] => Array (
[name] => Michael Max
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 10
[1] => 10
[2] => 10
)
)
[2] => Array (
[name] => Edward Foo
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 5
[1] => 10
[2] => 30
)
)
[3] => Array (
[name] => Michael Max
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 8
[1] => 8
[2] => 20
)
)
And I really need:
Array (
[0] => Array (
[name] => Edward Foo
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 15
[1] => 30
[2] => 80
)
)
[1] => Array (
[name] => Michael Max
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 18
[1] => 18
[2] => 30
)
)
)
I'm assuming the following:
every name entry in the original array has an identical desc_topic sub-array (e.g. they all have the same Apple/Banana/Orange values for every instance.
the qtd_posts sub-array has the to-be-grouped values in the same corresponding slots (e.g. all '1' entries are to be summed together, all '2' entries summed together, etc...)
You want to preserve the parent array keys so that all 'Edward Foo' entries will use the first key used by an Edward Foo entry (e.g. 0)
If that applies, then something like this should work:
$newarr = array();
$reverse_map = array();
foreach($array as $idx => $entry) {
if (isset($reverse_map[$entry['name']]) {
// have we seen this name before? retrieve its original index value
$idx = $reverse_map[$entry['name']];
} else {
// nope, new name, so store its index value
$reverse_map[$entry['name']] = $idx;
}
// copy the 'constant' values
$newarr[$idx]['name'] = $entry['name'];
$newarr[$idx]['desc_top'] = $entry['desc_topic'];
// sum the qtd_post values to whatever we previously stored.
foreach($entry['qtd_posts'] as $x => $y) {
$newarr[$idx]['qtd_posts'][$x] += $y;
}
}