无法在 PHP 中获取简单数组的数组值


can not get array value of a simple array in php

$color_hash 是一个 stdObject

stdClass Object
(
    [07] => GRAY
    [67] => BLUE
)

print_r($color_hash); 返回

stdClass Object ( [07] => GRAY [67] => BLUE )

$color_hash转换为数组

$colour_hash_array = (array)$colour_hash;

返回

Array
(
    [07] => GRAY
    [67] => BLUE
)

var_dump($colour_hash_array);

返回

array(2) { ["07"]=> string(4) "GRAY" ["67"]=> string(4) "BLUE" }

我尝试

log_me($colour_hash_array['07']);//Return GRAY
log_me($colour_hash_array['67']);//Return empty. HERE IS THE PROBLEM

function log_me($message) {
    if (WP_DEBUG === true) {
        if (is_array($message) || is_object($message)) {
            error_log(print_r($message, true));
        } else {
            error_log($message);
        }
    }
}

这太奇怪了。我错在哪里?

 foreach($colour_hash_array as $color)
    {
        log_me($color);
    }

打印

[26-Mar-2016 07:04:11 亚洲/Ho_Chi_Minh] 灰色

[2016 年 3 月 26 日 07:04:11 亚洲/Ho_Chi_Minh] 蓝色

更新:

我尝试创建一个数组

$colour_hash_array = array("07" => "GRAY","67"=>"BLUE");
log_me($colour_hash_array['07']);//Return GRAY
log_me($colour_hash_array['67']);//Return BLUE

这行得通。我无法再理解:(

你想要返回一个关联数组,你试图通过将解码的 json(这是一个对象(强制转换为数组来执行此操作。但这不能正常工作,正如您可能已经看到的那样。但是,json_decode函数中的第二个参数是将解码器设置为返回关联数组而不是对象 - 这正是您要查找的内容。这里有一个例子:

$encoded = json_encode(array("07" => "GRAY","67"=>"BLUE"));
$decoded = json_decode($encoded);             // Returns stdClass-object
$decoded_array = json_decode($encoded, true); // Return an associative array, 
                                              // which is what you're looking for 

您可以分别看到$decoded$decoded_arrayvar_dumps()

// var_dump($decoded);
object(stdClass)#7 (2) { 
    ["07"]=> string(4) "GRAY" 
    ["67"]=> string(4) "BLUE" 
} 
// var_dump($decoded_array);
array(2) { 
    ["07"]=> string(4) "GRAY" 
    [67]=> string(4) "BLUE" 
}

解决方案

所以在你定义$colour_hash的地方,你只需要在json_decode函数中添加第二个参数true,让它像这样

$colour_hash_array = json_decode($matches[1], true);

输出的结果应该是

log_me($colour_hash_array['07']); //Returns GRAY
log_me($colour_hash_array['67']); //Returns BLUE

引用

  • http://php.net/manual/en/function.json-decode.php

当你定义你的数组时,你正在使用整数作为你的键(我应该说我假设了这一点,但在我看来这是最有可能的(。

所以这个:

$arr = [
    07 => 'GRAY',
    67 => 'BLUE'
];

结果是:

Array(
    7 => 'GRAY',
    67 => 'BLUE
)

看看 0 是如何从键的前面掉下来的?整数不存储该零。

要引用该键,可以使用 $arr[7]$arr['7']


如果保留该零对您很重要,请使用字符串而不是整数来定义您的键,并且零应保留:

所以这个:

$arr = [
    '07' => 'GRAY',
    '67' => 'BLUE'
];

结果是:

Array(
    '07' => 'GRAY',
    '67' => 'BLUE
)
现在

键是一个字符串,您必须将其引用为确切的字符串,因此现在只有$arr['07']才能工作。

希望对您有所帮助!

对不起,我以前的愚蠢答案,我重现了这种行为,请参阅代码:

define('WP_DEBUG', true);
function log_me($message) {
    if (WP_DEBUG === true) {
        if (is_array($message) || is_object($message)) {
            error_log(print_r($message, true));
        } else {
            error_log($message);
        }
    }
}
//decode json and convert to array
$colour_hash_array = (array)json_decode('{"07":"GREY", "67":"BLUE", "09":"BLACK"}');
print_r($colour_hash_array);   
foreach($colour_hash_array as $key => $color) {
   print 'type:' . gettype($key) . ':' . $key . "'n";
}
log_me($colour_hash_array['07']);//Return GRAY
log_me($colour_hash_array['67']);//undefine offset
log_me($colour_hash_array['09']);//Return BLACK

但是如果我们使用 get_object_vars 将对象转换为数组,则此代码工作正常:

define('WP_DEBUG', true);
function log_me($message) {
    if (WP_DEBUG === true) {
        if (is_array($message) || is_object($message)) {
            error_log(print_r($message, true));
        } else {
            error_log($message);
        }
    }
}
//decode json and convert to array
$colour_hash_array = get_object_vars(json_decode('{"07":"GREY", "67":"BLUE", "09":"BLACK"}'));
print_r($colour_hash_array);

foreach($colour_hash_array as $key => $color) {
   print 'type:' . gettype($key) . ':' . $key . "'n";
}
log_me($colour_hash_array['07']);//Return GRAY
log_me($colour_hash_array['67']);//Return BLUE
log_me($colour_hash_array['09']);//Return BLACK