1

let me make this quick. I have this array :

$newdata = array(2)
    {[0] => array(1)
        {["data"] => string "{"A":2, "B":5, "C":[{"X":12, "Y":15},{"X":13, "Y":18}]}"}
    [1] => array(1)
        {["data"] => string "{"A":5, "B":2, "C":[{"X":11, "Y":17},{"X":18, "Y":14}]}"}}

How I can access A, B, C and X and Y inside it? I checked it with var_dump(), the result :

var_dump($newdata) is all of the array above
var_dump($newdata[0]) is the first array

Now this is the problem

I want to print element A in first array with :

var_dump($newdata[0]["A"]) -> NULL
var_dump($newdata[0]->A) -> NULL

And so when I try to print inside C :

var_dump($newdata[0]["A"][0]) -> NULL
var_dump($newdata[0]->A[0]) -> NULL

How I can access it? Any help most appreciated.

Here is a var_export() of $newdata

array (
    0 => array ('data' => '{"A":2, "B":5, "C":[{"X":12, "Y":15],{"X":13, "Y":18}]}'),
    1 => array('data' => '{"A":5, "B":2, "C":[{"X":11, "Y":17],{"X":18, "Y":14}]}'}
Rayan Suryadikara
  • 237
  • 1
  • 3
  • 17
  • Can you show an `echo var_export($newdata);` please – RiggsFolly May 08 '17 at 08:33
  • 1
    The 'data' part of your array looks like JSON, so you need to [`json_decode()`](https://secure.php.net/manual/en/function.json-decode.php) it before you can use it. **But it is malformed JSON!**. So you cannot decode it. – Tom Udding May 08 '17 at 08:36
  • That's a malformed JSON string. You can't easily acces the values. – Imanuel May 08 '17 at 08:36
  • 1
    please give me the corrent array because its shwoing Parse error: syntax error, unexpected '{' in E:\wamp\www\test\index.php on line 5; – rowmoin May 08 '17 at 08:37
  • Can you show an var_dump($newdata) – rowmoin May 08 '17 at 08:40
  • @RiggsFolly array (0 => array ('data' => '{"A":2, "B":5, "C":[{"X":12, "Y":15],{"X":13, "Y":18}]}'),1 => array('data' => '{"A":5, "B":2, "C":[{"X":11, "Y":17],{"X":18, "Y":14}]}'} – Rayan Suryadikara May 08 '17 at 08:40
  • The JSON String is malformed. Therefore you cannot decode it to anything useful to PHP – RiggsFolly May 08 '17 at 08:44
  • How did you create those JSON Strings? – RiggsFolly May 08 '17 at 08:44
  • That ain't gonna work `var_dump($newdata[0]["A"])` . You skipped `"data"` index. The correct one should be like that `var_dump($newdata[0]['data']["A"])` (but i think first you should decode the json to an array) – Antonios Tsimourtos May 08 '17 at 08:51
  • your JSON data is malformed could you please just check that data { "A": "2", "B": "5", "C": [{ "X": "12", "Y": "15"], { "X": "13", "Y": "18" }] } in https://jsonlint.com/?code= then i think you will be get it. so you need to fixed your array there are lot of mistake – rowmoin May 08 '17 at 09:09
  • first of all your string is invalid json format – Beginner May 08 '17 at 09:11

2 Answers2

0

These are JSON strings in your array. You need to decode them.

This should give you an idea:

$data = json_decode($newdata[0]['data'], true);
var_dump($data);
var_dump($data['A']);

(This only works with your corrected data in the first code block. The other examples are still broken.)

Imanuel
  • 3,596
  • 4
  • 24
  • 46
0

It's because your string is not a correct json string. The right way is to get to know why it's in the wrong format. Refer to this post for json parsing.

Here is a work around. Check the live demo.

In this code $string is your $newdata[0]['data'];

<?php
$string = '"{"A":2, "B":5, "C":[{"X":12, "Y":15],{"X":13, "Y":18}]}"';
$string = trim($string, '"');
$string[strpos($string, ']')] = '}';
print_r(json_decode($string, true));
Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295
  • so trim and strpos is essential given with JSON format above? – Rayan Suryadikara May 08 '17 at 08:55
  • No. your string is not json format. I just trans your non json sting to json. Then pare the json string. You can use online json checking tools to verify if your string is in the right json format. – LF00 May 08 '17 at 09:07