I suspect this question has been answered before but i have dug and dug this great forum for an answer in vain.....
I have 3 arrays that looks like this:
Array
(
[1] => 19
[2] => 2
[3] => 2018
)
Array
(
[1] => 19
[2] => 1
[3] => 2017
)
Array
(
[1] => 18
[2] => 2
[3] => 2016
)
I would like to convert this 3 arrays into a multidimensional array to look something like this:
$mynewArray = Array(
[0] =>array(
[1] => 19
[2] => 2
[3] => 2018
)
[1] =>array(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => array(
[1] => 18
[2] => 2
[3] => 2016
)
)
How do i achieve this in Php?
Demo Link.
You just need to add it in parent array as below,
$arr1 = [1 => 19, 2 => 2, 3 => 2018];
$arr2 = [1 => 19, 2 => 1, 3 => 2017];
$arr3 = [1 => 18, 2 => 2, 3 => 2016];
$mynewArray = [$arr1,$arr2,$arr3];
print_r($mynewArray);
Output
Array
(
[0] => Array
(
[1] => 19
[2] => 2
[3] => 2018
)
[1] => Array
(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => Array
(
[1] => 18
[2] => 2
[3] => 2016
)
)
Also you can append your child arrays to the parent by
$array1 = array("1"=>"1","2"=>"2","3"=>"3");
$array2 = array("1"=>"1","2"=>"2","3"=>"3");
$newarray = array($array1,$array2);
Related
Below is the array in php and I need function to solve n numbers of array as displayed in Expected result by summing the (2) and (3) values if (0) and (1) are matching :
Array
(
[0] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 1
[3] => 2
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 3
[3] => 4
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 5
[3] => 6
)
)
[1] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 7
[3] => 8
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 9
[3] => 10
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 11
[3] => 12
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
)
Expected Result :
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
Need help to get the above expected result for the raised query. I tried below code :
<?php
$sum = [];
foreach($weekly as $aSet) {
foreach($aSet as $k => $kv) {
$i = 0;
foreach($kv as $v) {
$sum[$k][$i] += $v;
$i++;
}
}
}
?>
But it will add all types :
[0] => Array
(
[0] => 0
[1] => 4038
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => 0
[1] => 4038
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => 0
[1] => 4038
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => 0
[1] => 4038
[2] => 13
[3] => 14
)
I want get sum only for 2 and 3rd value and need to keep constant for 0 and 1st value for exact match
Because all your nested arrays are with predefined amount of elements, all of those with the same indexes, you can use a for loop instead of the third foreach:
$sum = [];
foreach($weekly as $aSet)
{
foreach($aSet as $k => $kv)
{
$sum[$kv[0]][0] = $kv[0];
$sum[$kv[0]][1] = $kv[1];
for($i = 2; $i <= 3; $i++)
{
$sum[$kv[0]][$i] += $kv[$i];
}
}
}
echo "<pre>";
print_r($sum);
echo "</pre>";
With a for loop you can cycle inside the array considering only the needed elements of that array (in your case the indexes 0 and 1 aren't useful in this loop).
Output:
Array
(
[Week - 1] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[Week - 2] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[Week - 3] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[Week - 4] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
If you prefer the array with numbered indexes do that:
echo "<pre>";
print_r(array_values($sum));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
I'm so confused with arrays. Can any one help me to solve this??
I have 4 arrays, these all are related.
My array structure is like:
Array 1:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 2:
Array ( [0] => 500 [1] => 500 [2] => 1 [3] => 2 [4] => 3 [5] => 3 )
Array 3:
Array ( [0] => 2 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 4:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
I have to map all 1 value from Array 1 to another arrays.
If you want to map the values of 4 arrays to each position, you can:
$arr1 = array(1, 2, 1, 1, 2, 1 );
$arr2 = array(500, 500, 1, 2, 3, 3 );
$arr3 = array(2, 2, 1, 1, 2, 1 );
$arr4 = array(1, 2, 1, 1, 2, 1 );
$results = array_map(function($v1, $v2, $v3, $v4) {
return array($v1, $v2, $v3, $v4);
}, $arr1, $arr2, $arr3, $arr4);
echo "<pre>";
print_r( $results );
echo "</pre>";
This will result to:
Array
(
[0] => Array
(
[0] => 1
[1] => 500
[2] => 2
[3] => 1
)
[1] => Array
(
[0] => 2
[1] => 500
[2] => 2
[3] => 2
)
[2] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
)
[3] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 1
)
[4] => Array
(
[0] => 2
[1] => 3
[2] => 2
[3] => 2
)
[5] => Array
(
[0] => 1
[1] => 3
[2] => 1
[3] => 1
)
)
Doc: http://php.net/manual/en/function.array-map.php
Hello i am new in php and I just stuck in brainstorming array, I have below php array
[0] => Array
(
[0] => Array
(
[1] => 5
[2] => 12
[11] => 15
)
[1] => Array
(
[1] => 5
[2] => 12
[11] => 16
)
[2] => Array
(
[1] => 4
[2] => 9
[11] => 15
)
[3] => Array
(
[1] => 3
[2] => 9
[11] => 15
)
[4] => Array
(
[1] => 3
[2] => 9
[11] => 16
)
[5] => Array
(
[1] => 3
[2] => 13
[11] => 15
)
[6] => Array
(
[1] => 3
[2] => 13
[11] => 16
)
[7] => Array
(
[1] => 3
[2] => 12
[11] => 15
)
[8] => Array
(
[1] => 3
[2] => 12
[11] => 16
)
[9] => Array
(
[1] => 4
[2] => 9
[11] => 16
)
[10] => Array
(
[1] => 4
[2] => 13
[11] => 15
)
[11] => Array
(
[1] => 4
[2] => 13
[11] => 16
)
[12] => Array
(
[1] => 4
[2] => 12
[11] => 15
)
[13] => Array
(
[1] => 4
[2] => 12
[11] => 16
)
[14] => Array
(
[1] => 5
[2] => 9
[11] => 15
)
[15] => Array
(
[1] => 5
[2] => 13
[11] => 16
)
)
And I want below out put
[0]=> Array
(
[5]=> Array
(
[12]=> Array
(
[0] => 15
[1] => 16
)
[9]=> Array
(
[0] => 15
)
[13]=> Array
(
[0] => 16
)
)
[4]=> Array
(
[9]=> Array
(
[0] => 15
[1] => 16
)
[13]=> Array
(
[0] => 16
)
[12]=> Array
(
[0] => 15
[1] => 16
)
)
[3]=> Array
(
[9]=> Array
(
[0] => 15
[1] => 16
)
[13]=> Array
(
[0] => 15
[1] => 16
)
[12]=> Array
(
[0] => 15
[1] => 16
)
)
)
Sorry out put not much clear but I want check each array first common value e.g 5,4, and 3 then based on these value traverse though each value and create tree array
As Example simple i want each arrays first element and then recursively array check values and create new
Example have just 3 level but i want recursive n-level and , help would be Appreciate.
Not sure if I understand correctly as the samples are not explained clearly but I somehow was able to print the desired output. Explanations given thru comments
// I just simplified your given array
$input[0] = array(
[1 => 5, 2 => 12, 11 => 15],
[1 => 5, 2 => 12, 11 => 16],
[1 => 4, 2 => 9, 11 => 15],
[1 => 3, 2 => 9, 11 => 15],
[1 => 3, 2 => 9, 11 => 16],
[1 => 3, 2 => 13, 11 => 15],
[1 => 3, 2 => 13, 11 => 16],
[1 => 3, 2 => 12, 11 => 15],
[1 => 3, 2 => 12, 11 => 16],
[1 => 4, 2 => 9, 11 => 16],
[1 => 4, 2 => 13, 11 => 15],
[1 => 4, 2 => 13, 11 => 16],
[1 => 4, 2 => 12, 11 => 15],
[1 => 4, 2 => 12, 11 => 16],
[1 => 5, 2 => 9, 11 => 15],
[1 => 5, 2 => 13, 11 => 16]
);
// Declare new output array
$output[0] = [];
// Loop thru each array inside $input[0] array
foreach($input[0] as $key => $value) {
// Set each array value to variables
$one = $value[1];
$two = $value[2];
$eleven = $value[11];
// Check if $one key already exist, create if not
if (!isset($output[0][$one])) {
$output[0][$one] = [];
}
// Check if $two key already exist, create if not
if (!isset($output[$one][$two])) {
$output[0][$one][$two] = [];
}
// push each $eleven
$output[0][$one][$two][] = $eleven;
};
// Prints output array
echo '<pre>';
print_r($output);
To accommodate situations where you have multiple first-level elements, use a loop. Use another loop to iterate each entry. You can even use array destructuring inside the nested foreach() to declare variables.
Code: (Demo)
$result = [];
foreach ($input as $key => $array) {
foreach ($array as [1 => $one, 2 => $two, 11 => $eleven]) {
$result[$key][$one][$two][] = $eleven;
}
}
var_export($result);
There is no need to declare/assign parent keys to append data into them. There are deliberately no conditions in my snippet.
I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?
The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I need to get the sum of all numeric values in my array for each designated month. Ideally, it would return the following format.
April
total = 22
March
total = 'sum'
Array
(
[April] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] =>
[4] => 3
[5] =>
[6] => 2
[7] => 6
[8] => 3
[9] => 2
)
[March] => Array
(
[0] => 3.19198
[1] => 2.52219
[2] => 3.40053
[3] => 2.42639
[4] => 3.92301
[5] => 3.23758
[6] => 3.22457
[7] => 2.62855
)
Apply array_sum() on each of the sub-arrays using array_map():
$result = array_map('array_sum', $data);
Output:
Array
(
[April] => 22
[March] => 24.5548
)
Demo