Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14971

Re: 4 dimensional associative array access

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.php
Subject Re: 4 dimensional associative array access
Date 2015-02-23 22:39 +0000
Organization A noiseless patient Spider
Message-ID <mcga7o$gs7$2@dont-email.me> (permalink)
References <f6d75dde-ec33-4c8e-8d77-b370152a72bd@googlegroups.com>

Show all headers | View raw


On Mon, 23 Feb 2015 00:09:40 -0800, Adnan Shiekh wrote:

> Hi everyone,
> 
> i have a question how to access 4 dimesional array efficiently in php
> 
> 
> <?php $json = '{
> "return": true,
> "output": {
> "total": 100,
> "data": [
> {
> "name": "john",
> "password": "hello",
> }
> },
> {
> "name": "jack",
> "password": "hello2",
> }
> }
> ]
> }
> }
> ';
> $result = json_decode ($json);
> ?>
> 
> i have convert this json and print like this
> 
> |name| password|
> _______________
> |john| hello   |
> |jack| hello2  |
> 
> i know this json convert in to associative array so that how can i
> access that?? thanks in advance...

1) Look at the options to json_decode. If you want an associative array 
instead of an object, you need to pass the option.

2) Fix your json string. 

Try this code:

<?php

$arr = array(
'return' => true,
'output' => 
    array(
    'total' => 100,
    'data' => 
        array(
            array('name' => 'john', 'password' => 'hello'),
            array('name' => 'jack', 'password' => 'hello2')
        )
    )
);

$json = json_encode($arr);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'json error number ' . json_last_error() . "\n";
    exit;
}

echo "\$json = '$json'\n";

$result = json_decode ($json, true);

if (json_last_error() !== JSON_ERROR_NONE)
    echo 'json error number ' . json_last_error() . "\n";
else
    print_r($result);

?>

'hello2' can be accessed as:

$result['output']['data'][1]['password']

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 00:09 -0800
  Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-23 09:48 -0500
  Re: 4 dimensional associative array access Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-02-23 15:52 +0100
  Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 10:26 -0800
    Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-23 15:44 -0500
  Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 10:39 -0800
  Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 13:31 -0800
  Re: 4 dimensional associative array access Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-23 22:39 +0000
  Re: 4 dimensional associative array access Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-24 00:13 +0000
  Re: 4 dimensional associative array access Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-24 01:49 +0000
    Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-23 20:56 -0500
  Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 23:07 -0800
    Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-24 07:47 -0500

csiph-web