How to create multidimensional array with key value pair php - php

I have two arrays like this :
Array 1 is having x-axis values in array y-axis values in array
x-array: ["04 Feb","05 Feb","06 Feb","07 Feb","08 Feb","09 Feb","10 Feb"]
y-array: [3.27,3.34,3.27,3.2,3.28,3.17,3.15]
Array2 is having x-axis values in array y-axis values in array
x-array2: ["11 Feb", "12 Feb"]
y-array2: [3.19, 3.36]
How to create multidimensional array with key value pair.
Like below in php
Array
(
[0] => Array
(
[x-axis] => Array
(
[0] => 04 Feb
[1] => 05 Feb
[2] => 06 Feb
[3] => 07 Feb
[4] => 08 Feb
[5] => 09 Feb
[6] => 10 Feb
)
[y-axis] => Array
(
[0] => 3.27
[1] => 3.34
[2] => 3.27
[3] => 3.2
[4] => 3.28
[5] => 3.17
[6] => 3.15
)
)
[1] => Array
(
[x-axis] => Array
(
[0] => 11 Feb
[1] => 12 Feb
)
[y-axis] => Array
(
[0] => 3.19
[1] => 3.36
)
)
)

$new_array = array(array("x-axis"=>$x-axis-array,
"y-axis"=>$y-axis-array
),
array("x-axis"=>$x-axis-array2,
"y-axis"=>$y-axis-array2
)
);
echo "<pre>";
print_r($new_array);

$array = array(
0 => array(
"x-axis" => $x_array,
"y-axis" => $y_array),
1 => array(
"x-axis" => $x_array2,
"y-axis" => $y_array2)
) ;
var_dump($array) ;

Related

Update multidimensional array from (Y-m) to (Y)

I would like to know if there is any way to change or update a multidimensional array from this
Array (
[AZ] => Array ( [1] => 2020-01 [2] => 2020-02 )
[BY] => Array ( [0] => 2020-03 [1] => 2020-04 )
[CX] => Array ( [1] => 2020-05 [2] => 2020-06 [3] => 2020-07 )
[DW] => Array ( [106] => 2019-01 [107] => 2019-02 [108] => 2019-03)
)
To this
Array (
[AZ] => Array ( [1] => 2020 [2] => 2020 )
[BY] => Array ( [0] => 2020 [1] => 2020 )
[CX] => Array ( [1] => 2020 [2] => 2020 [3] => 2020 )
[DW] => Array ( [106] => 2019 [107] => 2019 [108] => 2019)
)
I don't know if this is possible but I hope someone can help me
You can use array_walk_recursive to walk over all the values in the array, changing those that match a YYYY-MM format to YYYY using preg_replace:
array_walk_recursive($array, function (&$v) {
$v = preg_replace('/^(\d{4})-\d\d$/', '$1', $v);
});
Note we use &$v as the argument to the callback so that we can change the values in the array.
Output:
Array
(
[AZ] => Array
(
[1] => 2020
[2] => 2020
)
[BY] => Array
(
[0] => 2020
[1] => 2020
)
[CX] => Array
(
[1] => 2020
[2] => 2020
[3] => 2020
)
[DW] => Array
(
[106] => 2019
[107] => 2019
[108] => 2019
)
)
Demo on 3v4l.org

Creating multi-dimensional array from multiple distinct arrays

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);

Display a sum of two array values with same keys

I have two array with same keys this two array contain month wise data of my table. i want sum of this values and return same keys sum of values in one other array
Here is my two array
array1
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 0 )
[3] => Array ( [0] => Apr [1] => 9 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array 2:
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 0 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 1 )
[4] => Array ( [0] => May [1] => 1 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
i also tried using this code
function sum_arrays($array1, $array2) {
$array = array();
foreach($array1 as $index => $value) {
$array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
}
return $array;
}
i want result like below
Array ( [0] => Array ( [0] => Jan [1] => 0 )
[1] => Array ( [0] => Feb [1] => 22 )
[2] => Array ( [0] => Mar [1] => 18 )
[3] => Array ( [0] => Apr [1] => 10 )
[4] => Array ( [0] => May [1] => 2 )
[5] => Array ( [0] => Jun [1] => 0 )
[6] => Array ( [0] => Jul [1] => 0 )
[7] => Array ( [0] => Aug [1] => 0 )
[8] => Array ( [0] => Sep [1] => 0 )
[9] => Array ( [0] => Oct [1] => 0 )
[10] => Array ( [0] => Nov [1] => 0 )
[11] => Array ( [0] => Dec [1] => 0 )
)
array_map approach (if 2 arrays have month items in same order):
// shortened array samples
$arr1 = [
["Jan", 0],
["Feb", 22],
["Mar", 0]
];
$arr2 = [
["Jan", 0],
["Feb", 0],
["Mar", 18]
];
$result = array_map(function($a, $b){
return [$a[0], $a[1] + $b[1]];
}, $arr1, $arr2);
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Jan
[1] => 0
)
[1] => Array
(
[0] => Feb
[1] => 22
)
[2] => Array
(
[0] => Mar
[1] => 18
)
)
Using The foreach Loop:
$sum = 0;
foreach($items as $item) {
$sum += $item['qty'];
}
echo $sum;
Using array_map():
echo array_sum(array_map(
function($item) {
return $item['qty'];
}, $items)
);
Using array_reduce():
echo array_reduce($items, function($carry, $item) {
$carry += $item['qty'];
return $carry;
});

