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?

Shubham
  • 43
  • 6
  • 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. – nice_dev Sep 25 '19 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? – Blake Morgan Sep 25 '19 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 – ADyson Sep 25 '19 at 15:46
  • @vivek_23 I've updated the question. Please check the question again. – Shubham Sep 25 '19 at 15:53
  • 1
    @Shubham the data in the updated part is exactly the same as on the top. What the difference? – AterLux Sep 25 '19 at 15:55
  • @BlakeMorgan I just want the values inside the array with the key `name` its name could have been anything like `xyz` or `abc`. I don't want the name of the key. I want its value. – Shubham Sep 25 '19 at 15:55
  • @AterLux previously, it was formatted. So the double backslashes were converted to a single backslash. – Shubham Sep 25 '19 at 15:56
  • 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. – ADyson Sep 25 '19 at 16:11
  • @ADyson will removing the brackets `[ ]` from the string (beginning and end) from the developer's end solve the problem? – Shubham Sep 25 '19 at 16:30
  • No. Not re-encoding that info as a separate bit of JSON and then adding it to the outer section as if it's a string will solve it. What you've effectively got here is a JSON object which has a property "data" within it. That property contains a string. So when you decide your JSON you get a string. The contents of that string look like JSON except with some extra escape characters added to deal with having quote marks inside a string. – ADyson Sep 25 '19 at 17:25
  • What I would guess your android dev is doing is encoding the list of names as a JSON string. He's then setting that string as a property of another object. Lastly he's then encoding that second object as JSON and sending it to you. This results in the original JSON string being escaped / double-encoded. What he needs to do is create one single object containing all the data, and then encode that whole object into JSON in a single step. That will create a sensible JSON object which he can send to you – ADyson Sep 25 '19 at 17:37

2 Answers2

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;
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Yes... although IMHO it would be better to have the data sent as a single object to begin with – ADyson Sep 25 '19 at 17:42
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");
    }
AterLux
  • 4,566
  • 2
  • 10
  • 13
  • I posted the formatted JSON, please check the question again. I have edited the question. – Shubham Sep 25 '19 at 15:39
  • @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. – AterLux Sep 25 '19 at 15:53
  • The codes inside the answer doesn't give any output! – Shubham Sep 25 '19 at 16:00
  • It does output each "name" item. Just have checked once again. Don't forget the ` – AterLux Sep 25 '19 at 16:49
  • @MaroofMandal yes, it does. Demo: http://sandbox.onlinephpfunctions.com/code/114c275463a248fcf3bfab3289deeb5c5070377e . however it would be altogether better if the data was in a sensible format when it was sent – ADyson Sep 25 '19 at 17:45
  • To do it this way you should use `addslashes` instead of manually adding them. – AbraCadaver Sep 25 '19 at 17:46