Need some help formatting this array properly:
Here is the dump of the array:
array (size=5)
'Bank Deposit' =>
array (size=13)
'Payment Method' => string 'Bank Deposit' (length=12)
0 => string 'Bank Deposit' (length=12)
201704 => string '1200' (length=4)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cash' =>
array (size=13)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '300' (length=3)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '12' (length=2)
201709 => string '12' (length=2)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Cheque' =>
array (size=13)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '231' (length=3)
201709 => string '231' (length=3)
201702 => string '0' (length=1)
201710 => string '0' (length=1)
'Mobile Money' =>
array (size=13)
'Payment Method' => string 'Mobile Money' (length=12)
0 => string 'Mobile Money' (length=12)
201704 => string '0' (length=1)
201705 => string '0' (length=1)
201701 => string '0' (length=1)
201706 => string '0' (length=1)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
201602 => string '0' (length=1)
201709 => string '0' (length=1)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
'' =>
array (size=13)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
201602 => string '243' (length=3)
201709 => string '243' (length=3)
201702 => string '150' (length=3)
201710 => string '150' (length=3)
QUESTION: How do I get rid of the 'Payment Method' and 0 keys in the array?
WHAT I'VE TRIED:
Table as it is: https://ibb.co/gW55g5
Splicing the value from offset 1: https://ibb.co/fogNTk
Splicing the value and key from offset 1: https://ibb.co/jgU98k
This removes the columns but then reindexes the keys!
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach (array_splice($v,1) as $k => $v){
echo '<th>'.$k.'</th>';
}
var_dump($temp);
}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach (array_splice($v , 1) as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div>
</div>
I made an earlier post but it seemed to not get to the point but if you need any extra help in getting the picture you can see a more detailed question here: Multidimensional Array Dynamic Columns Format. Thanks
<?php
$data = [
'foo' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201704' => '23',
],
'bar' =>
[
'unwanted' => 'something',
0 => 'something else not wanted',
'201703' => '47',
]
];
foreach($data as $k => $v) {
unset($data[$k]['unwanted']);
unset($data[$k][0]);
}
var_export($data);
Output:
array (
'foo' =>
array (
201704 => '23',
),
'bar' =>
array (
201703 => '47',
),
)
Related
I don't understand why in my foreach loop I can't use
$products_option_value['products_option_value_id'] for example. I must use
$option['products_option_value']['products_option_value_id'] to display the good result else I have just a value
Below the elements
var_dump($options_array);
array (size=7)
0 =>
array (size=7)
'products_option_id' => string '213' (length=3)
'products_option_value' =>
array (size=13)
'products_option_value_id' => string '171' (length=3)
'option_value_id' => string '179' (length=3)
'name' => string 'S' (length=1)
'image' => null
'quantity' => string '100' (length=3)
'subtract' => string '0' (length=1)
'price' => string '1.0000' (length=6)
'price_prefix' => string '+' (length=1)
'weight' => string '0.00' (length=4)
'weight_prefix' => string '+' (length=1)
'customers_group_id' => string '99' (length=2)
'products_option_model' => string '99' (length=2)
'option_tax_class_id' => string '99' (length=2)
'option_id' => string '40' (length=2)
'name' => string 'Taille' (length=6)
'type' => string 'select' (length=6)
'value' => null
'required' => null
etc
foreach ($options_array as $option) {
foreach ($option['products_option_value'] as $products_option_value) {
var_dump($products_option_value);
}
}
the result is :
var_dump($option); result
array (size=7)
'products_option_id' => string '213' (length=3)
'products_option_value' =>
array (size=13)
'products_option_value_id' => string '171' (length=3)
'option_value_id' => string '179' (length=3)
'name' => string 'S' (length=1)
'image' => null
'quantity' => string '100' (length=3)
'subtract' => string '0' (length=1)
'price' => string '1.0000' (length=6)
'price_prefix' => string '+' (length=1)
'weight' => string '0.00' (length=4)
'weight_prefix' => string '+' (length=1)
'customers_group_id' => string '99' (length=2)
'products_option_model' => string '99' (length=2)
'option_tax_class_id' => string '99' (length=2)
'option_id' => string '40' (length=2)
'name' => string 'Taille' (length=6)
'type' => string 'select' (length=6)
'value' => null
'required' => null
var_dump($products_option_value) result
products_info_options_new.php:84:string '171' (length=3)
products_info_options_new.php:84:string '179' (length=3)
products_info_options_new.php:84:string 'S' (length=1
etc
http://php.net/manual/en/control-structures.foreach.php
You should use foreach($array as $key => $value) when iterating trough arrays that have keys. (I mean arrays such as this: array("key" => $value), and this is also your array)
I've had around 2 hours sleep in 36 hours so I may have minor over sights here but Here is the link of my previous post to get to where I am now:
Pivot MySQL Table with Dynamic Columns
With the help of that post I was able to generate a dynamic SQL query using PDO and PHP. In comparison to my attempts earlier in manipulating the array and displaying it in a tabular format I am completely stumped in this.
Here is the query:
$sql = "
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN EXTRACT(YEAR_MONTH FROM month_payment) = ',
EXTRACT(YEAR_MONTH FROM month_payment),
' THEN AMOUNT ELSE 0 END) AS `',
EXTRACT(YEAR_MONTH FROM month_payment),
'`'
)
) AS `pivot_columns`
FROM record_payment
WHERE month_payment BETWEEN ? AND ?
";
$stmt = $pdo->prepare($sql);
$date_from = '2015-01-01';
$date_to = '2017-08-01';
$stmt->execute([$date_from, $date_to]);
$row = $stmt->fetch();
$stmt->closeCursor();
$pivot_columns = $row['pivot_columns'];
$sql = "
SELECT title AS `Payment Method`, {$pivot_columns}
FROM record_payment t1
JOIN setting_payment_method spm ON spm.id = t1.method_id
WHERE month_payment BETWEEN ? AND ?
GROUP BY title WITH ROLLUP
";
echo $sql;
$stmt = $pdo->prepare($sql);
$stmt->execute([$date_from, $date_to]);
$results = $stmt->fetchAll();
$stmt->closeCursor();
var_dump($results);
Here is a vardump of my array:
array (size=3)
0 =>
array (size=9)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '1500' (length=4)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
1 =>
array (size=9)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
201612 => string '0' (length=1)
201708 => string '0' (length=1)
2 =>
array (size=9)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
201612 => string '800' (length=3)
201708 => string '800' (length=3)
UPDATED: This is how the dump looks after this manipulation
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
}
var_dump($temp);
BELOW
array (size=3)
'Cash' =>
array (size=7)
'Payment Method' => string 'Cash' (length=4)
0 => string 'Cash' (length=4)
201704 => string '1500' (length=4)
201705 => string '120' (length=3)
201701 => string '800' (length=3)
201706 => string '800' (length=3)
201707 => string '120' (length=3)
'Cheque' =>
array (size=7)
'Payment Method' => string 'Cheque' (length=6)
0 => string 'Cheque' (length=6)
201704 => string '10' (length=2)
201705 => string '0' (length=1)
201701 => string '590' (length=3)
201706 => string '590' (length=3)
201707 => string '0' (length=1)
'' =>
array (size=7)
'Payment Method' => null
0 => null
201704 => string '1510' (length=4)
201705 => string '120' (length=3)
201701 => string '1390' (length=4)
201706 => string '1390' (length=4)
201707 => string '120' (length=3)
My issue are this:
The keys of the array is generated dynamically from the user
interface. They select between 2 date fields and the query extracts
the year and month from it and sums up the values. e.g 201504 = April
2015 but an index key (0) comes from nowhere and just replicates the payment method. The Payment method should be at the top level with the date as the key and the aggregated
amount the key pair value.
I have tried manipulating the array by doing a nested loop and
creating an array from the subarray but with little result. I have
tried splicing array, slicing the array, shifting the array,
flattening the array, merging the array and many other things in order to manipulate and
format the array.
This is how the table comes out:
whilst using a nested loop and echoing the key and value.
https://ibb.co/dPYSuQ
EDITED
<div class='box-body no-padding'>
<table class='table cell-border' id='summary'><thead>
<tr>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
} foreach ($v as $k => $v){
echo '<th>'.$k.'</th>';
}}
?>
</tr></thead>
<?php
$temp = [];
foreach($results as $k => $v){
$temp[$v['Payment Method']] = $v;
foreach ($v as $k => $v){
echo '<td>'.$v.'</td>';
}
echo "</tr>";
}
?>
</table>
</div>
</div> </div>
This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 7 years ago.
I have an array which looks like (var_dump):
array (size=3)
0 =>
array (size=8)
'id' => string '1' (length=1)
'user_id' => string '64' (length=2)
'level' => string '1' (length=1)
'score' => string '9999' (length=4)
't1' => string '1' (length=1)
't2' => string '0' (length=1)
't3' => string '0' (length=1)
'attempts' => string '1' (length=1)
1 =>
array (size=8)
'id' => string '2' (length=1)
'user_id' => string '64' (length=2)
'level' => string '2' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '4' (length=1)
2 =>
array (size=8)
'id' => string '3' (length=1)
'user_id' => string '64' (length=2)
'level' => string '3' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '7' (length=1)
How can I change the key 0, 1, 2 etc.. to be the value of level inside that array?
For example:
1 =>
array (size=8)
'id' => string '1' (length=1)
'user_id' => string '64' (length=2)
'level' => string '1' (length=1)
'score' => string '9999' (length=4)
't1' => string '1' (length=1)
't2' => string '0' (length=1)
't3' => string '0' (length=1)
'attempts' => string '1' (length=1)
2 =>
array (size=8)
'id' => string '2' (length=1)
'user_id' => string '64' (length=2)
'level' => string '2' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '4' (length=1)
3 =>
array (size=8)
'id' => string '3' (length=1)
'user_id' => string '64' (length=2)
'level' => string '3' (length=1)
'score' => string '123456789' (length=9)
't1' => string '1' (length=1)
't2' => string '1' (length=1)
't3' => string '0' (length=1)
'attempts' => string '7' (length=1)
I have already tried renaming the key inside a forloop, that did not replace only the key, but instead, it replaced the whole array and left it blank.
Thanks
$arr = [ ['id'=>1], ['id'=>2], ['id'=>3]];
$new = [];
foreach($arr as $item)
$new[$item['id']] = $item;
print_r($new);
result
Array (
[1] => Array ( [id] => 1)
[2] => Array ( [id] => 2)
[3] => Array ( [id] => 3)
}
For a project i am trying to rebuild a array that the database returned. This data is needed to populate a graph.
array
0 =>
array
'payment_method_stats_id' => string '1' (length=1)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-01-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'January' (length=7)
1 =>
array
'payment_method_stats_id' => string '49' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-02-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'February' (length=8)
2 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'March' (length=5)
3 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'Paypal' (length=5)
'month' => string 'January' (length=5)
4 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'Visa' (length=5)
'month' => string 'January' (length=5)
The database returns a array with payment methods and with the month. so ideal is returned 12 times only with a different month.
now i need this data for a graph and i want to rebuild this array into this
'Januari'
=>payment
=> values, values, values, values values,
=>payment
=> values, values, values, values, values
how could i accomplish this
I have a function that I am writing to take in values datetime values from an array and convert it into hours and minutes format like 0010. I want to use this to later extract other values from the array as this is an associative array
function getData($lengthArray, $csvdat) { # passing down the values of the array and the length of the array
$newData = array(
'nightTime1',
'nightTime2',
'dayTime1',
'peakTime',
'dayTime2',
'winterNightTime1',
'winterNightTime2',
'winterDayTime1',
'winterDayTime2',
'winterPeakTime');
for ($count = 0; $count <= $lengthArray; $count++ ) { #looping to the length of the array
$arrayTimestamp = $csvdat[$count]['timestamp'];
$comparator = date('Hs', strtotime($arrayTimestamp)); #extracting the time format
$nightTime1 = 0759;
$dayTime1 = 1659;
$peakTime = 1859;
$dayTime2 = 2259;
$nightTime2 = 2359;
if ( $comparator == $nightTime1 ) {
if (empty($newData['nightTime1'])) {
$newData['nightTime1'] = $nightTime;
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan1'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan2'];
$newData['nightTime1']['phase1'] = $csvdat[$count++]['day_chan3'];
}
else if (!empty($newData)) {
}
}
}
return $newData;
}
getData($lengthData , $csvdata);
var_dump($newData);
The problem I am facing now is that I get an Undefined offset error at the line
$arrayTimestamp = $csvdat[$count]['timestamp'];
and an undefined variable for $newData. I am not very good at php so please advise.
Here is the var_dump of the csvdat array
array (size=118061)
0 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5972' (length=4)
'curr_property_cost' => string '62' (length=2)
'day_property' => string '19' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2898' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '9' (length=1)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2189' (length=4)
'curr_chan2' => string '2898' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '7' (length=1)
'day_chan2' => string '9' (length=1)
'day_chan3' => string '2' (length=1)
1 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '37' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2141' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '16' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2141' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '14' (length=2)
'day_chan2' => string '16' (length=2)
'day_chan3' => string '5' (length=1)
2 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5215' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '54' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2157' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '23' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2157' (length=4)
'curr_chan2' => string '2157' (length=4)
'curr_chan3' => string '901' (length=3)
'day_chan1' => string '21' (length=2)
'day_chan2' => string '23' (length=2)
'day_chan3' => string '8' (length=1)
3 =>
array (size=15)
'timestamp' => string '01/02/2014 00:00' (length=16)
'curr_property' => string '5183' (length=4)
'curr_property_cost' => string '54' (length=2)
'day_property' => string '71' (length=2)
'day_property_cost' => string '0' (length=1)
'curr_solar_generating' => string '2125' (length=4)
'curr_solar_export' => string '0' (length=1)
'day_solar_generated' => string '31' (length=2)
'day_solar_export' => string '0' (length=1)
'curr_chan1' => string '2173' (length=4)
'curr_chan2' => string '2125' (length=4)
'curr_chan3' => string '885' (length=3)
'day_chan1' => string '28' (length=2)
'day_chan2' => string '31' (length=2)
'day_chan3' => string '11' (length=2)
Try
for ($count = 0; $count < $lengthArray; $count++ )
instead of
for ($count = 0; $count <= $lengthArray; $count++ )
If and only if you are 100% sure that $lengthArray is the size of the $csvdat array, then your error is caused by this line:
for ($count = 0; $count <= $lengthArray; $count++ ) {
it should be
for ($count = 0; $count < $lengthArray; $count++ ) {
Right now you're breaking the loop if the $count is lower OR the same as $lengthArray. Notice that last key in your array is 3 and its length is 4.
EDIT
Now about Undefined variable: $newData.
You're trying to var_dump($newData) which is undefined. It exists inside the getData function but not outside it. Maybe you wanted to do it like this:
$newData = getData($lengthData , $csvdata);
var_dump($newData);
It should be $count < $lengthArray in for loop
for ($count = 0; $count < $lengthArray; $count++ ) {
}