简体   繁体   中英

How can I get the value of a key in this associative array

I have a CMS I am using that serializes their data in the database. I used the unserialize() function to convert the data into an associative array. Now I am having a hard time pulling the value of the image from the associative array:

This is the simple while loop I am using to loop through the rows:

while($row = mysql_fetch_assoc($query_models)){

    $model_name = $row['ModelName'];
    $model_thumbnail = unserialize($row['info']);

}

this is the Key and Value of the array I need to get the value of, so I can assign the correct thumbnail image to the respected person:

["1x_filename"]=> string(19) "00/83/83-set-1x.jpg"

The full array is below, and the key I am targeting is located more at the bottom of this array:

array(1) { 
    ["thumbs"]=> array(2) { 
        [16]=> array(17) { 
            ["id"]=> string(2) "82" 
            ["1x_width"]=> string(3) "220" 
            ["1x_height"]=> string(3) "330" 
            ["2x_width"]=> string(3) "440" 
            ["2x_height"]=> string(3) "660" 
            ["3x_width"]=> string(3) "660" 
            ["3x_height"]=> string(3) "990" 
            ["4x_width"]=> string(3) "880" 
            ["4x_height"]=> string(4) "1320" 
            ["width"]=> string(3) "220" 
            ["height"]=> string(3) "330" 
            ["retinamode"]=> string(1) "1" 
            ["filename"]=> string(10) "82-set.jpg" 
            ["1x_filename"]=> string(19) "00/82/82-set-1x.jpg"
            ["2x_filename"]=> string(19) "00/82/82-set-2x.jpg"
            ["3x_filename"]=> string(19) "00/82/82-set-3x.jpg"
            ["4x_filename"]=> string(19) "00/82/82-set-4x.jpg" 
        } 
        [17]=> array(17) { 
            ["id"]=> string(2) "83" 
            ["1x_width"]=> string(3) "106" 
            ["1x_height"]=> string(3) "150" 
            ["2x_width"]=> string(3) "212" 
            ["2x_height"]=> string(3) "300" 
            ["3x_width"]=> string(3) "318" 
            ["3x_height"]=> string(3) "450" 
            ["4x_width"]=> string(3) "424" 
            ["4x_height"]=> string(3) "600" 
            ["width"]=> string(3) "106" 
            ["height"]=> string(3) "150" 
            ["retinamode"]=> string(1) "1" 
            ["filename"]=> string(10) "83-set.jpg" 
            ["1x_filename"]=> string(19) "00/83/83-set-1x.jpg" 
            ["2x_filename"]=> string(19) "00/83/83-set-2x.jpg" 
            ["3x_filename"]=> string(19) "00/83/83-set-3x.jpg" 
            ["4x_filename"]=> string(19) "00/83/83-set-4x.jpg"
        } 
    } 
}

Any help would be greatly appreciated. Thanks!

Just like this :

$model_thumbnail = unserialize($row['info']);
$picturePath = $model_thumbnail['thumbs'][17]['1x_filename'];

picturePath will contain your images path

I would recommend you to use mysqli or an other database adapter in php, as mysql_ functions are depricated and even removed in never php versions. But according to your code, you could simply use a foreach loop to get your data.

Your results gives back some kind of data groups, I assume those are the user ids or something similar. It could also be the version or something like it. As I do not know that here are two possible ways

Get all:

while($row = mysql_fetch_assoc($query_models)){

    $model_name = $row['ModelName'];
    $model_thumbnail = unserialize($row['info']);
    // echo all 1x_filename values from all given "ids"
    foreach ($model_thumbnail['thumbs'] as $model_id => $model_values) {
        print $model_values['1x_filename'];
    }

}

Get only the latest id, in case those are for some kind of versioning:

while($row = mysql_fetch_assoc($query_models)){

    $model_name = $row['ModelName'];
    $model_thumbnail = unserialize($row['info']);

    // sort array, so highest ID (assume-ably the newest)
    krsort($model_thumbnail['thumbs']);

    // get array entry
    $firstEntry = reset($model_thumbnail['thumbs']);
    print  $firstEntry['1x_filename'];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM