Задать вопрос
@V9821

Как вытащить url из массива?

Как можно вытащить из этого массива url картинки с размером 604 в мой код?
{
    "response": {
        "count": 10,
        "items": [
            {
                "id": 76,
                "from_id": -206341834,
                "owner_id": -206341834,
                "date": 1635582068,
                "marked_as_ads": 0,
                "post_type": "post",
                "text": "",
                "attachments": [
                    {
                        "type": "photo",
                        "photo": {
                            "album_id": -7,
                            "date": 1635582068,
                            "id": 457239054,
                            "owner_id": -206341834,
                            "has_tags": false,
                            "access_key": "76e2c3a835f5a4cc1d",
                            "post_id": 76,
                            "sizes": [
                                {
                                    "height": 21,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=75x21&quality=96&sign=8aa84cb6975e4de920245e769cf2a53f&c_uniq_tag=sLINAUcU8unP7Fy0u6LVFVW6okvRhFVqd0pvQDOLMCQ&type=album",
                                    "type": "s",
                                    "width": 75
                                },
                                {
                                    "height": 37,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=130x37&quality=96&sign=85670086f0d1fc48cebffb19fd6051f8&c_uniq_tag=aqj7l9zkLyurmUipyH7hnOQa--cgtKEpPCfAZLoAT7g&type=album",
                                    "type": "m",
                                    "width": 130
                                },
                                {
                                    "height": 172,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=604x172&quality=96&sign=411f8b44e840be3e3fab13f8a571e573&c_uniq_tag=n7WU0f_Z0U2l5vQcZ_oJvCjl_dylcHdtp27_skvzOM4&type=album",
                                    "type": "x",
                                    "width": 604
                                },
                                {
                                    "height": 230,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=807x230&quality=96&sign=4f7adb4ccbc1356e93242b37bee5f3d8&c_uniq_tag=Mom16YuevSy4N3BUQjpWWP_rxgd8_GoK04x5DDCsdfg&type=album",
                                    "type": "y",
                                    "width": 807
                                },
                                {
                                    "height": 286,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=1004x286&quality=96&sign=6294e717d879a6c6817ffa63cc88a2c0&c_uniq_tag=RjM7SnmnqAmOhbaSMGlyuejYT57m8qiuSN4Dlh6zpmU&type=album",
                                    "type": "z",
                                    "width": 1004
                                },
                                {
                                    "height": 87,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=130x87&quality=96&crop=288,0,427,286&sign=597fc3aa018be2fe2ec5d4a9ac0451fd&c_uniq_tag=T-CKMuVm406BtIZw98ZJ_NxkdoupFQMqRCa1V7drcE4&type=album",
                                    "type": "o",
                                    "width": 130
                                },
                                {
                                    "height": 133,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=200x133&quality=96&crop=287,0,430,286&sign=6188ec41453036d893de32ccf7141cf4&c_uniq_tag=t68y12R5nhRVKPoJrPgpReIZZF0j17v9Y1yUqxe6fUk&type=album",
                                    "type": "p",
                                    "width": 200
                                },
                                {
                                    "height": 213,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=320x213&quality=96&crop=287,0,430,286&sign=7b203f726f6166072fa985d75b30a86e&c_uniq_tag=fYQ_zJaFaJLw2QQu1WzvZkpXZ9DABoEZR9rk12wmV-4&type=album",
                                    "type": "q",
                                    "width": 320
                                },
                                {
                                    "height": 286,
                                    "url": "https://sun9-41.userapi.com/impg/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA/TVvXcAsnENA.jpg?size=510x286&quality=96&crop=247,0,510,286&sign=73773ebeaa1b32614f363e09077d49e6&c_uniq_tag=OVXA90aetOhzt8HzcwRD95eRbUPUwNvK75VG2_ENqWY&type=album",
                                    "type": "r",
                                    "width": 510
                                }
                            ],
                            "text": "",
                            "user_id": 100
                        }
                    }
                ],
                "post_source": {
                    "type": "vk"
                },
                "comments": {
                    "can_post": 1,
                    "count": 0,
                    "groups_can_post": true
                },
                "likes": {
                    "can_like": 1,
                    "count": 0,
                    "user_likes": 0,
                    "can_publish": 1
                },
                "reposts": {
                    "count": 0,
                    "user_reposted": 0
                },
                "donut": {
                    "is_donut": false
                },
                "short_text_rate": 0.8,
                "hash": "80fJag8LLqgU0VXNjw"
            }
        ]
    }
}

