Jump to content

Echo nested arrays?


techker
 Share

Recommended Posts

Hey guys i need to echo this:

Quote

{
   "error":false,
   "result":[{
  "apk_sign":"0F:19:F7:2D:F0:40:2C:FE:25R:C1:EB:80:0FF:FC:92:DA:4F:BE:2F:9F",
    "portal_url":"
   http://changeme.net:80,
    http://changeme2.net:80,
    http://changeme3.net:80
  "}]
  }

Up to know i tried lots of combos but can seem to get the Array correctly?

Quote

$named_array3 = array(
         "error" => false,
         "result" => array(
            "apk_sign" => "0F:19:F7:2D:F0:40:2C:FE:25d:C1D:EB:80:0F:FCD:92:DA:46:BEF:23D:9DD",
            "portal_url" => "http:\/\/changeme.net:80,http:\/\/changeme.net:80,http:\/\/changeme.net:80")
);

it echos

Quote

 

{

   "error":false,
   "result":{      "apk_sign":"0F:19:F7:2D:F0:40:2C:FE:25:C1:EB:80:0F:FC:92:DA:46:BE:23:9D",
      "portal_url":[

       "http:\\\/\\\/changeme.net:80,http:\\\/\\\/changeme.net:80,http:\\\/\\\/changeme.net:80"    

]}

}

 

but the [ needs to be at results....

 

Thx for the help

Link to comment
Share on other sites

The answer is to use a recursive function.  Run this:

$array1 = array(
    'key'=>'value',
    'key2'=>'value2',
    'key3'=>'value3',
    'arr2'=>array(
           'subkey'=>'subvalue',
           'subkey2'=>'subvalue2'
            ),
    'key4'=>'value4',
    'arr3'=>array(
           'subkey3'=>'subvalue3',
           'subkey4'=>'subvalue4'
           ),
     'lastkey'=>'lastvalue'
    );
DumpArray($array1);
//***************************
function DumpArray($ar)
{
    foreach($ar as $k=>$v)
    {
        if (is_array($v))
        {
            echo "Array $k - ";
            DumpArray($v);
        }
        else
            echo "$k - $v<br>";
    }
    echo "End array<br>";
    return;
}
exit();
	

You can add code to make the presentation neater if you want but at least this shows you how to process the nested arrays.

 

Edited by ginerjm
Link to comment
Share on other sites

array() can be used to create something that looks like an object (with keys) and something that looks like a list of things (no keys).

The result part has two opening symbols: [ and a {. The first one is a list, the second is an object.
Two symbols, two array()s. One inside the other.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.