首页 > 编程语言 >PHP json_decode如何输出数组(php json_decode how to output array)

PHP json_decode如何输出数组(php json_decode how to output array)

时间:2022-10-27 11:03:54浏览次数:48  
标签:string items decode json address array php Array


  Here is my php code with json formatted string:

$string=';
  ?>

  I want to learn how to parse/output json string into something I can show in html or put into database .. however i am stuck on something that is prob very simple but I've spent most of the morning trying to figure out.

  What I want to understand is why the results of my code above gives me the following result:

  "items: Array"

  And not what I want/expect to get:

  "items: W 7th Ave"

  "items: W 8th St"

  What am i missing? Isn't "Address" the next "level" down from "Item" in the array?

盾畳圭宛
  $string=file_get_contents('https://www.it1352.com/string.json');
  $json=json_decode($string);
  if you want to have items:
:
  foreach ($json['items'] as $address)
  {
  echo "items:". $address['address'] ."
  ";
  };
  anyway if you are not sure about how an array is built you could print it via:
  print_r($json);
  which will print:
  Array
  (
  [items]=> Array
  (
  [0]=> Array
  (
  [address]=> W 7th Ave
  )
  [1]=> Array
  (
  [address]=> W 8th St
  )
  )
  )

  now you found out that $json contains just an array (items) of two array, then, if you loop it, you will get that array which is printed in your example.

  As explained above you need to go one step deeper by looping the elements in your items array and print their address element.

  here is the complete script: http://pastie.org/2275879

标签:string,items,decode,json,address,array,php,Array
From: https://blog.51cto.com/yetaotao/5800292

相关文章

  • PHP 2D数组输出所有组合
    我需要做同样的事情,并且尝试了此处发布的以前的解决方案,但无法使它们起作用。我从这个聪明的人http://www.php.net/manual/en/ref.array.php#54979中获得了一个样本。但......
  • PHP – 在数组中分解值,输出到制表符分隔文件
    我在名为test_tab.txt的文件中有以下内容(制表符分隔):header1header2header3field1field1afield1b;field1cfield2field2afield2bfield3field3a......
  • PHP session数组输出(遍历输出)
    做购物车弄的下边是存储的sessionvar_dump的结果,我想遍历数组输出里边的信息,如编号名称价格等array(size=3)'C00000013'=>array(size=5)'id'=>string......
  • PHP 字符串分割数组并输出
    文章目录摘要1.1常见概念1.1.1机器学习本质1.1.2什么是神经网络1.1.3各种常见算法图示1.1.4计算图的导数计算1.1.5理解局部最优与全局最优1.1.6大数据与深度学习......
  • PHP :array_diff 用法(php计算数组的差集)
    说多了都是废话,直接上图:结果输出:由上图的结果可以看出:array_diff($a,$b)的结果只输出了5与8,则可以看出,输出的是$a的差集。array_diff($b,$a)......
  • openlayers 添加行政区域 geojson
    先地图初始化然后再添加图层 import{VectorasVectorLayer}from'ol/layer';import{VectorasVectorSource}from'ol/source';import{GeoJSON}from'ol......
  • 配置 tsconfig.json
    tsconfig.json所包含的属性并不多,只有7个,ms官方也给出了它的定义文件。但看起来并不怎么舒服,这里就翻译整理一下。(若有误,还请指出)files:数组类型,用于表示由ts管理......
  • 解决PHPExcel科学计数法问题
     1.使用PhpSpreadsheet进行导入导出最简单正确直接的办法是使用正确的(方法)常见给单元格赋值方法:setCellValue是不行的解决办法:1.单元格值后接......
  • php代码审计
    反斜杠被过滤,利用cd回到根目录<?phperror_reporting(0);if(isset($_GET["cmd"])){if(preg_match('/et|echo|cat|tac|base|sh|more|less|tail|vi|head|nl|env|fl|\|......
  • PHP 中的 array
    PHP中的array实际上是一个有序映射。映射是一种把values关联到keys的类型。此类型针对多种不同用途进行了优化;它可以被视为数组、列表(向量)、哈希表(映射的实现)、字......