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];
}
Related
I have an array $result as such:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
[1] => Array (
[itemid] => 2
[name] => B
)
)
and an array $items as such:
[0] => Array (
[itemid] => 2
[name] => B
)
[1] => Array (
[itemid] => 4
[name] => D
)
How do I remove all items from the $result array, that occur in the $items array? In this case, the $result would become:
[0] => Array (
[0] => Array (
[itemid] => 1
[name] => A
)
)
[1] => Array (
[0] => Array (
[itemid] => 3
[name] => C
)
)
Since the question is mostly code, here's some extra characters to make StackOverflow accept the question.
I think this is what you want. (Not tested yet)
<?php
foreach ($result as $key => $array) {
$result[$key] = array_diff($array, $items);
}
print_r($result);
Hi I am working on some array operations.
I need to convert first value of array as key and second value of array as value.
I have one variable $testArray which stores array like below.
Array
(
[0] => Array
(
[0] => Color
[1] => White on Red
)
[1] => Array
(
[0] => Depicted Text
[1] => EMPTY
)
[2] => Array
(
[0] => Depth [Nom]
[1] => 0.004 in
)
[3] => Array
(
[0] => Language
[1] => English
)
[4] => Array
(
[0] => Length [Nom]
[1] => 10 in
)
[5] => Array
(
[0] => Material
[1] => Adhesive Vinyl
)
[6] => Array
(
[0] => Mounting
[1] => Surface
)
[7] => Array
(
[0] => Width [Nom]
[1] => 14 in
)
[8] => Array
(
[0] => Wt.
[1] => 0.056 lb
)
)
Expected output :
Array
(
[0] => Array
(
[Color] => White on Red
)
[1] => Array
(
[Depicted Text] => EMPTY
)
[2] => Array
(
[Depth [Nom]] => 0.004 in
)
[3] => Array
(
[Language] => English
)
[4] => Array
(
[Length [Nom]] => 10 in
)
[5] => Array
(
[Material] => Adhesive Vinyl
)
[6] => Array
(
[Mounting] => Surface
)
[7] => Array
(
[Width [Nom]] => 14 in
)
[8] => Array
(
[Wt.] => 0.056 lb
)
)
I have already tried with array function array_keys and array_values but it won't working
Simple solution using array_map function:
$result = array_map(function($v){
return [$v[0] => $v[1]];
}, $testArray);
Assuming that structure will always be the same, you could do this:
$output = array();
foreach($testArray as $v){
$output[] = array($v[0] => $v[1]);
}
See it in action here.
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.
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;
}
}
i have a multi dimension array with sub array having repeated values of 'eduHisRowId' like:
Array
(
[0] => Array
(
[eduHisRowId] => 4
[repOrderId] => 15
)
[1] => Array
(
[eduHisRowId] => 5
[repOrderId] => 16
)
[2] => Array
(
[eduHisRowId] => 5
[repOrderId] => 17
)
[3] => Array
(
[eduHisRowId] => 6
[repOrderId] => 18
)
[4] => Array
(
[eduHisRowId] => 7
[repOrderId] => 19
)
[5] => Array
(
[eduHisRowId] => 7
[repOrderId] => 20
)
[6] => Array
(
[eduHisRowId] => 8
[repOrderId] => 21
)
)
Now i want sort out these repeated values such that i could be able to check that the record present on index '[1] => Array' is associated with the record which is present on index '[2] => Array' & this associated relation will also be in array format like:
Array
(
[0] => Array
(
[0] => 4
[1] => Array
(
[0] => 15
)
)
[1] => Array
(
[0] => 15
[1] => Array
(
[0] => 16
[0] => 17
)
)
[2] => Array
(
[0] => 6
[1] => Array
(
[0] => 18
)
)
[3] => Array
(
[0] => 7
[1] => Array
(
[0] => 19
[0] => 20
)
)
[4] => Array
(
[0] => 8
[1] => Array
(
[0] => 21
)
)
)
where 0th index of innre mos array will contain 'eduHisRowId' value & the array on 1st index will contain 'repOrderId' values.
Thanks in advance...
Can I suggest a different solution? What about an array structure that looks like:
Array
(
[4] => Array
(
[0] => 15
)
[5] => Array
(
[0] => 16
[1] => 17
)
)
The keys are the eduHisRowId values and the value is an array of corresponding repOrderId values.
Creating this array would go like follows:
function consolidate($item, $key, $array) {
$rowId = $item['eduHisRowId'];
if(!array_key_exists($rowId, $array)) {
$array[$rowId] = array();
}
$array[$rowId][] = $item['repOrderId'];
}
$result = array();
array_walk($dataArray, 'consolidate', &$result);
$dataArray is your multidimensional array, the resulting array is in $result.
Reference: array_walk(), array_key_exists()