i am trying to flat out a multidimensional array to a simple array. I tried searching for answers and used some functions i found here. But i am stuck on adding variables inside foreach loop after flatting array out:
This is my function:
public function create_order_direct($order_data) {
$result = MarathonUtil::flatten_array($order_data);
foreach ($result as $data) {
$media_id = $data['media_id'];
$headline = $data['headline'];
$client_contact = $data['client_contact'];
$plan_number = $data['plan_number'];
$insertion_date = $data['insertion_date']; //NOW();?
$start_date = $data['insertion_start_date'];
$end_date = $data['insertion_end_date'];
$PO_number = $data['PO_number'];
$price_code = $data['price_code'];
$gross = $data['gross'];
}
}
MarathonUtil::flatten_array
is this:
static function flatten_array($array) {
$prefix = '';
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + self::flatten_array($value, $prefix . $key . '.');
}
else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
So when running example echo $media_id
, it says undefined
. When i return $result
, all data is available but not inside foreach? I only get the data $price_code
and $gross
(those are the deepest in array, see under)
The array looks like this before flatting out:
[{
"media_id": "FACE",
"headline": "FACEBOOK FEBRUAR 2018",
"agreement_id": "REDP",
"client_contact": "Asim Tariq",
"order_id": 4710,
"plan_number": 407,
"insertion": {
"insertion_date": "2018-09-25",
"start_date": "2018-09-25",
"end_date": "2018-10-10",
"PO_number": 150,
"price_row": {
"price_code": "000",
"gross": 10002345
}
}
}]
so a simple echo $data['media_id']
won't work inside foreach or either print_r($result)
that gives me only price_code
and gross