简体   繁体   English

如何获取此关联数组中键的值

[英]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.我有一个 CMS 我正在使用它在数据库中序列化他们的数据。 I used the unserialize() function to convert the data into an associative array.我使用 unserialize() 函数将数据转换为关联数组。 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 循环:

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" ["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.我建议您在 php 中使用 mysqli 或其他数据库适配器,因为 mysql_ 函数在从未使用的 php 版本中被弃用甚至删除。 But according to your code, you could simply use a foreach loop to get your data.但是根据您的代码,您可以简单地使用 foreach 循环来获取数据。

Your results gives back some kind of data groups, I assume those are the user ids or something similar.您的结果会返回某种数据组,我假设这些是用户 ID 或类似的东西。 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:只获取最新的 id,以防它们用于某种版本控制:

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'];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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