Shifting array Values PHP - php

Please Help me. I have And Following array :
[0] => Array (
[0] => Name
[1] => Age
)
[1] => Array (
[0] => Name 1
[1] => 20
)
[2] => Array (
[0] => Name 2
[1] => 21
)
[3] => Array (
[0] => Name 3
[1] => 22
)
[4] => Array (
[0] => Name 4
[1] => 23
)
[5] => Array (
[0] => Name 5
[1] => 24
)
I want to achieve shift the values of array like this output:
Because when exporting excel file into php database values will insert incorrectly with shifting of 1 row of age:
[0] => Array (
[0] => Name
[1] => 20
)
[1] => Array (
[0] => Name 1
[1] => 21
)
[2] => Array (
[0] => Name 2
[1] => 22
)
[3] => Array (
[0] => Name 3
[1] => 23
)
[4] => Array (
[0] => Name 4
[1] => 24
)
[5] => Array (
[0] => Name 5
[1] => 25
)
Thanks In Advance.

You can also loop then check for the last array key
$array = [
["Name","Age"], ["Name 1","20"], ["Name 2","23"], ["Name 3","30"],
["Name 4","20"], ["Name 5","26"], ["Name 6","27"], ["Name 7","21"], ["Name 8","26"]
];
// Get last array key using count() or you can use end() then key()
$last_key = count($array) - 1;
$result = []; // initialize result array
foreach ($array as $key => $value) {
if ($key == $last_key) break; // if last key, break loop
$result[] = [ $array[$key][0], $array[++$key][1] ]; // push name, age+1 values
}
print_r($result);

Related

PHP merge and reorder multidimensional array

I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));

PHP Merge array with same keys and one same value

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.

Output arrays within an array grouped by a common index

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];
}

How to SUM and GROUP BY a multidimensional ARRAY by a key?

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;
}
}

Find value assoaction in array

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()

Categories