Php add array value of 1st position if 0th position matches [closed] - php

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

Related

PHP - Appending a normally structured array to the nested arrays within a multidimensional array [duplicate]

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

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

Rebuild php array structure

I've been looking for the follow solution to create the array structure I need.
For a a list with updates for a specific user who can be following different things, I need to get first the followed items and the data compare to this item. my output for this is as follow.
array how it is right now
Array
(
[0] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
)
)
[1] => Array
(
[website_id] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
)
)
)
I want to create the above array like tho on below
array like it has to be
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[follower_id] => 1
)
[1] => Array
(
[id] => 1
[follower_id] => 2
)
[2] => Array
(
[id] => 1
[follower_id] => 3
)
[3] => Array
(
[id] => 1
[follower_id] => 4
)
[4] => Array
(
[id] => 1
[follower_id] => 5
)
[5] => Array
(
[id] => 1
[follower_id] => 1
)
)
Thanks in advice.
With kind regards,
Nicky
Seems to me that array_merge() should be sufficient:
$newArray = array_merge($myArray[0]['website_id'], $myArray[1]['website_id']);

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