(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 3
[1] => 4
)
)
)
I want to merge only specific keys as below, Key 0 and 1 are merged as a single array. Like this I want.
In the below example, keys 0 and 1 are merged, it can be changing, so I want custom.. please help me
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
)
This script will help you
<?php
$array = [
[
[
1,
2,
],
[
3,
4,
],
[
3,
4,
]
]
];
array_unshift($array[0], array_merge($array[0][0], $array[0][1]));
unset($array[0][1]);
unset($array[0][2]);
print_r($array);
output
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[3] => Array
(
[0] => 3
[1] => 4
)
)
)
First use array_merge to merge the elements and then use array_unshift to push the new element to array and then use unset to remove the old elements
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 different arrays with different dimensions that i need to merge in order to get a result with a specific this structure:
the first:
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
)
The second:
Array
(
[0] => 2017-12-26
[1] => 2018-01-30
)
The result should be :
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] =>2017-12-26
[1] => 2018-01-30
[2] => 1
)
)
I tried using array_merge but it does not work because they have not the same dimension. Also I need an element in the second tab ([2] => 1).
<?php
$array=Array
(
0 => Array
(
0 => '2017-11-03',
1 => '2017-11-05',
2 => '1',
),
1 => Array
(
0=> '2017-11-23',
1 => '2017-11-25',
2 => '1'
),
);
$arraySmall=Array
(
0 => '2017-12-26',
1 => '2018-01-30'
);
array_push($arraySmall, "1");
array_push($array, $arraySmall);
echo'<pre>';
print_r($array);
And the output is :
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] => 2017-12-26
[1] => 2018-01-30
[2] => 1
)
)
This way can work even without this line array_push($arraySmall, "1");
You can give it a try. In order to "merge" you need same size but for "push" you don't. So if you commend the line i told you the output will look like this:
Array
(
[0] => Array
(
[0] => 2017-11-03
[1] => 2017-11-05
[2] => 1
)
[1] => Array
(
[0] => 2017-11-23
[1] => 2017-11-25
[2] => 1
)
[2] => Array
(
[0] => 2017-12-26
[1] => 2018-01-30
)
)
What you describe is appending, not merging. Try this:
$arraySecond[] = 1; // This adds [2]=> 1
$arrayFirst[] = $arraySecond; // This adds second array to end of first
for your given example:
$x = ... // first array [2 dimensions]
$y = ... // second array [1 dimension]
$y = array_merge($y, array_diff($x[0], $y)); // add missing '1' to $y or any other key that are present in elements of $x and have to be added to $y
$x[] = $y; // append the 1-dim array as the new element in $x
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 have this array
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 6
)
[3] => Array
(
[0] => 7
[1] => 8
)
)
I want this array to be looks like this one:
Array
(
[0] => Array
(
[0] => 1
[1] => 5
)
[1] => Array
(
[0] => 3
[1] => 7
)
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 4
[1] => 8
)
)
I've spent almost 24 hours to accomplish this task. As you can understand, I need help. Please don't abuse on me by asking what have you tried. Can anyone accomplish this array job? thanks
Edit:
I have this array:
$yourArray = array(
array(1,2),
array(3,4),
array(5,6),
array(7,8),
);
It outputs this one:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
[1] => 6
)
[3] => Array
(
[0] => 7
[1] => 8
)
)
I want $yourArray to outputs this one:
Array
(
[0] => Array
(
[0] => 1
[1] => 5
)
[1] => Array
(
[0] => 3
[1] => 7
)
[2] => Array
(
[0] => 2
[1] => 6
)
[3] => Array
(
[0] => 4
[1] => 8
)
)
Let me write this too, I get output by using print_r($yourArray). And of course by using keyboard. Hope this helps
Considering what OP has made us understand, and quite seriously, I think this is what you're looking for:
$yourArray = array(
array(1,5),
array(3,7),
array(2,6),
array(4,8),
);
var_dump($yourArray);
Demo.
I really can not understand whether it is serious or not.
Explain yourself.
Here you go:
<?php
$array = array(
array(1,2),
array(3,4),
array(5,6),
array(7,8)
);
function processArray(&$array) {
for ($i = 0; $i < count($array); $i++) {
if ($array[$i][0] > 4) {
$array[$i][0] = $array[$i][0] - 3;
}
if ($array[$i][1] < 5) {
$array[$i][1] = $array[$i][1] + 3;
}
}
}
processArray($array);
print_r($array);
Outputs:
Array ( [0] => Array ( [0] => 1 [1] => 5 ) [1] => Array ( [0] => 3 [1] => 7 ) [2] => Array ( [0] => 2 [1] => 6 ) [3] => Array ( [0] => 4 [1] => 8 ) )
I don't see the pattern so the only possibility I see is doing it by hand.
$foo = [
[1,2],
[3,4],
[5,6],
[7,8]
];
$bar = [
[$foo[0][0],$foo[2][0]],
[$foo[1][0],$foo[3][0]],
[$foo[0][1],$foo[2][1]],
[$foo[1][1],$foo[3][1]]
];
print_r($bar);
Edit
Use array() instead of [ ] if you're using an older version of PHP.
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()