php循环输出数组 json,php循环通过json数组(php loop through json array)
php循環通過json數組(php loop through json array)
我有一個像這樣的json字符串:
$fields_string = '
{"fields":
{"customers":[{"name":"john","id":"d1"},
{"name":"mike","id":"d2"},
{"name":"andrew","id":"d3"},
{"name":"peter","id":"d4"}]
}
}'
我該如何打印每個名字? 稍后我將在html選項中使用它們,我知道如何做到這一點。 但是我無法把繩子拿出來。 這是我嘗試過的東西:
$obj = json_decode($fields_string);
$fields_detail = $obj-?{"fields"}->{"customers"};
在這一點上,我能夠通過echo json_encode($fields_detail)打印出客戶數組,但在此之前,我希望使用foreach來echo json_encode($fields_detail)名稱。 我試了好幾次,但沒辦法。 任何人都可以幫忙。
謝謝!
I have a json string like this:
$fields_string = '
{"fields":
{"customers":[{"name":"john","id":"d1"},
{"name":"mike","id":"d2"},
{"name":"andrew","id":"d3"},
{"name":"peter","id":"d4"}]
}
}'
How can I print each name? I will use them later in a html select options, I know how to do that. But I couldn't get the string out. Here are something I tried:
$obj = json_decode($fields_string);
$fields_detail = $obj-?{"fields"}->{"customers"};
at this point, I am able to print the customer array out by echo json_encode($fields_detail), but before that, I want to get the name break down using foreach. I tried several times, it didn't work. Can anyone help please.
Thanks!
原文:https://stackoverflow.com/questions/15000874
更新時間:2020-01-20 20:54
最滿意答案
客戶是一個對象數組,因此迭代每個對象并讀取屬性應該有效。
foreach ($fields_detail as $customer) {
echo $customer->name;
}
Customers is an array of objects so iterating over each object and reading the property should work.
foreach ($fields_detail as $customer) {
echo $customer->name;
}
2013-02-21
相關問答
如果需要關聯數組,請將第二個函數參數設置為true 如果您需要關聯數組,某些版本的php需要第2個參數 $json = '[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]';
$array = json_decode( $json, true );
Set the second function parameter to true if you require an associative a
...
還有一個級別高于您嘗試訪問的級別。 嘗試這個: $tasks = $decoded['tasks'];
foreach($tasks as $task) {
echo $task["task"];
}
There is another level above what are you trying to access. Try this: $tasks = $decoded['tasks'];
foreach($tasks as $task) {
echo $task["task
...
json_decode()不返回數組。 要做到這一點,你需要做json_decode($_REQUEST['names'], true) http://php.net/manual/en/function.json-decode.php json_decode() doesn't return an array. To get it to do so you'd need to do json_decode($_REQUEST['names'], true) http://php.net/manu
...
您是否有理由不使用json_encode來執行此操作? 您可以使用json_encode通過一個快速調用將普通的php數組轉換為JSON。 $arr = ("a"=>"one", "b"=>"two");
echo $json_encode($arr);
http://php.net/manual/en/function.json-encode.php Is there a reason you aren't using json_encode to do this? You can conv
...
你正在訪問一個數組。 您使用[]執行此操作,而不是使用{} 。 要獲得令牌數組,您需要$json2['reply1']['tokens'] 。 同樣,您使用[]訪問此數組中的各個項目,而不是-> 。 使用$laa->token; 將再次失敗。 請嘗試以下方法 $tokens = $json2['reply1']['tokens'];
foreach ($tokens as $token) {
echo $token['token']
}
You're accessing an array.
...
在循環內試試這個: if(isset($row['tableDet'])){// or !is_null() or is_numeric()
$tableDet = "occupied";
}else{
$tableDet = $row['tableDet']);//or = "free" or null
}
$contact= array(
"ID" => $row['tabledetailid'],
"Name" => $row['name'],
"tabl
...
一旦你在php中把一個關聯數組編碼成一個JSON對象,它不再是一個數組,它是一個帶有鍵和值的對象。 您可以使用for..in在javascript中迭代它們 for (var key in obj) {
console.log(obj[key]);
}
注意: for..in不是garuntee順序,如果你需要確保順序,你將不得不使用你的數組索引,而不是key=>value ,而是使用for循環(或while )。 Once you encode an associative array
...
客戶是一個對象數組,因此迭代每個對象并讀取屬性應該有效。 foreach ($fields_detail as $customer) {
echo $customer->name;
}
Customers is an array of objects so iterating over each object and reading the property should work. foreach ($fields_detail as $customer) {
echo $custom
...
每次進入循環時,都會覆蓋您的數組 嘗試: $json1[] = array(); $json2[] = array(); $this->db->sql = "SELECT ID AS ID, WebsiteName AS SelectText, WebsiteCode AS SelectValue FROM Banners_Websites ORDER BY WebsiteName";
$rs = $this->db->query($this->db->sq
...
您有許多具有相同數據的不必要變量。 你只需要做以下事情 <?php
header('Content-type: application/json');
$db = new PDO('sqlite:whad.db')or die('Could not open database');
$json = array(
"Daniel" => array(),
"Andi" => array(),
"Dave" => array(),
"Simon" => array()
);
for
...
總結
以上是生活随笔為你收集整理的php循环输出数组 json,php循环通过json数组(php loop through json array)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android shell 解压zip,
- 下一篇: 显示省份简称,如内蒙古自治区:内蒙古