1

I have strings like this:

$T = '[{"X":"13","Y":"76", "R":"90"},{"X":"12","Y":"24","R":"-19"}]}';

$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
    {"X":"12","Y":"24","R":"-19"}]}';

I want to find each X value in the string, and get the value after them.

Note that the number of X values differs.

For example, I want:

13, 12, 1, 1, 12

I tried explode(), but that only grabbed the first X value (13). I am new to PHP, and don't know an elegant solution. Any suggestions are appreciated.

John
  • 93
  • 2
  • 8

1 Answers1

0

Remove the last curly brace (}) of your strings to have valid json, i.e:

$A = '[{"X":"1","Y":"6", "R":"0"},{"X":"1","Y":"4","R":"9"},
    {"X":"12","Y":"24","R":"-19"}]}';
$A = trim($A, '}');
$json = json_decode($A, true);
foreach($json as $value)
{
    echo $value['X'];
}
# 1112
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268