Ad

Get Key From JSON String With Square Brackets Php

- 1 answer

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%2F7gsKd4jpbEqYGDPXPaf4W3ziKmVovALZeVZjNLB7yucI7HNQL%2FwXfbYgtPG0sueOLkw2GIJObKgRybxfII5%2B60KZ0YbqeDvNpWJ5LlabIewv8oH9d714K4JQysNKHnAlDmWcjDqN8L%2BuKr9bk6C8443lu4pKG6kA07t%2BVTisQcJ1aoAL2KBBEZPohza5RyEAXP2Xqvw7c%2BusJuoGZ1yFhM%2F","img":"https://2.mbvnclick6.com/im?sid=9&data=1rKVNc6AXz6iYQn9cjy9P%2FLf4HZAeikPaYO%2FsHkHO5T7xg6BQ9IglRCgDMiiGpkm518POj2%2FbY1XTe2SoCQIOEZOkD21ClIjtm28IfmW04Te9vAxxJLINx0jsBz5rkc5ySHki7BrYTXFfrSj%2BHGuFVrXiaWK4KgHB637vnrM39IlG08uJqhAS2bDCNMMyQRafEu1SzDQo8VCdnraZ2RzRjSOYbfMk3MVy6Kp66o%2BkgF8DBzMvI6g7trQx%2F1sKv%2FL","url":"https://2ch.mbvnclick6.com?sid=9&d=2&data=8m1BQpjNMT66FgtgYOqMgbBK5dPsbIf9oZ%2B3DAdiww1mjhvVjzWMvf8Qu0ZIDfIvpy0%2B22hgzz0BxqCIY1ZlM%2F%2FJlE5ahWzlZql3PdgKt8BdISWI4IHogQHO%2BY9s52sDXFxXC6PEAh6KaNDyoZLkOR4HeKxakB9KWcWiQ0nuZNhJbVdHn9JPeMDhI2xEzXLnHwAUpsyzPi43HV1L1K1c2oi8wrXVAQsp11kA2LJDgF%2FFJAy%2F3jz4OCm7IFsQlfP7Vd5qBsFpX9yg6g5sj3aqfiayIZk3CqRm18addVnM2mqcE7VavPzB7JEVmETIefBF7DeY%2BtMXFv56nF4Y714XpdTl0b%2B0negf9uZo0rRprqjTM8POOkFIk00E3ZakcbwOXA%2BS5e4FHkK4IdVZas%2BlvmS87rXll2ZRKw8MRlOqnbals%2FhZh9pBqxtDTk4eADJU7xZTqZuXmcq1YwnuwrEV98Q97x3NUHpGn%2FdsRoM6zg8%3D","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?

Ad

Answer

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%2F7gsKd4jpbEqYGDPXPaf4W3ziKmVovALZeVZjNLB7yucI7HNQL%2FwXfbYgtPG0sueOLkw2GIJObKgRybxfII5%2B60KZ0YbqeDvNpWJ5LlabIewv8oH9d714K4JQysNKHnAlDmWcjDqN8L%2BuKr9bk6C8443lu4pKG6kA07t%2BVTisQcJ1aoAL2KBBEZPohza5RyEAXP2Xqvw7c%2BusJuoGZ1yFhM%2F","img":"https://2.mbvnclick6.com/im?sid=9&data=1rKVNc6AXz6iYQn9cjy9P%2FLf4HZAeikPaYO%2FsHkHO5T7xg6BQ9IglRCgDMiiGpkm518POj2%2FbY1XTe2SoCQIOEZOkD21ClIjtm28IfmW04Te9vAxxJLINx0jsBz5rkc5ySHki7BrYTXFfrSj%2BHGuFVrXiaWK4KgHB637vnrM39IlG08uJqhAS2bDCNMMyQRafEu1SzDQo8VCdnraZ2RzRjSOYbfMk3MVy6Kp66o%2BkgF8DBzMvI6g7trQx%2F1sKv%2FL","url":"https://2ch.mbvnclick6.com?sid=9&d=2&data=8m1BQpjNMT66FgtgYOqMgbBK5dPsbIf9oZ%2B3DAdiww1mjhvVjzWMvf8Qu0ZIDfIvpy0%2B22hgzz0BxqCIY1ZlM%2F%2FJlE5ahWzlZql3PdgKt8BdISWI4IHogQHO%2BY9s52sDXFxXC6PEAh6KaNDyoZLkOR4HeKxakB9KWcWiQ0nuZNhJbVdHn9JPeMDhI2xEzXLnHwAUpsyzPi43HV1L1K1c2oi8wrXVAQsp11kA2LJDgF%2FFJAy%2F3jz4OCm7IFsQlfP7Vd5qBsFpX9yg6g5sj3aqfiayIZk3CqRm18addVnM2mqcE7VavPzB7JEVmETIefBF7DeY%2BtMXFv56nF4Y714XpdTl0b%2B0negf9uZo0rRprqjTM8POOkFIk00E3ZakcbwOXA%2BS5e4FHkK4IdVZas%2BlvmS87rXll2ZRKw8MRlOqnbals%2FhZh9pBqxtDTk4eADJU7xZTqZuXmcq1YwnuwrEV98Q97x3NUHpGn%2FdsRoM6zg8%3D","price":"0.0055"}]';
$json = json_decode($txt);
$json = $json[0];
echo $json->title;
Ad
source: stackoverflow.com
Ad