0

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

Asim
  • 936
  • 2
  • 10
  • 29
  • 1
    Possible duplicate of [How to Flatten a Multidimensional Array?](https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – Asim Sep 26 '18 at 14:22
  • 1
    there's one answer in your link close to mine, but many others are a tad verbose. – Progrock Sep 26 '18 at 19:40

1 Answers1

1
<?php
$json=<<<JSON
[{
    "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
        }
    }
}]
JSON;

$out = [];
$data = json_decode($json, TRUE);
array_walk_recursive($data, function($v, $k) use (&$out) {
    $out[$k] = $v;
});
var_export($out);

Output:

array (
    'media_id' => 'FACE',
    'headline' => 'FACEBOOK FEBRUAR 2018',
    'agreement_id' => 'REDP',
    'client_contact' => 'Asim Tariq',
    'order_id' => 4710,
    'plan_number' => 407,
    'insertion_date' => '2018-09-25',
    'start_date' => '2018-09-25',
    'end_date' => '2018-10-10',
    'PO_number' => 150,
    'price_code' => '000',
    'gross' => 10002345,
  )
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • If you really want individual variables you can use extract on `$out`. But I'd advice against it, and just access values via the keys. – Progrock Sep 26 '18 at 14:12
  • 1
    The code above is working with the result given for me on Php 7.1. Perhaps you don't need the json_decode step if you already have an array. I was assuming input as JSON as you say 'The array looks like this before flatting out:' – Progrock Sep 26 '18 at 14:13