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)相关推荐

  1. php在页面循环输出标签,自定义页面循环

    一.语法{page  ....}{/page} 二.参数介绍参数介绍 site站点id,默认当前站点,填写其他站点id id指定显示,多个id以小写分号分开,例如1,2,3,4,5 show有show ...

  2. python一个文本循环输出_Python实现动态循环输出文字功能

    在一些公共场所经常可以看到一些动态提示的文字,以下代码即为文字的循环变化代码: import sys import time def print_act(word): print('新春佳节快乐'+' ...

  3. android 一维数组遍历,$.each()循环遍历一维数组、二维数组、JSON数据和DOM元素

    本文介绍$.each()循环遍历一维数组.二维数组.JSON数据和DOM元素. 请注意在使用JQuery $.each()方法前,要首先引用jquery库文件. $.each()循环遍历一维数组 Jq ...

  4. php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急

    PHP抓取页面上的数组 并循环输出 急 在线等 我用file_get_contents()抓取了 这个网址上的内容 http://simonfenci.sinaapp.com/index.php?ke ...

  5. jq循环输出数组并显示在html,jquery怎么循环输出数组元素?

    jquery怎么循环输出数组元素?下面本篇文章给大家介绍一下使用jquery循环输出数组元素的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 用JQuery循环输出数组元素即数 ...

  6. 利用循环,使得10 * 10的二维数组具有以下值,并按以下结构输出在屏幕上

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p113 习题4 5.利用循环,使得10 * 10的二维数组具有以下值,并按以下结构输出在屏幕上 0 1 ...

  7. shell数组目录遍寻循环输出

    linux shell编程控制结构:expr.let.for.while.until.shift.if.case.break.continue.函数.select 学习笔记 分类: linux2013 ...

  8. php循环输出数组key

    摘要:众所周知在学习网站开发时数组也是一个非常绕脑且很关键的知识点.特别是涉及到多维数组,很多零基础的朋友往往是看着看着就晕头转向了.那么本篇文章就为大家详细介绍下在... 众所周知在学习网站开发时数 ...

  9. php 遍历输出数组,php中数组怎么循环输出?遍历数组的方法介绍

    遍历数组中的所有元素是常用的一种操作,在遍历的过程中可以完成查询等功能.在我们日常生活中,如果想要去商场买一件衣服,就需要在商场中逛上一遍,看是否又想要的衣服,逛商场的过程我们就相当于遍历数组的操作. ...

最新文章

  1. 代码中配置log4j (同时输出不同级别的log到不同终端)
  2. linux停止ftp服务命令,centos没有service命令,怎么启用ftp服务
  3. 计量经济学建模_不了解计量经济学的前世今生,怎么能学会他
  4. XGBoost对比RandomForest、GBDT、决策树、SVM,XGB+LR精度还能提升
  5. 加载顺序_Java的web.xml组件加载顺序
  6. C#LeetCode刷题之#893-特殊等价字符串组​​​​​​​​​​​​​​(Groups of Special-Equivalent Strings)
  7. Spring Boot : Spring Boot Slf4j 以及 log4j 以及门面日志
  8. 网络通信 netstat
  9. 前端响应式のmedia文件分离
  10. PMP试题 | 每日一练,快速提分 8.5
  11. Excel函数大全一《求和与统计函数》
  12. Win11修改用户名(超详细图文)
  13. Python自定义豆瓣电影种类,排行,点评的爬取与存储(基础)
  14. 【智能优化算法】基于黑寡妇优化算法求解单目标优化问题含Matlab源码
  15. 配置 Tapestry
  16. 使用云服务器搭一个网站(1)
  17. 2021最新苹果CMS影视站全站模板100+套模板分享下载
  18. SpringBoot 接口数据加解密
  19. python 读取csv某一列_Python 读取csv的某列
  20. 【时间复杂度】你还在担心时间复杂度太高吗?

热门文章

  1. TortoiseSVN (Subversion客户端) 使用手册(中文) (四)
  2. 物通博联·设备远程维护快线
  3. Java笔试题:顺时针方向输出n*m的矩阵
  4. 关于像素PNG图片在游戏引擎缩放后出现毛边的解决方案
  5. 计算机在工程设计的应用,计算机辅助软件和网络技术在工程设计中的应用
  6. html5 关闭窗口事件,web窗口关闭事件之onunload、onbeforeunload事件详解
  7. 机器学习 之 决策树(Decision Tree)文本算法的精确率
  8. 欧洲统一语言参考标准C1,CEFR(欧洲共同语言参考标准)与英语考试换算标准
  9. Protege-OWL笔记3推理机(非一致性)
  10. 志宇-springSecurty