日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php循环输出数组 json,php循环通过json数组(php loop through json array)

發(fā)布時(shí)間:2024/3/26 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php循环输出数组 json,php循环通过json数组(php loop through json array) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

php循環(huán)通過(guò)json數(shù)組(php loop through json array)

我有一個(gè)像這樣的json字符串:

$fields_string = '

{"fields":

{"customers":[{"name":"john","id":"d1"},

{"name":"mike","id":"d2"},

{"name":"andrew","id":"d3"},

{"name":"peter","id":"d4"}]

}

}'

我該如何打印每個(gè)名字? 稍后我將在html選項(xiàng)中使用它們,我知道如何做到這一點(diǎn)。 但是我無(wú)法把繩子拿出來(lái)。 這是我嘗試過(guò)的東西:

$obj = json_decode($fields_string);

$fields_detail = $obj-?{"fields"}->{"customers"};

在這一點(diǎn)上,我能夠通過(guò)echo json_encode($fields_detail)打印出客戶數(shù)組,但在此之前,我希望使用foreach來(lái)echo json_encode($fields_detail)名稱。 我試了好幾次,但沒(méi)辦法。 任何人都可以幫忙。

謝謝!

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

更新時(shí)間:2020-01-20 20:54

最滿意答案

客戶是一個(gè)對(duì)象數(shù)組,因此迭代每個(gè)對(duì)象并讀取屬性應(yīng)該有效。

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

相關(guān)問(wèn)答

如果需要關(guān)聯(lián)數(shù)組,請(qǐng)將第二個(gè)函數(shù)參數(shù)設(shè)置為true 如果您需要關(guān)聯(lián)數(shù)組,某些版本的php需要第2個(gè)參數(shù) $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

...

還有一個(gè)級(jí)別高于您嘗試訪問(wèn)的級(jí)別。 嘗試這個(gè): $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()不返回?cái)?shù)組。 要做到這一點(diǎn),你需要做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來(lái)執(zhí)行此操作? 您可以使用json_encode通過(guò)一個(gè)快速調(diào)用將普通的php數(shù)組轉(zhuǎn)換為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

...

你正在訪問(wèn)一個(gè)數(shù)組。 您使用[]執(zhí)行此操作,而不是使用{} 。 要獲得令牌數(shù)組,您需要$json2['reply1']['tokens'] 。 同樣,您使用[]訪問(wèn)此數(shù)組中的各個(gè)項(xiàng)目,而不是-> 。 使用$laa->token; 將再次失敗。 請(qǐng)嘗試以下方法 $tokens = $json2['reply1']['tokens'];

foreach ($tokens as $token) {

echo $token['token']

}

You're accessing an array.

...

在循環(huán)內(nèi)試試這個(gè): 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中把一個(gè)關(guān)聯(lián)數(shù)組編碼成一個(gè)JSON對(duì)象,它不再是一個(gè)數(shù)組,它是一個(gè)帶有鍵和值的對(duì)象。 您可以使用for..in在javascript中迭代它們 for (var key in obj) {

console.log(obj[key]);

}

注意: for..in不是garuntee順序,如果你需要確保順序,你將不得不使用你的數(shù)組索引,而不是key=>value ,而是使用for循環(huán)(或while )。 Once you encode an associative array

...

客戶是一個(gè)對(duì)象數(shù)組,因此迭代每個(gè)對(duì)象并讀取屬性應(yīng)該有效。 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

...

每次進(jìn)入循環(huán)時(shí),都會(huì)覆蓋您的數(shù)組 嘗試: $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

...

您有許多具有相同數(shù)據(jù)的不必要變量。 你只需要做以下事情 <?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

...

總結(jié)

以上是生活随笔為你收集整理的php循环输出数组 json,php循环通过json数组(php loop through json array)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。