简体   繁体   English

从数组中获取值

[英]fetch value from an array

I need to fetch value from an array .I need to fetch the value name,value,user_id from an given array 我需要从数组中获取值。我需要从给定数组中获取值名称,值,user_id

$inner_content='[{"name":"radio","value":"1","id":"1","user_id":"admin@gmail.com","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); 
}

In JSON, square brackets denote an array, and curly braces denote an object. 在JSON中,方括号表示数组,大括号表示对象。 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. 如您所见,如果您仔细查看$inner_content ,它是一个包含一堆对象的数组,因此您需要对其进行索引。

$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). 如果要获取所有名称,则可以在$data1上使用foreach循环(但只有第一个具有您想要的所有属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM