3592

PHP: How to access array element values using array-index

Question:

How to access array element values using array-index?

<? $json = '{ "dynamic":{ "pageCount":"12", "tableCount":"1" } }'; $arr = json_decode($json, true); echo $arr['dynamic']['pageCount']; // working echo $arr[0]['pageCount']; // not working ?>

I will not know what is there in 'dynamic', so i want to access pageCount values dynamically?

Answer1:

<a href="http://php.net/array_values" rel="nofollow">array_values</a> is function you are looking for

Examples:

<?php $json = '{ "dynamic":{ "pageCount":"12", "tableCount":"1" } }'; $arr = json_decode($json, true); echo $arr['dynamic']['pageCount']; // working $arr = array_values($arr); echo $arr[0]['pageCount']; // NOW working ?>

Answer2:

$arr = json_decode($json, true); foreach ($arr as $key => $value) { if (isset($value['pageCount'])) { //do something with the page count } }

If the structure is always a single nested JS object:

$obj = current($arr); echo $obj['pageCount'];

Recommend

  • What is an “illegal primary” in awk?
  • algorithm to merge two arrays into an array of all possible combinations
  • How to load icon from resource in Java?
  • Error establishing a database connection on wordpress
  • Export Sql Server 2008 database into MS Access Database
  • IntelliJ Tomcat Server plugin fails to auto-populate my empty external Tomcat `base` folder
  • No perfect way to detect device orientation on iPad?
  • Error when deploying Tizen App - Failed to get a device information
  • Using a custom URL rewriter, IIS6, and urls with .htm, .html, etc
  • How do I redirect an user back to the page they were trying to access once they log in? (Django)
  • Can upload photo when using the Google Photos API
  • GLSurfaceView Zoom(PINCH ZOOM)
  • Returning the content of multiple files in node.js
  • query a json key in postgres json field
  • Why is this Animatable property being set again?
  • Coin change recursive approach
  • Winston logger not write to file
  • jQuery how to translate livequery to on?
  • Firebase suddenly reports invalid signature
  • Identifying dates in strings using NLTK
  • Hyperlink to Outlook Attachment
  • How to redirect into different page by user type in php and mysql
  • Generate and export point cloud from Project Tango
  • Wireshark Display Filter for Unique Source/Destination IP and Protocol
  • Google Spreadsheet Script to Blink a range of Cells
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Capture SIGFPE from SIMD instruction
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Using Service Component Runtime
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • How to mutate multiple variables without repeating codes?
  • Access to a Matlab gui from the web