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
Related
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
I am trying to get the values in the [headers] array from the last array number. I then want to assign these values to variables to use in a foreach loop. The [headers] array numbers are sometimes different to other [headers] arrays, you can see this below.
The first [headers] array has 'From' and 'Date' under [0] => Array and [1] Array. The second [headers] array has 'From' and 'Date' under [11] => Array and [12] => Array.
So far I have only managed to get a first matched value using:
$arr = array($array);
$arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
foreach ($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
if ($subArray['name'] === 'From') {
$outputArray = iterator_to_array($subArray);
}
}
Using this array:
Array
(
[id] => 15acc078ff3c13bb
[historyId] => 30098
[messages] => Array
(
[0] => Array
(
[id] => 15acc078ff3c13bb
[threadId] => 15acc078ff3c13bb
[labelIds] => Array
(
[0] => Label_335
[1] => IMPORTANT
[2] => Label_332
[3] => CATEGORY_PERSONAL
[4] => INBOX
)
[snippet] => ok
[historyId] => 30084
[internalDate] => 1489481730000
[payload] => Array
(
[mimeType] => multipart/alternative
[filename] =>
[headers] => Array
(
[0] => Array
(
[name] => From
[value] => google#gmail.com
)
[1] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:55:30 +0700
)
)
)
[1] => Array
(
[id] => 15acc09c623d48dd
[threadId] => 15acc078ff3c13bb
[labelIds] => Array
(
[0] => SENT
)
[snippet] => test On Tue, Mar 14, 2017 at 3:55 PM, test user wrote: > ok
[historyId] => 30098
[internalDate] => 1489481877000
[payload] => Array
(
[partId] =>
[mimeType] => text/plain
[filename] =>
[headers] => Array
(
[11] => Array
(
[name] => From
[value] => google2#gmail.com
)
[12] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:57:57 +0700
)
)
)
)
)
)
You could do the following in order to iterate all headers with the structure of your array. Finding the last one should be trivial.
foreach($yourArray['messages'] as $messages){
foreach($messages['payload']['headers'] as $header){
// header will have those values:
/*
* [0] => Array
(
[name] => From
[value] => google#gmail.com
)
[1] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:55:30 +0700
)
[0] => Array
(
[name] => From
[value] => google2#gmail.com
)
[1] => Array
(
[name] => Date
[value] => Tue, 14 Mar 2017 15:57:57 +0700
)
*/
}
}
you can use array_values to reindex the array.
$maxHistoryId = 0;
$headers = [];
foreach($array['id'] as $v)
{
if($v['historyId'] >= $max)
{
$max = $v['historyId'];
$headers = $v['payload']['headers'];
}
}
$resultArray = array_values($headers);
$resultArray = $resultArray[0];
really need your help with this array :
Array
(
[status] => 200
[error] =>
[resource] => Array
(
[type] => stats
[data] => Array
(
[0] => Array
(
[date] => Array
(
[year] => 2015
[month] => 12
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 2.53
[average] => 1.265
)
)
[1] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => USD
[stats] => Array
(
[count] => 2
[total] => 15
[average] => 7.5
)
)
[2] => Array
(
[date] => Array
(
[year] => 2016
[month] => 1
)
[currency] => AUD
[stats] => Array
(
[count] => 1
[total] => 15
[average] => 15
)
)
[3] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => AUD
[stats] => Array
(
[count] => 7
[total] => 1419.02
[average] => 202.71714285714
)
)
[4] => Array
(
[date] => Array
(
[year] => 2016
[month] => 2
)
[currency] => USD
[stats] => Array
(
[count] => 8
[total] => 2186.4
[average] => 273.3
)
)
[5] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => USD
[stats] => Array
(
[count] => 3
[total] => 865
[average] => 288.33333333333
)
)
[6] => Array
(
[date] => Array
(
[year] => 2016
[month] => 3
)
[currency] => AUD
[stats] => Array
(
[count] => 19
[total] => 127279
[average] => 6698.8947368421
)
)
)
)
)
How i can get $value of [month] in the loop??
Help me please!
Do you mean you're trying to access the "month" index within date?
If so, you could try something like this:
Let's say $array is your array.
foreach($array['resource']['data'] as $data) {
echo $data['date']['month']; // Prints every month.
echo "<br/>";
}
Hope this helps.
Try array_column function (available since PHP 5.5)
// $arr is the initial array
$months = array_column($arr, 'month');
http://php.net/manual/ru/function.array-column.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) ;
I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}