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
Related
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];}));
Below are my arrays echoed inside a for-each loop.
Array
(
[0] => 1
[1] => 2
[2] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
)
Array
(
[0] => 2
[1] => 1
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 1
)
Array
(
[0] => 3
[1] => 3
[2] => 3
)
Array
(
[0] => 3
[1] => 3
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 2
)
Array
(
[0] => 4
[1] => 2
[2] => 1
)
I would like to group these arrays based on the value of the first item (index = 0) and get the following dimension array.
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 2
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
)
Array
(
[3] => Array
(
[0] => 2
[1] => 1
[2] => 1
)
)
Array
(
[4] => Array
(
[0] => 3
[1] => 3
[2] => 1
)
[5] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[6] => Array
(
[0] => 3
[1] => 3
[2] => 2
)
)
Array
(
[7] => Array
(
[0] => 4
[1] => 2
[2] => 2
)
[8] => Array
(
[0] => 4
[1] => 2
[2] => 1
)
)
I have spent hours to figure this out but due to less experience I still cannot get this done. Please help me with some algoritme.
Won't that do the trick?
$result = [];
foreach ($originalArrays as $array) {
$result[$array[0]][] = $array;
}
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 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'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']);