2

I need to fetch value from an array .I need to fetch the value name,value,user_id from an given array

$inner_content='[{"name":"radio","value":"1","id":"1","user_id":"[email protected]","web":"571710720","type":"poll_info","pg":"question_response"},{"name":"fav-color[]","value":"blue"}]'


$id='5';  //value given for expample.
$inner="select * from user_response where POLL_ID=$id";
$inner1=mysql_query($inner);
while($ifet=mysql_fetch_assoc($inner1))
{
  $inner_content = $ifet['CONTENT_VALUES'];
  $data1 = json_decode($inner_content);
  $test1[]=array('name'=>$data1->name); 
}
4
  • is there associative array..???? Commented Feb 12, 2016 at 6:15
  • var_dump($data1); what is the output of this? Commented Feb 12, 2016 at 6:16
  • the decoded json string turned into object has another dimension inside, its multi leveled, its supposed to be $data1[0]->name and so on Commented Feb 12, 2016 at 6:16
  • ya multi-level array Commented Feb 12, 2016 at 6:18

1 Answer 1

3

In JSON, square brackets denote an array, and curly braces denote an object. As you can see if you look carefully at $inner_content, it's an array containing a bunch of objects, so you need to index it.

$test1[] = array('name' => $data1[0]->name);

This just gets the name from the first object in the array. If you want to get all the names, you could use a foreach loop on $data1 (but only the first one has all the properties that you say you want).

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

10 Comments

That's not very meaningful. What is it doing instead? Are you getting any errors or warnings (make sure you have error_reporting(E_ALL) enabled)?
It return the word 'Array'
@Sougata Presumably the JSON in his actual database isn't like the variable he posted in the question.
object(stdClass)#2 (7) { ["name"]=> string(5) "multi" ["value"]=> string(8) "option 4" ["id"]=> string(1) "4" ["user_id"]=> string(15) "[email protected]" ["web"]=> string(9) "571710720" ["type"]=> string(9) "poll_info" ["pagename"]=> string(17) "question_response" }
If that's what var_dump says, then there's no way that $data1[0]->name could return the word Array. It should return multi.
|

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.