3

The following array has JSON within:

$row['modifiers']

How can I access the modifier_value within this?

I have tried:

$row['modifiers']['modifier_value'];

This is how the array looks:

[{"modifier_id":"","modifier_name":"Type","modifier_type":"custom","modifier_value":"Tour","option_id":"","price_mod":"","price_mod_inc_tax":""}]
3
  • 1
    You'll need to decode that JSON to work with it, otherwise PHP just sees it as a string. Take a look at json_decode Commented Dec 11, 2015 at 10:10
  • I already tried decoding but its within 'stdClass Object'. If I decode, how would I access it: Array ( [0] => stdClass Object ( [modifier_id] => [modifier_name] => Type [modifier_type] => custom [modifier_value] => Tour [option_id] => [price_mod] => [price_mod_inc_tax] => ) ) Commented Dec 11, 2015 at 10:12
  • Niranjan's answer should have you covered but for reference json_decode has a second optional argument that can give you back objects as associative arrays Commented Dec 11, 2015 at 10:15

1 Answer 1

3

Try like this

    <?php

    $a = '[{"modifier_id":"","modifier_name":"Type","modifier_type":"custom","modifier_value":"Tour","option_id":"","price_mod":"","price_mod_inc_tax":""}]';

    $decode = json_decode($a);

    foreach ($decode as $arr){
        echo $arr->modifier_name;
        echo $arr->modifier_type;
        // so on
    }

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

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.