-4

Is there an elegant way to convert this JSON string:

{
  "my_index": 1,
  "first_name": "John",
  "last_name": "Smith",
  "address": {
    "address1": "123 Main St",
    "address2": "PO Box 123",
    "city": "Anywhere",
    "state": "CA",
    "zip": 12345
  }
}

To this PHP code block:

$data = array();
$data["my_index"] = 1;
$data["first_name"] = "John";
$data["last_name"] = "Smith";

$data["address"] = array();
$data["address"]["address1"] = "123 Main St";
$data["address"]["address2"] = "PO Box 123";
$data["address"]["city"] = "Anywhere";
$data["address"]["state"] = "CA";
$data["address"]["zip"] = 12345;

Basically, building code to paste into something else. I don't want a json_decode()'ed object. I literally want to end of with a string of PHP code, not a PHP object!

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Erick
  • 369
  • 3
  • 13
  • 2
    Possible duplicate of [json\_decode to array](https://stackoverflow.com/questions/5164404/json-decode-to-array) then var_export it. – Andreas May 16 '19 at 06:19
  • NO. That simply creates a PHP object, not a string of code. Do people even read things on here anymore before down-voting or marking as a duplicate without even reading the full post? – Erick May 16 '19 at 06:22
  • 1
    OP: *Those down-voting or suggesting using json_decode() are not paying attention. I literally want to end of with a string of PHP code, not a PHP object!*. And you have not made **any** attempts in solving your question. So downvotes are valid. – Andreas May 16 '19 at 06:23

2 Answers2

1

Not 100% the same as what your after, but it effectively creates a piece of PHP code which you can use. The main thing is to decode it to a PHP array and then use var_export() to output the resultant array. Add a bit of boilerplate round it to give some code...

$data='{
  "my_index": 1,
  "first_name": "John",
  "last_name": "Smith",
  "address": {
    "address1": "123 Main St",
    "address2": "PO Box 123",
    "city": "Anywhere",
    "state": "CA",
    "zip": 12345
  }
}';
echo '$data = '.var_export(json_decode($data, true), true).';';

gives you

$data = array (
  'my_index' => 1,
  'first_name' => 'John',
  'last_name' => 'Smith',
  'address' => 
  array (
    'address1' => '123 Main St',
    'address2' => 'PO Box 123',
    'city' => 'Anywhere',
    'state' => 'CA',
    'zip' => 12345,
  ),
);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • 1
    Not exactly, but I'll give you the answer just to spite those above in the comments. :-) – Erick May 16 '19 at 06:59
1
$string = '{
    "my_index": 1,
    "first_name": "John",
    "last_name": "Smith",
    "address": {
        "address1": "123 Main St",
        "address2": "PO Box 123",
        "city": "Anywhere",
        "state": "CA",
        "zip": 12345
    }
}';
$recursiveIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($string, true)), RecursiveIteratorIterator::SELF_FIRST);
$data = array('$data = array();');

foreach ($recursiveIterator as $key => $value) {
    $currentDepth = $recursiveIterator->getDepth();
    $keys = array();

    // Traverse up array to get keys
    for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) {
        $keys[] = $recursiveIterator->getSubIterator($subDepth)->key();
    }

    if (is_array($value)) {
        $data[] = '';
    }

    $data[] = '$data["' . implode('"]["', array_reverse($keys)) . '"] = ' . (!is_array($value) ? is_int($value) ? $value : '"' . $value . '"' : 'array()') . ';';
}

echo '<pre>';
print_r(implode("\n", $data));
echo '</pre>';
Erick
  • 369
  • 3
  • 13
  • That is awesome! That's why I didn't post example code because I was doing an old-school recursive function that was a mess and hadn't considered a recursive iterator. Perfect solution! Thank you, owe you a beer! – Erick May 16 '19 at 13:48