2

$color_hash is an stdObject

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

print_r($color_hash); return

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

I convert $color_hash to an array

$colour_hash_array = (array)$colour_hash;

return

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

Tried

var_dump($colour_hash_array);

return

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

I try

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);
        }
    }
}

That is so strange. Where i am wrong?

using

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

    }

print

[26-Mar-2016 07:04:11 Asia/Ho_Chi_Minh] GRAY

[26-Mar-2016 07:04:11 Asia/Ho_Chi_Minh] BLUE

UPDATE:

I tried to create an array

$colour_hash_array = array("07" => "GRAY","67"=>"BLUE");

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

This works. I can not understand any more :(

17
  • and what does the log_me do exactly? Commented Mar 26, 2016 at 0:06
  • even though I try $c = $colour_hash_array['67']; echo $c; it return empty Commented Mar 26, 2016 at 0:08
  • That's odd! And how is $colour_hash_array defined in the code? Commented Mar 26, 2016 at 0:12
  • I updated my question, how is $colour_hash_array defined Commented Mar 26, 2016 at 0:19
  • how you use php version? Your code work is fine for me in php 5.6 Commented Mar 26, 2016 at 0:21

3 Answers 3

2

You want to return an associative array, which you're trying to do by casting the decoded json (which is an object) into an array. But that doesn't work properly, as you might've seen already. However, the second parameter in the json_decode function is to set the decoder to return an associative array instead of an object - which is exactly what you're looking for. An example of it here:

$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 

You can see the var_dumps() of $decoded and $decoded_array to be respectively

// 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" 
}

The solution

So where you define $colour_hash, you simply need to add a second parameter true to the json_decode function, making it like this

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

The result of your output should be

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

References

Sign up to request clarification or add additional context in comments.

Comments

0

When you define your array, you're using integers for your keys (I should say I am assuming this, but it's what seems most likely to me).

So this:

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

Results in this:

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

See how the 0 was dropped from the front of the key? An integer doesn't store that zero.

To reference that key, you can use $arr[7] or $arr['7'].


If it is important for you to keep that zero, then use a string to define your key, instead of an integer, and the zero should remain:

So this:

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

Results in this:

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

Now that the key is a string, you have to reference it is as the exact string, so only $arr['07'] will work now.

Hope that helps!

2 Comments

That 0 is just the octal notation. 07 (base 8) == 7 (base 10). In this case $arr[07], $arr[7], $arr['7'] give the same result.
Agreed, if the key is defined as an integer, in base 10 or 8, $arr[07], $arr[7], and $arr['7'] should work just fine, though $arr['07'] will not. However, if the key is defined as a string, then $arr['07'] will work and $arr[07], $arr[7], and $arr['7'] will throw an Undefined offset error.
0

Sorry my previus stupid answer, I reproduce this behavior, please see code:

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

But if we use get_object_vars for convert object to array, this code work is fine:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.