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()
Related
This question already has answers here:
Map/Merge data from a flat array into the rows of a 2d array [duplicate]
(5 answers)
Closed 5 months ago.
I have two arrays. One is a multidimensional array and the other is structured normally as seen below.
Array (
[0] = Array
(
[0] => Array
(
[james] => 1
[kevin] => 2
)
[1] => Array
(
[joe] => 1
[jim] => 2
)
)
[1] = Array
(
[0] => Array
(
[jill] => 1
[john] => 2
)
[1] => Array
(
[janet] => 1
[clarence] => 2
)
)
)
and the second array
Array
(
[0] => Array
(
[total_stuff] => 75210
)
[1] => Array
(
[total_stuff] => 95640
)
)
How would I append the first value of the second array to the end of the first inner array within the multidimensional array so it would look like the array that follows? I need to preserve the values of the second array but not the keys.
Array (
[0] = Array
(
[0] => Array
(
[james] => 1
[kevin] => 2
)
[1] => Array
(
[joe] => 1
[jim] => 2
)
[2] => Array
(
[total_stuff] => 75210
)
)
[1] = Array
(
[0] => Array
(
[jill] => 1
[john] => 2
)
[1] => Array
(
[janet] => 1
[clarence] => 2
)
[2] => Array
(
[total_stuff] => 95640
)
)
)
You can do it using array map and array merge
<?php
$a1=array (
array
(
array
(
"james" => 1,
"kevin" => 2
),
array
(
"joe" => 1,
"jim" => 2
)
),
array
(
array
(
"jill" => 1,
"john" => 2
),
array
(
"janet" => 1,
"clarence" => 2
)
)
);
$a2=array
(
array
(
"total_stuff" => 75210
),
array
(
"total_stuff" => 95640
)
);
//merge each index with corresponding index of second array to form new array as you desired
$new = array_map(function ($a,$k)use($a2) { return array_merge($a,array($a2[$k])); }, $a1,array_keys($a1));
echo "<pre>";
print_r($new);
?>
output
Array
(
[0] => Array
(
[0] => Array
(
[james] => 1
[kevin] => 2
)
[1] => Array
(
[joe] => 1
[jim] => 2
)
[2] => Array
(
[total_stuff] => 75210
)
)
[1] => Array
(
[0] => Array
(
[jill] => 1
[john] => 2
)
[1] => Array
(
[janet] => 1
[clarence] => 2
)
[2] => Array
(
[total_stuff] => 95640
)
)
)
working fiddle http://phpfiddle.org/main/code/ymf7-69si
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];}));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Array ( [Hydraulics] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson3 [1] => 1 ) [3] => Array ( [0] => Lesson1 [1] => 1 ) [4] => Array ( [0] => Lesson2 [1] => 1 ) [5] => Array ( [0] => Lesson3 [1] => 1 ) ) [Waste Water Engineering] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 0 ) ) [RCC Structure Design] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 1 ) ) [Irrigation] => Array ( [0] => Array ( [0] => Lesson1 [1] => 0 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 1 ) ) [Plastic Blocks] => Array ( [0] => Array ( [0] => Lesson1 [1] => 1 ) [1] => Array ( [0] => Lesson2 [1] => 1 ) [2] => Array ( [0] => Lesson3 [1] => 1 ) ) )
If you see Hydraulics array lesson1 appears 2 times. I want to add Lesson1 1st position value to be added and delete other duplicate entries. I want to feed the data to google charts.I have removed some array part as it was too long.
You can simply loop through the array to find same values and you can also add and remove them, check the code below to understand, I hope this will work for you.
<?php
$array['Hydraulics'] = array (
0 => array ( 0 => 'Lesson1', 1 => 1 ),
1 => array ( 0 => 'Lesson3', 1 => 1 ),
3 => array ( 0 => 'Lesson1', 1 => 1 ),
4 => array ( 0 => 'Lesson2', 1 => 1 ),
5 => array ( 0 => 'Lesson3', 1 => 1 )
);
$checked_keys=array(); //array to store checked keys.
foreach($array['Hydraulics'] as $key1 =>$val1){ ///first loop
$string1 = $val1[0]; //value at key 0 for each node eg. Lesson1,Lesson3 etc
foreach($array['Hydraulics'] as $key2 => $val2){ ///again loop the same array for finding same values
$string2 = $val2[0]; //value at key 0 for each node eg. Lesson1,Lesson3 etc
if($string1==$string2 && $key2 != $key1 && !in_array($key2,$checked_keys)){ //will go further only value matches and key of first loop != second loop
$array['Hydraulics'][$key1][1] = $val1[1]+$val2[1]; //add the values and index 1.
$checked_keys[]= $key1; ///push chekced keys in array for skipping next time.
unset($array['Hydraulics'][$key2]); //unset the duplicate values.
}
}
}
echo "<pre>";print_r($array);//output
?>
This will give you :
Array
(
[Hydraulics] => Array
(
[0] => Array
(
[0] => Lesson1
[1] => 2
)
[1] => Array
(
[0] => Lesson3
[1] => 2
)
[4] => Array
(
[0] => Lesson2
[1] => 1
)
)
)
CLICK HERE FOR LIVE DEMO
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 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];
}