3

I have got this array from the Android developer whom I am working with and I have to get the values of the key name from the following array:

[ 
   { 
      "data":"[{\"name\":\"step 1 kdfhghdkgjdf\\nkjdhfgkjhdkjghd\\nkdfjhgkjdhfg\\n\\n\\ndfjhgkjdfjhgdfgd\\n\"},{\"name\":\"step 2 dhfgkjdfhkhkjchjkfd\\ndkjhjdf\\njhkdfhkghdkfhgkdhg\\n\\n\\ndfjhgkjdfhgdfhgkjdhfgkjhdf\"},{\"name\":\"step 3 kkkkkkkkkk\"},{\"name\":\"step 4 ljlejrhlflhgf\\n\\n\\ndfhjk\"}]",
      "status":1
   }
]

I have tried doing the following:

 <?php
    $s = '[
  {
    "data": "[{\"name\":\"step 1 kdfhghdkgjdf\\nkjdhfgkjhdkjghd\\nkdfjhgkjdhfg\\n\\n\\ndfjhgkjdfjhgdfgd\\n\"},{\"name\":\"step 2 dhfgkjdfhkhkjchjkfd\\ndkjhjdf\\njhkdfhkghdkfhgkdhg\\n\\n\\ndfjhgkjdfhgdfhgkjdhfgkjhdf\"},{\"name\":\"step 3 kkkkkkkkkk\"},{\"name\":\"step 4 ljlejrhlflhgf\\n\\n\\ndfhjk\"}]",
    "status": 1
  }
]';
    $obj = json_decode($s,true);
    echo $obj[0]['data']
    ?> 

Which gives me following output:

[
  {
    "name": "step 1 kdfhghdkgjdf kjdhfgkjhdkjghd kdfjhgkjdhfg dfjhgkjdfjhgdfgd "
  },
  {
    "name": "step 2 dhfgkjdfhkhkjchjkfd dkjhjdf jhkdfhkghdkfhgkdhg dfjhgkjdfhgdfhgkjdhfgkjhdf"
  },
  {
    "name": "step 3 kkkkkkkkkk"
  },
  {
    "name": "step 4 ljlejrhlflhgf dfhjk"
  }
]

But I want just the values of the key name like:

step 1 kdfhghdkgjdf kjdhfgkjhdkjghd kdfjhgkjdhfg dfjhgkjdfjhgdfgd
step 2 dhfgkjdfhkhkjchjkfd dkjhjdf jhkdfhkghdkfhgkdhg dfjhgkjdfhgdfhgkjdhfgkjhdf
step 3 kkkkkkkkkk
.
.
.

My question is similar to this one: Get value from JSON array in PHP except the format is different.

Can I get the values in this format? If so, how? If not, is the format incorrect?

11
  • 5
    Tell the Android dev to give json data correctly again. This is of course valid JSON but it looks like JSON inside JSON. I am telling this because he/she is making things unnecessarily complicated on server side. Commented Sep 25, 2019 at 15:23
  • What do you mean "you wan the values of the key name"? Would an array with each index being one of those strings suffice? Commented Sep 25, 2019 at 15:46
  • 1
    A big part of the issue here is that (some of) the data is double-encoded. That needs to be fixed by the application sending the data. It's not the server's job to deal with that kind of issue. Get them to send one coherent JSON object, not this silly JSON-within-JSON format Commented Sep 25, 2019 at 15:46
  • 1
    @Shubham the data in the updated part is exactly the same as on the top. What the difference? Commented Sep 25, 2019 at 15:55
  • 1
    Even following the edit, the basic point still stands. The data is still JSON-within-JSON and it still needs correcting at source to make it all one single object, so that it's simple and predictable to deal with. Speak to your android developer and explain the situation. Commented Sep 25, 2019 at 16:11

2 Answers 2

3

Assuming $obj[0]['data'] actually has the JSON that you posted, just decode and extract the name columns:

foreach(array_column(json_decode($obj[0]['data'], true), 'name') as $name) {
    echo $name;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes... although IMHO it would be better to have the data sent as a single object to begin with
2

First of all you have not a json-structure inside the "data" field, but just a string, which contains json data.

Therefore you did it wrong, when convert the data into a constant value. You have to double all backslashes first.

Then you can get the "data" element and perform json_decode once more.

<?php
    $s = '[
  {
    "data": "[{\\"name\\":\\"step 1 kdfhghdkgjdf\\\\nkjdhfgkjhdkjghd\\\\nkdfjhgkjdhfg\\\\n\\\\n\\\\ndfjhgkjdfjhgdfgd\\\\n\\"},{\\"name\\":\\"step 2 dhfgkjdfhkhkjchjkfd\\\\ndkjhjdf\\\\njhkdfhkghdkfhgkdhg\\\\n\\\\n\\\\ndfjhgkjdfhgdfhgkjdhfgkjhdf\\"},{\\"name\\":\\"step 3 kkkkkkkkkk\\"},{\\"name\\":\\"step 4 ljlejrhlflhgf\\\\n\\\\n\\\\ndfhjk\\"}]",
    "status": 1
  }
]';
    $obj = json_decode($s,true);
    $data = json_decode($obj[0]['data'], true);

    foreach($data as $item) {
      print($item['name'] . "\r\n");
    }

6 Comments

I posted the formatted JSON, please check the question again. I have edited the question.
@Shubham, that does not change my answer. When you put slashes into a string constant in php, you have to escape them by adding slashes.
The codes inside the answer doesn't give any output!
It does output each "name" item. Just have checked once again. Don't forget the <?php prefix
@MaroofMandal yes, it does. Demo: sandbox.onlinephpfunctions.com/code/… . however it would be altogether better if the data was in a sensible format when it was sent
|

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.