0

I have been stumped on this for hours. I need to get the initial key of the array and the id. However I am only getting 1 result returned back.

Below is example code and also here is a link - https://3v4l.org/HdMtA

In short the expected output should be.

key 11111111 id val_somevalue5555

key 2222222  id val_somevalue25

I am only getting one result.

key 11111111 id val_somevalue5555

.

$json = '[{
    "11111111": {
        "id": "val_somevalue5555",
        "customer": {
            "32312": {
                "name": "john doe"
            }
        }
    },
    "2222222": {
        "id": "val_somevalue25",
        "customer": {
            "32312234": {
                "name": "jane doe"
            }
        }
    }
}]';

$jsonarr = json_decode($json, true);

$newarr = [];

foreach($jsonarr as $value)
{
    $key = key($value);
    $newarr[] = ['key' => $key, 'id' => $value[$key]['id']];
}
var_dump($newarr);

Any help would be appreciated not sure if its the format of the json or what.

0

5 Answers 5

1

You are iterating the wrong thing.

Using print_r() to view the contents of the decoded array shows us that the thing you want to iterate over is - in fact - wrapped in another array.

print_r($jsonarr) returns this:

Array
(
    [0] => Array
        (
            [11111111] => Array
                (
                    [id] => val_somevalue5555
                    [customer] => Array
                        (
                            [32312] => Array
                                (
                                    [name] => jane doe
                                )

                        )

                )

            [2222222] => Array
                (
                    [id] => val_somevalue25
                    [customer] => Array
                        (
                            [32312234] => Array
                                (
                                    [name] => jane doe
                                )

                        )

                )

        )

)

So, what you have is a JSON object wrapped in JSON array with said object being the only item inside it.

You want to either:

a) Get rid of those [ and ] things at the beginning and the end of your JSON, or... (if you don't have control over that JSON)

b) Iterate the inner object (PHP represents it as associative array):

$jsonarr = json_decode($json, true);
$newarr = [];

foreach($jsonarr[0] as $key => $value) {
    $newarr[] = ['key' => $key, 'id' => $value['id']];
}`

var_dump($newarr);

Behold: https://3v4l.org/qCvRd

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you and also thank you for the explanation! Much appreciated!
0

Change your loop to use the key from the foreach loop itself: Like this

foreach($jsonarr as $key => $value)
{
   $newarr[] = ['key' => $key, 'id' => $value[$key]['id']]; //this seems off but what do I know.
}

PHP uses a copy ( or reference?) of the array for Foreach, it can produce some unexpected things to happen. It does some spooky voodoo stuff some times, if you ever try editing the array structure while using reference in foreach such as foreach($var as $k => &$v ) look out. I swear one time it was like phantom items in the array ... lol

Comments

0

You need to change in you json structure and then make necessary changes which I mentioned below

$json = '{
    "11111111": {
        "id": "val_somevalue5555",
        "customer": {
            "32312": {
                "name": "john doe"
            }
        }
    },
    "2222222": {
        "id": "val_somevalue25",
        "customer": {
            "32312234": {
                "name": "jane doe"
            }
        }
    }
}';

$jsonarr = json_decode($json, true);

$newarr = [];

foreach($jsonarr as $key=> $value)
{

    $newarr[] = ['key' => $key, 'id' => $value['id']];
}
var_dump($newarr);

Comments

0

Are you looking this o/p:

$json = '[{
    "11111111": {
        "id": "val_somevalue5555",
        "customer": {
            "32312": {
                "name": "john doe"
            }
        }
    },
    "2222222": {
        "id": "val_somevalue25",
        "customer": {
            "32312234": {
                "name": "jane doe"
            }
        }
    }
}]';

$jsonarr = json_decode($json, true);

$newarr = [];

foreach($jsonarr[0] as $key => $value)
{
    //echo "<br />".$key = key($value);
    $newarr[] = ['key' => $key, 'id' => $value['id']];
}
var_dump($newarr);

Output:

array(2) {
  [0]=>
  array(2) {
    ["key"]=>
    int(11111111)
    ["id"]=>
    string(17) "val_somevalue5555"
  }
  [1]=>
  array(2) {
    ["key"]=>
    int(2222222)
    ["id"]=>
    string(15) "val_somevalue25"
  }
}

Or you can change the json structure as ankit patel said and follow his code...

Comments

0

Add another foreach to loop the elements of [0] if you will have more than this.

foreach($jsonarr as $value)
{
    foreach($value as $key => $val){
        $newarr[] = ['key' => $key, 'id' => $val['id']];    
    }
}

Result

array(2) {
  [0]=>
  array(2) {
    ["key"]=>
    int(11111111)
    ["id"]=>
    string(17) "val_somevalue5555"
  }
  [1]=>
  array(2) {
    ["key"]=>
    int(2222222)
    ["id"]=>
    string(15) "val_somevalue25"
  }
}

Test here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.