0

I am trying to retrieve data from this array in php.

array(2) {
   ["getWysiwyg"]=>
     string(37) "[{"basicsDescription":"<p><br></p>"}]"
   ["getGoal"]=>
     string(27) "[{"iconURL":"","title":""}]"
}

I tried Input::get('getWysiwyg') it returns [{"basicsDescription":"<p><br></p>"}]

Now how could i get the value i.e <p><br></p>

2
  • 2
    That's JSON which you need to decode with json_decode() Commented Nov 18, 2015 at 13:05
  • 2
    aaand here is the link to json_decode() ;) Commented Nov 18, 2015 at 13:06

3 Answers 3

1

As I see your array items are json encoded ..

Try to decode them as this:

foreach($array as $key=>$value){
    $decodedValue = json_decode($value, true);
    print_r($decodedValue);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use json_decode(), because the string [{"basicsDescription":"<p><br></p>"}]represents an array with an object in JSON.

$string = '[{"basicsDescription":"<p><br></p>"}]';

$objectArray = json_decode( $string );

$objectArray now looks like:

array(1) {
    [0]=>
    object(stdClass)#1 (1) {
      ["basicsDescription"]=>
      string(11) "<p><br></p>"
    }
}

To get the value of basicsDescription you need to access the array in this case with the index 0, then you have the object:

$object = $objectArray[0];

Once you've got the object you can access it's attributes with the object operator ->:

$object->basicsDescription;// content: <p><br></p>

Short form of this:

$string = '[{"basicsDescription":"<p><br></p>"}]';// in your case Input::get('getWysiwyg')

$objectArray = json_decode( $string );
$objectArray[0]->basicsDescription;

If it's possible, that there are more than one item in it, you should go for foreach

If all items of your array representing JSON strings you can use array_map():

$array = array(
    "getWysiwyg" => '[{"basicsDescription":"<p><br></p>"}]',
    "getGoal" => '[{"iconURL":"","title":""}]'
);

$array = array_map( 'json_decode' , $array );

echo "<pre>";
var_dump( $array );

This will output:

array(2) {
  ["getWysiwyg"]=>
  array(1) {
    [0]=>
    object(stdClass)#1 (1) {
      ["basicsDescription"]=>
      string(11) "<p><br></p>"
    }
  }
  ["getGoal"]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (2) {
      ["iconURL"]=>
      string(0) ""
      ["title"]=>
      string(0) ""
    }
  }
}

Comments

0

Decode and print as follows

$object = json_decode(Input::get('getWysiwyg'));
print $object[0]->basicsDescription;

or directly with the help of array dereferencing

print json_decode(Input::get('getWysiwyg'))[0]->basicsDescription;

will output

<p><br></p>

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.