My current Array looks like the following which i'm trying to restructure to be able to exact data more efficiently
Array
(
[0] => 2022, 6, amex_direct, 99.991
[1] => 2022, 7, amex_direct, 99.984
[2] => 2022, 8, amex_direct, 99.98
[3] => 2022, 9, amex_direct, 99.986
[4] => 2022, 5, first_data, 99.995
[5] => 2022, 6, first_data, 99.995
[6] => 2022, 7, first_data, 99.982
[7] => 2022, 8, first_data, 99.996
[8] => 2022, 9, first_data, 99.995
[9] => 2022, 5, integrations, 99.994
[10] => 2022, 6, integrations, 99.988
[11] => 2022, 7, integrations, 99.882
[12] => 2022, 8, integrations, 99.991
[13] => 2022, 9, integrations, 99.959
[14] => 2022, 9, mastercard_direct, 100.0
[15] => 2022, 5, moneris, 99.99
[16] => 2022, 6, moneris, 99.985
[17] => 2022, 7, moneris, 99.997
[18] => 2022, 8, moneris, 99.97
[19] => 2022, 9, moneris, 99.993
)
I'm looking to have it broken down easier as the following:
Array
(
[0] => [year] => 2022
[month] => 6
[name] => amex_direct
[percent] => 99.991
[1] => [year] => 2022
[month] => 6
[name] => amex_direct
[percent] => 99.984
.......
CodePudding user response:
Loop through the array, explode the elements and re-assign the value.
$array = array( "2022, 6, amex_direct, 99.991", "2022, 7, amex_direct, 99.984" );
$keys = array( 'year', 'month', 'name', 'percent' );
foreach ( $array as $key => $val ) {
$array[$key] = explode( ',', $val );
$temp = array();
for ( $i = 0; $i < count( $array[$key] ); $i ) {
$temp[$keys[$i]] = $array[$key][$i];
}
$array[$key] = $temp;
}
CodePudding user response:
Thanks i've got it sorted by month now but now i need
array (size=5)
' 6' =>
array (size=5)
0 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 6' (length=2)
'name' => string ' amex_direct' (length=12)
'percent' => string ' 99.991' (length=7)
5 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 6' (length=2)
'name' => string ' first_data' (length=11)
'percent' => string ' 99.995' (length=7)
10 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 6' (length=2)
'name' => string ' integrations' (length=13)
'percent' => string ' 99.988' (length=7)
16 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 6' (length=2)
'name' => string ' moneris' (length=8)
'percent' => string ' 99.985' (length=7)
7' =>
array (size=5)
1 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 7' (length=2)
'name' => string ' amex_direct' (length=12)
'percent' => string ' 99.984' (length=7)
6 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 7' (length=2)
'name' => string ' first_data' (length=11)
'percent' => string ' 99.982' (length=7)
11 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 7' (length=2)
'name' => string ' integrations' (length=13)
'percent' => string ' 99.882' (length=7)
17 =>
array (size=4)
'year' => string ' 2022' (length=5)
'month' => string ' 7' (length=2)
'name' => string ' moneris' (length=8)
'percent' => string ' 99.997' (length=7)
Now I need the data like the following which the first digit of each row is the month
6,99.991,99.995,99.988,99.985
7,99.984,99.95 .......