calculate data within foreach loop

I have pulled out data from my database of eps of companies for last 2 years I wanted to sum up eps by using a small formula what happened here is I pulled out data in the form of array now I want to put formula like this `
`
$curr_eps - $old_eps / $old_eps * 100;
With the out put I am getting I am unable to put formula for every company and get separate calculated values
My output data is like this
foreach($data3 as $key => $pr_data) {
$prof_data[] = $pr_data;
}
Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
)
[2] => Array
(
[eps] => 0.90
[year] => 2015
)
[3] => Array
(
[eps] => 0.81
[year] => 2014
)
[4] => Array
(
[eps] => 1.05
[year] => 2016
)
[5] => Array
(
[eps] => 3.71
[year] => 2015
)
[6] => Array
(
[eps] => 1.61
[year] => 2016
)
[7] => Array
(
[eps] => -0.49
[year] => 2015
)
)
I am wondering to put data here as of without loop this is the output coming on
$prof_data[] = $data3 //This contains array value;
Out Put`
Array
(
[0] => Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
[company_id] => 348
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
[company_id] => 348
)
)
[1] => Array
(
[0] => Array
(
[eps] => 0.90
[year] => 2015
[company_id] => 351
)
[1] => Array
(
[eps] => 0.81
[year] => 2014
[company_id] => 351
)
)
[2] => Array
(
[0] => Array
(
[eps] => 1.05
[year] => 2016
[company_id] => 356
)
[1] => Array
(
[eps] => 3.71
[year] => 2015
[company_id] => 356
)
)
[3] => Array
(
[0] => Array
(
[eps] => 1.61
[year] => 2016
[company_id] => 366
)
[1] => Array
(
[eps] => -0.49
[year] => 2015
[company_id] => 366
)
)
[4] => Array
(
)
)
Now can anybody help me out to solve this issue I am not good while making to understand what I wam trying to do please let e know if you have queries
As I understand you can perform calculation as
foreach($data3 as $data){
$net = $currenteps - $data['eps'];
}
You haven't defined what are the currenteps and oldeps, however using $data['eps'] and $data['year'], you can access each eps value and respective year of array

how to arrange multidimensional array - Group by value in php?

I want to arrange same date items to single index, I have following array -
Array(
[0] => Array
(
[date] => 30 Dec 2015
[record] => Array
(
[id] => 84675
[name] => Item1
)
)
[1] => Array
(
[date] => 28 Dec 2015
[record] => Array
(
[id] => 84675
[name] => item2
)
)
[2] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 2011
[name] => item3
)
)
[3] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 86649
[name] => item4
)
))
I want to arrange this array like -
Array(
[0] => Array
(
[date] => 30 Dec 2015
[record] => Array
(
[id] => 84675
[name] => Item1
)
)
[1] => Array
(
[date] => 28 Dec 2015
[record] => Array
(
[id] => 84675
[name] => item2
)
)
[2] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 2011
[name] => item3
),Array
(
[id] => 86649
[name] => item4
)
)
)
I want to arrange same date items into single index, can anybody please help me.I tried to arrange it using loops but couldn't get success.Any help would be really appreciated.
Thanks!
Try this code it can help
<?php
$result = array();
foreach($data as $info)
{
$result[$info['date']][] = $info;
}
print_r($result);
?>
You can do this in such a way also. In this way you will be able to get the dates in keys of $record_array and record values in values. Try this..
$your_array = array();
$record_array = array();
foreach ($your_array as $value){
$record_array[$value['date']][] = $value['record'];
}
print_r($record_array);
Hope this wil help

Categories