簡體   English   中英

從數組中獲取值

[英]fetch value from an 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); 
}

在JSON中,方括號表示數組,大括號表示對象。 如您所見,如果您仔細查看$inner_content ,它是一個包含一堆對象的數組,因此您需要對其進行索引。

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

這只是從數組中的第一個對象獲取名稱。 如果要獲取所有名稱,則可以在$data1上使用foreach循環(但只有第一個具有您想要的所有屬性)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM