I Have three arrays as array1 array2 array3 follows, I want this combine like as array4.
array1
(
[1869] => 1
[1871] => 1
[0] => 2
[1807] => 1
[1875] => 1
[1811] => 1
[1877] => 1
[1878] => 1
[1879] => 1
[1880] => 1
[1886] => 1
[1850] => 2
[1618] => 3
[1679] => 1
)
array2
(
[1619] => 1
[1625] => 1
)
array3
(
[1111] => 1
[2222] => 1
)
Need Output like:
array4
(
[1869] => 1
[1871] => 1
[0] => 2
[1807] => 1
[1875] => 1
[1811] => 1
[1877] => 1
[1878] => 1
[1879] => 1
[1880] => 1
[1886] => 1
[1850] => 2
[1618] => 3
[1679] => 1
[1619] => 1
[1625] => 1
[1111] => 1
[2222] => 1
)
I want this array1 and array2 combine like array3.Please some one help me.Thanks in advance.
If each array is on a variable you can do this,
$final=$arr1 + $arr2 + $arr3;
THat way you will not loose the keys as well. Check the manual here.
Or
$array1 = array('1869' => 1,'1871' => 1,'0' => 2,'1807' => 1,'1875' => 1,'1811' => 1,'1877' => 1,'1878' => 1,'1879' => 1,'1880' => 1,'1886' => 1,'1850' => 2,'1618' => 3,'1679' => 1);
$array2 = array('1619' => 1,'1625' => 1);
$array3 = array('1111' => 1,'2222' => 1);
$newArray = array_replace( $array1,$array2,$array3);
But I think just using the + is preferred.
print_r($newArray);
Related
I have the following array:
[0] => Array
(
[id] => 1
[uid] => 50
[sum1] => 1
[sum2] => 2
)
[1] => Array
(
[id] => 2
[uid] => 50
[sum1] => 2
[sum2] => 4
)
[2] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
As you can see, on some of those indexes, [uid] is the same. The length and data of the array is dynamic.
what i need to do is merge the indexes that have the same value for[uid] and sum the values for the other keys:
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
But for the life of me, i can't figure out how to do that.
Any help appreciated!
You can do it like this way,
<?php
$mainArray = [
0 => Array
(
'id' => 1,
'uid' => 50,
'sum1' => 1,
'sum2' => 2
),
1 => Array
(
'id' => 2,
'uid' => 50,
'sum1' => 2,
'sum2' => 4
),
2 => Array
(
'id' => 3,
'uid' => 51,
'sum1' => 3,
'sum2' => 5
)
];
$newArray=[];
foreach ($mainArray as $value) {
if(!array_key_exists($value['uid'], $newArray)){
// Store first time
$newArray[$value['uid']] = $value;
}else{
// Already exist, then sum and replace it
$newArray[$value['uid']]['id'] = $value['id'];
$newArray[$value['uid']]['sum1'] += $value['sum1'];// Sum with previous value
$newArray[$value['uid']]['sum2'] += $value['sum2'];// Sum with previous value
}
}
$newArray = array_values($newArray);// Reset indexes, Start the array index with zero
print_r($newArray);// To see the output
Output:
Array
(
[0] => Array
(
[id] => 2
[uid] => 50
[sum1] => 3
[sum2] => 6
)
[1] => Array
(
[id] => 3
[uid] => 51
[sum1] => 3
[sum2] => 5
)
)
I think you are correct. Actually, i solved the problem using something similar:
$sumArray = Array();
foreach ($array1 as $item){
if(isset($sumArray[$item['uid']])){
$sumArray[$item['idPartener']]['sum1'] += $item['sum1'];
$sumArray[$item['idPartener']]['sum2'] += $item['sum2'];
}else{
$sumArray[$item['uid']] = Array(
'id' => $item['id'],
'sum1' => $item['sum1'],
'sum2' => $item['sum2'] ,
);
}
}
Thanks all for your help!
I want to remove the first.. how to say that outside of the array and left inside array of a multidimension array. I couldn't found out the solution.. someone knows how to do it??
This is my code. I assign a array and i try to put into another array like this.
$final_sku = array();
foreach($skus as $sku){
foreach($sku as $key => $s){
$final_sku[] = array('Sku' => $s);
}
}
$newArray = array(
"Product" => array(
"PrimaryCategory" => "1",
"AssociatedSku" => "12",
"Attributes" => array(
),
"Skus" => $final_sku
)
);
And this is the $final_sku output be like.
[0] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
)
[1] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
)
I want the output be like this.
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
UPDATE: I want to pass the array and convert to xml. So the array key would be duplicated.
It does not make sense to have the same array of 2 members under the same key.
If you want to access one of them, what will be its uniqueness over the other?
The logical solution is
[sku] => [
0 => [first sku data....],
1 => [sku data....],
]
Request your help on sorting an array of array in PHP as below, tried all these function ksort, usort, arsort, krsort nothing seem to be working, any help would be much appreciated.
Original Array
Array
(
[serv1-DB] => Array
(
[2019-11-22] => 0
[2019-11-23] => 0
[2019-11-24] => 0
[2019-11-25] => 1
[2019-11-26] => 3
[2019-11-27] => 3
[2019-11-28] => 3
[2019-11-29] => 3
)
[ser2-DB] => Array
(
[2019-11-25] => 0
[2019-11-24] => 0
[2019-11-22] => 0
[2019-11-23] => 0
[2019-11-26] => 3
[2019-11-27] => 3
[2019-11-28] => 3
[2019-11-29] => 3
)
Output Required
Array
(
[serv1-DB] => Array
(
[2019-11-29] => 3
[2019-11-28] => 3
[2019-11-27] => 3
[2019-11-26] => 3
[2019-11-25] => 1
[2019-11-24] => 0
[2019-11-23] => 0
[2019-11-22] => 0
)
[ser2-DB] => Array
(
[2019-11-29] => 3
[2019-11-28] => 3
[2019-11-27] => 3
[2019-11-26] => 3
[2019-11-25] => 0
[2019-11-24] => 0
[2019-11-23] => 0
[2019-11-22] => 0
)
You want to sort the sub arrays, so you need to loop over each of them (in write mode) and sort the keys in reverse order using krsort:
foreach ($array as &$subArray) {
krsort($subArray);
}
Demo: https://3v4l.org/g8pBu
You have to sort reversely on the arrays like this:
// The arrays
$arrays = array(
"serv1-DB" => array(
"2019-11-22" => 0,
"2019-11-23" => 0,
"2019-11-24" => 0,
"2019-11-25" => 1,
"2019-11-26" => 3,
"2019-11-27" => 3,
"2019-11-28" => 3,
"2019-11-29" => 3
),
"ser2-DB" => array(
"2019-11-25" => 0,
"2019-11-24" => 0,
"2019-11-22" => 0,
"2019-11-23" => 0,
"2019-11-26" => 3,
"2019-11-27" => 3,
"2019-11-28" => 3,
"2019-11-29" => 3
)
);
// The sorting part
foreach($arrays AS $k => $array) {
krsort($array);
$arrays[$k] = $array;
}
I have these two 2Dimensional arrays,
$array1
Array(
'week1' => Array (
0 => '2015-06-29',
1 => '2015-06-30',
)
)
$array2
Array(
'week1' => Array (
0 => '2015-07-01',
1 => '2015-07-02',
2 => '2015-07-03',
3 => '2015-07-04',
4 => '2015-07-05',
),
'week2' => Array (
0 => '2015-07-06',
1 => '2015-07-07',
2 => '2015-07-08',
3 => '2015-07-09',
4 => '2015-07-10',
5 => '2015-07-11',
6 => '2015-07-12',
),
)
And this is my expected result,
Array(
'week1' => Array (
0 => '2015-06-29',
1 => '2015-06-30',
2 => '2015-07-01',
3 => '2015-07-02',
4 => '2015-07-03',
5 => '2015-07-04',
6 => '2015-07-05',
),
'week2' => Array (
0 => '2015-07-06',
1 => '2015-07-07',
2 => '2015-07-08',
3 => '2015-07-09',
4 => '2015-07-10',
5 => '2015-07-11',
6 => '2015-07-12',
),
)
i have been trying to use array_push but the array become 3 dimensional instead of joining the same key name. Could you guys halp me out?
Thanks in advance. :D
I think you can use array_merge_recursive (http://php.net/manual/en/function.array-merge-recursive.php)
Just:
array_merge_recursive($array1, $array2)
Just loop through your array, which you want to add and use the key to array_merge() the array, e.g.
foreach($array1 as $k => $v)
$array2[$k] = array_merge($array2[$k], $v);
I have a dummy array, that I want to order.
How can I have following result
with foreach?
with while next()
with RecursiveIterator
with IteratorIterator
which one is the fastest?
Here is array
$files = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
2015 => 'de.php',
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
);
Expected Result
$expected = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'test.php',
4 => 'wp-config.php',
5 => 'wp-content/',
6 => 'wp-content/uploads/',
7 => 'wp-content/uploads/2013/',
8 => 'wp-content/uploads/2013/05/',
9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
14 => '...'
);
array_walk_recursive is what you're looking for.
Example (not tested):
$expected = [];
array_walk_recursive($your_array, function($item, $key) {
// push item on to $expected
$expected[] = $item;
});
Working example