мой код

foreach($news['items'] as $n) {
	$id++;
	foreach($n['attachments'] as $item) {
		$img = $item['photo']['photo_604']; 
	}
  • Вопрос задан
  • 108 просмотров
Подписаться 1 Простой Комментировать
Решения вопроса 1
@Verkan
<?php //php 7.2.24

$json = '{"response":{"count":10,"items":[{"id":76,"from_id":-206341834,"owner_id":-206341834,"date":1635582068,"marked_as_ads":0,"post_type":"post","text":"","attachments":[{"type":"photo","photo":{"album_id":-7,"date":1635582068,"id":457239054,"owner_id":-206341834,"has_tags":false,"access_key":"76e2c3a835f5a4cc1d","post_id":76,"sizes":[{"height":21,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=75x21&quality=96&sign=8aa84cb6975e4de920245e769cf2a53f&c_uniq_tag=sLINAUcU8unP7Fy0u6LVFVW6okvRhFVqd0pvQDOLMCQ&type=album","type":"s","width":75},{"height":37,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=130x37&quality=96&sign=85670086f0d1fc48cebffb19fd6051f8&c_uniq_tag=aqj7l9zkLyurmUipyH7hnOQa--cgtKEpPCfAZLoAT7g&type=album","type":"m","width":130},{"height":172,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=604x172&quality=96&sign=411f8b44e840be3e3fab13f8a571e573&c_uniq_tag=n7WU0f_Z0U2l5vQcZ_oJvCjl_dylcHdtp27_skvzOM4&type=album","type":"x","width":604},{"height":230,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=807x230&quality=96&sign=4f7adb4ccbc1356e93242b37bee5f3d8&c_uniq_tag=Mom16YuevSy4N3BUQjpWWP_rxgd8_GoK04x5DDCsdfg&type=album","type":"y","width":807},{"height":286,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=1004x286&quality=96&sign=6294e717d879a6c6817ffa63cc88a2c0&c_uniq_tag=RjM7SnmnqAmOhbaSMGlyuejYT57m8qiuSN4Dlh6zpmU&type=album","type":"z","width":1004},{"height":87,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=130x87&quality=96&crop=288,0,427,286&sign=597fc3aa018be2fe2ec5d4a9ac0451fd&c_uniq_tag=T-CKMuVm406BtIZw98ZJ_NxkdoupFQMqRCa1V7drcE4&type=album","type":"o","width":130},{"height":133,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=200x133&quality=96&crop=287,0,430,286&sign=6188ec41453036d893de32ccf7141cf4&c_uniq_tag=t68y12R5nhRVKPoJrPgpReIZZF0j17v9Y1yUqxe6fUk&type=album","type":"p","width":200},{"height":213,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=320x213&quality=96&crop=287,0,430,286&sign=7b203f726f6166072fa985d75b30a86e&c_uniq_tag=fYQ_zJaFaJLw2QQu1WzvZkpXZ9DABoEZR9rk12wmV-4&type=album","type":"q","width":320},{"height":286,"url":"https:\/\/sun9-41.userapi.com\/impg\/Jl2qOYk10GNB5SfzzpiHksZYSC4rw9sQRR7oFA\/TVvXcAsnENA.jpg?size=510x286&quality=96&crop=247,0,510,286&sign=73773ebeaa1b32614f363e09077d49e6&c_uniq_tag=OVXA90aetOhzt8HzcwRD95eRbUPUwNvK75VG2_ENqWY&type=album","type":"r","width":510}],"text":"","user_id":100}}],"post_source":{"type":"vk"},"comments":{"can_post":1,"count":0,"groups_can_post":true},"likes":{"can_like":1,"count":0,"user_likes":0,"can_publish":1},"reposts":{"count":0,"user_reposted":0},"donut":{"is_donut":false},"short_text_rate":0.800000,"hash":"80fJag8LLqgU0VXNjw"}]}}';

$data = json_decode($json);

$sizes = $data->response->items[0]->attachments[0]->photo->sizes;
    
    foreach ($sizes as $size){
        if($size->width === 604){
            $url = $size->url;
            break;
        }
    }

    echo $url ?? null
?>
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы