galabl
galabl

Reputation: 63

Unable to access array key when key name have special character

I'm experiencing issue with accessing to array key witch have apostrophe in the name, please see the var dump of array

array(5) { ["Shipper's choice"]=> array(2) { ["weight_sign"]=> string(1) "+" ["weight_count"]=> string(0) "" } ["Interesting wood grain"]=> array(2) { ["weight_sign"]=> string(1) "+" ["weight_count"]=> string(0) "" } ["Part blond face wood"]=> array(2) { ["weight_sign"]=> string(1) "+" ["weight_count"]=> string(0) "" } ["Mostly light colored"]=> array(2) { ["weight_sign"]=> string(1) "+" ["weight_count"]=> string(0) "" } ["Darkest wood"]=> array(2) { ["weight_sign"]=> string(1) "+" ["weight_count"]=> string(0) "" } } 

If I try to access to $array["Shipper's choice"] it's working fine.

But if I have that key in a variable $value = "Shipper's choice" and try to access the array with $array[$value] I get the following error message Notice: Undefined index: Shipper's choice.

This is how I actually want to access this value $option_weights[$options["id"]][$value]["weight_sign"]

I was unable to find the solution on my own so I'm asking for your help.

Thanks.

Edit:

This is working fine if the key doesn't have '(apostrophe) in the name

Edit 2: Try to recreate with this:

This is the array I'm getting

$arr = [
            [
                "id" => "5f297bc71bd21",
                "raw" => [
                    "5f297"
                ],
                "values" => [
                    [
                        "label" => "Soonest Available",
                        "price" => 0,
                        "price_type" => "none",
                        "slug" => "5f297"
                    ]
                ],
                "label" => "Tine Color"
            ],
            [
                "id" => "5f297bc71bd3c",
                "raw" => [
                    "5f297"
                ],
                "values" => [
                    [
                        "label" => "Shipper's choice",
                        "price" => 0,
                        "price_type" => "none",
                        "slug" => "5f297"
                    ]
                ],
                "label" => "Want special wood?"
            ]
        ];

Then from database I'm pulling this value with WordPress get_post_meta() function

$post_meta_value = a:5:{s:13:"5f297bc71bd21";a:8:{s:17:"Soonest Available";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:4:"Blue";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:5:"Green";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:3:"Red";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:6:"Yellow";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:5:"White";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:9:"Unpainted";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:12:"Custom Paint";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}}s:13:"5f297bc71bd34";a:5:{s:1:"D";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:2:"Db";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:2:"Eb";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:1:"C";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:1:"E";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}}s:13:"5f297bc71bd3c";a:5:{s:16:"Shipper's choice";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:22:"Interesting wood grain";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:20:"Part blond face wood";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:20:"Mostly light colored";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}s:12:"Darkest wood";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:0:"";}}s:13:"5f297bc71bd44";a:1:{s:3:"Yes";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:1:"5";}}s:13:"5f297bc71bd47";a:1:{s:3:"Yes";a:2:{s:11:"weight_sign";s:1:"+";s:12:"weight_count";s:1:"2";}}}

$option_weights = get_post_meta('post_id', '$post_meta_value', true)

Here is the code to use:

foreach ($arr as $options) {
    if (isset($options["id"]) && array_key_exists($options["id"], $option_weights)) {
        $value = $options["values"][0]["label"];
        if (strlen($option_weights[$options["id"]][$value]["weight_count"]) > 0) {
            // Code breaks here
        }
}

Upvotes: 0

Views: 269

Answers (1)

galabl
galabl

Reputation: 63

From @Jeto:

Your key probably contains HTML entities. And since you're probably dumping them on a webpage, you're not seeing them. Check the source of the page. Pretty sure you'll see Shipper's choice

Upvotes: 1

Related Questions