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

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值