Home > OS >  get key from JSON string with square brackets php
get key from JSON string with square brackets php

Time:04-01

I have an array:

[{"title":" \ud83c\uddfa\ud83c\udde6 \u041b\u0443\u0447\u0448\u0435\u0435 \u043a\u0430\u0437\u0438\u043d\u043e \u0423\u043a\u0440\u0430\u0438\u043d\u044b \ud83c\uddfa\ud83c\udde6 ","desc":"50 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0445 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u0437\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e!","icn":"https://2.mbvnclick6.com/ic?sid=9&data=0TC0cpTWi6iiAYRq5etiA8TwHo6uLuZWVMRoqiXe/7gsKd4jpbEqYGDPXPaf4W3ziKmVovALZeVZjNLB7yucI7HNQL/wXfbYgtPG0sueOLkw2GIJObKgRybxfII5+60KZ0YbqeDvNpWJ5LlabIewv8oH9d714K4JQysNKHnAlDmWcjDqN8L+uKr9bk6C8443lu4pKG6kA07t+VTisQcJ1aoAL2KBBEZPohza5RyEAXP2Xqvw7c+usJuoGZ1yFhM/","img":"https://2.mbvnclick6.com/im?sid=9&data=1rKVNc6AXz6iYQn9cjy9P/Lf4HZAeikPaYO/sHkHO5T7xg6BQ9IglRCgDMiiGpkm518POj2/bY1XTe2SoCQIOEZOkD21ClIjtm28IfmW04Te9vAxxJLINx0jsBz5rkc5ySHki7BrYTXFfrSj+HGuFVrXiaWK4KgHB637vnrM39IlG08uJqhAS2bDCNMMyQRafEu1SzDQo8VCdnraZ2RzRjSOYbfMk3MVy6Kp66o+kgF8DBzMvI6g7trQx/1sKv/L","url":"https://2ch.mbvnclick6.com?sid=9&d=2&data=8m1BQpjNMT66FgtgYOqMgbBK5dPsbIf9oZ+3DAdiww1mjhvVjzWMvf8Qu0ZIDfIvpy0+22hgzz0BxqCIY1ZlM//JlE5ahWzlZql3PdgKt8BdISWI4IHogQHO+Y9s52sDXFxXC6PEAh6KaNDyoZLkOR4HeKxakB9KWcWiQ0nuZNhJbVdHn9JPeMDhI2xEzXLnHwAUpsyzPi43HV1L1K1c2oi8wrXVAQsp11kA2LJDgF/FJAy/3jz4OCm7IFsQlfP7Vd5qBsFpX9yg6g5sj3aqfiayIZk3CqRm18addVnM2mqcE7VavPzB7JEVmETIefBF7DeY+tMXFv56nF4Y714XpdTl0b+0negf9uZo0rRprqjTM8POOkFIk00E3ZakcbwOXA+S5e4FHkK4IdVZas+lvmS87rXll2ZRKw8MRlOqnbals/hZh9pBqxtDTk4eADJU7xZTqZuXmcq1YwnuwrEV98Q97x3NUHpGn/dsRoM6zg8=","price":"0.0055"}]

I tried to get for example title by using <?php echo $json['title']; ?>. It didn't help. I think that it's because of that square brackets [. It's like array in array. How can I get value of title?

CodePudding user response:

you should access the 0 index of your array first! $json[0] contains the main object you want!

try this:

$txt = '[{"title":" \ud83c\uddfa\ud83c\udde6 \u041b\u0443\u0447\u0448\u0435\u0435 \u043a\u0430\u0437\u0438\u043d\u043e \u0423\u043a\u0440\u0430\u0438\u043d\u044b \ud83c\uddfa\ud83c\udde6 ","desc":"50 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0445 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u0437\u0430 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044e!","icn":"https://2.mbvnclick6.com/ic?sid=9&data=0TC0cpTWi6iiAYRq5etiA8TwHo6uLuZWVMRoqiXe/7gsKd4jpbEqYGDPXPaf4W3ziKmVovALZeVZjNLB7yucI7HNQL/wXfbYgtPG0sueOLkw2GIJObKgRybxfII5+60KZ0YbqeDvNpWJ5LlabIewv8oH9d714K4JQysNKHnAlDmWcjDqN8L+uKr9bk6C8443lu4pKG6kA07t+VTisQcJ1aoAL2KBBEZPohza5RyEAXP2Xqvw7c+usJuoGZ1yFhM/","img":"https://2.mbvnclick6.com/im?sid=9&data=1rKVNc6AXz6iYQn9cjy9P/Lf4HZAeikPaYO/sHkHO5T7xg6BQ9IglRCgDMiiGpkm518POj2/bY1XTe2SoCQIOEZOkD21ClIjtm28IfmW04Te9vAxxJLINx0jsBz5rkc5ySHki7BrYTXFfrSj+HGuFVrXiaWK4KgHB637vnrM39IlG08uJqhAS2bDCNMMyQRafEu1SzDQo8VCdnraZ2RzRjSOYbfMk3MVy6Kp66o+kgF8DBzMvI6g7trQx/1sKv/L","url":"https://2ch.mbvnclick6.com?sid=9&d=2&data=8m1BQpjNMT66FgtgYOqMgbBK5dPsbIf9oZ+3DAdiww1mjhvVjzWMvf8Qu0ZIDfIvpy0+22hgzz0BxqCIY1ZlM//JlE5ahWzlZql3PdgKt8BdISWI4IHogQHO+Y9s52sDXFxXC6PEAh6KaNDyoZLkOR4HeKxakB9KWcWiQ0nuZNhJbVdHn9JPeMDhI2xEzXLnHwAUpsyzPi43HV1L1K1c2oi8wrXVAQsp11kA2LJDgF/FJAy/3jz4OCm7IFsQlfP7Vd5qBsFpX9yg6g5sj3aqfiayIZk3CqRm18addVnM2mqcE7VavPzB7JEVmETIefBF7DeY+tMXFv56nF4Y714XpdTl0b+0negf9uZo0rRprqjTM8POOkFIk00E3ZakcbwOXA+S5e4FHkK4IdVZas+lvmS87rXll2ZRKw8MRlOqnbals/hZh9pBqxtDTk4eADJU7xZTqZuXmcq1YwnuwrEV98Q97x3NUHpGn/dsRoM6zg8=","price":"0.0055"}]';
$json = json_decode($txt);
$json = $json[0];
echo $json->title;
  • Related