0

I got this JSON which I need to convert to associative PHP array

[
    {
        "32": {
            "name": "Price File Jan 2022.xlsx",
            "datastartingrow": "1"
        }
    },
    {
        "33": {
            "name": "Price File Feb 2022.xlsx",
            "datastartingrow": "2"
        }
    }
]

$decoded = json_decode($json, true) creates unnecessary arrays [0] and [1], so I can't access $decoded[32] without double looping.

Array
(
    [0] => Array
        (
            [32] => Array
                (
                    [name] => Price File Jan 2022.xlsx
                    [datastartingrow] => 1
                )

        )

    [1] => Array
        (
            [33] => Array
                (
                    [name] => Price File Feb 2022.xlsx
                    [datastartingrow] => 2
                )

        )

)

Is there an elegant way of accessing each array by using key [32] and [33]?

Thanks.

Alex G
  • 3,048
  • 10
  • 39
  • 78
  • It's not json_decode that "adds" those arrays. That's the structure of the JSON you're decoding. You access them like you would any array: `$array[0][32]` and `$array[1][33]`. https://3v4l.org/YkDeg – M. Eriksson Feb 09 '22 at 18:02
  • Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – M. Eriksson Feb 09 '22 at 18:05
  • Your structure is the problem, you have a top level array with two items so of course you will see 0 & 1 indexes and by passing `assoc=true` you will of course see the nested array, so php is working as intended, You may need to change the structure of the json. – Dev Man Feb 09 '22 at 18:07
  • @DevMan: can you propose a structure? – Alex G Feb 09 '22 at 18:16
  • 1
    How is the JSON generated? A better structure would probably be: `{ "32": {"name":....}, "33": {"name": ...} }`. Then you would be able to access them like: `$array['32']` and `$array['33']`. – M. Eriksson Feb 09 '22 at 18:20
  • @M.Eriksson: still creates one extra array on the top, which I can eliminate by using `json_decode(....)[0]`. Post it as an answer and I will accept. – Alex G Feb 09 '22 at 18:26
  • @Alex G: Are you looking for access to the arrays with name and datastartingrow without knowing the keys 32 and 33? – jspit Feb 09 '22 at 18:31
  • Not if you structure it as I suggested. It should not be inside `[ ]`: https://3v4l.org/gRi74 – M. Eriksson Feb 09 '22 at 18:35
  • If you have control over the JSON, and assuming 32 and 33 are arbitrary id's, then I would create my JSON as follows: `[{ "id":"32", "name":....}, {"id":"33", "name": ...} }]`. – Vincent Nikkelen Feb 09 '22 at 18:39

2 Answers2

1

Somthing like this should be appropriate for a start

<?php
$data = '{"32":{"name":"Price File Jan 2022.xlsx","datastartingrow":"1"},"33":{"name":"Price File Feb 2022.xlsx","datastartingrow":"2"}}';

$decoded = json_decode($data, true);

foreach($decoded as $key => $element){
  echo 'element key is '.$key.' and item is '. json_encode($element). ' |||||||||||||||||| ' ;
}

?>

Check out the live example here

Dev Man
  • 2,114
  • 3
  • 23
  • 38
1
$jsonStr = '[
    {
        "32": {
            "name": "Price File Jan 2022.xlsx",
            "datastartingrow": "1"
        }
    },
    {
        "33": {
            "name": "Price File Feb 2022.xlsx",
            "datastartingrow": "2"
        }
    }
]';
$decoded = json_decode($jsonStr,true);

Direct access to the array with key 32:

$a32 = array_column($decoded,32)[0];
/*
array (
  'name' => "Price File Jan 2022.xlsx",
  'datastartingrow' => "1",
)
*/

Similarly for key 33

$a33 = array_column($decoded,33)[0];
jspit
  • 7,276
  • 1
  • 9
  • 17