简体   繁体   中英

get all values from array inside of another array

i am stucked trying to get information from an array file in php. I was able to read it and convert to php array, but now i want to print the stored information in a table but always i get incorrect values.

¿how can i retrieve the information correctly?

Array:

array (
  'enableservice' => true,
  'putaLimit' => 
  array (
    0 => 0,
    1 => 0,
    2 => 0,
    3 => 0,
    4 => 0,
  ),
  'remoteService' => true,
  'Console' => 
  array (
    0 => 
    array (
      'bConsole' => 1,
      'ConsoleReference' => 'help',
      'EnableConsole' => true,
      'ViewPermissions' => 1,
    ),
  ),
  'accountData' => 
  array (
    0 => 
    array (
      'AccountName' => 'CSSName',
      'accountBalance' => 
      array (
        0 => 450440561,
        1 => 278575333,
        2 => 325290889,
        3 => 1838037277,
        4 => 155835317,
      ),
    ),
    1 => 
    array (
      'AccountName' => 'CSSCustomer',
      'accountBalance' => 
      array (
        0 => 5230834,
        1 => 3008402,
        2 => 3008400,
        3 => 3231485,
        4 => 9025200,
      ),
    ),
  ),
)

accountData is the array which i want to show as datatable or table. I'll glad for your help

used print_r($content) to show this:

    stdClass Object
(
    [enableservice] => 1
    [putaLimit] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
        )

    [remoteService] => 1
    [Console] => Array
        (
            [0] => stdClass Object
                (
                    [bConsole] => 1
                    [ConsoleReference] => help
                    [EnableConsole] => 1
                    [ViewPermissions] => 1
                )

        )

    [accountData] => Array
        (
            [0] => stdClass Object
                (
                    [AccountName] => CSSName
                    [accountBalance] => Array
                        (
                            [0] => 450440561
                            [1] => 278575333
                            [2] => 325290889
                            [3] => 1838037277
                            [4] => 155835317
                        )

                )

            [1] => stdClass Object
                (
                    [AccountName] => CSSCustomer
                    [accountBalance] => Array
                        (
                            [0] => 5230834
                            [1] => 3008402
                            [2] => 3008400
                            [3] => 3231485
                            [4] => 9025200
                        )

                )

        )

)

I used the array you gave and created a solution below. This solution is not the only way to do this. But it is easy to understand.

You can read further on the subject and look at other examples of using associative arrays from the official docs.

Here's the sandbox code sample link for the solution.

http://sandbox.onlinephpfunctions.com/code/532935a21f5f80d8e6d128bed69be18096093ab2

<?php

$arr = array (
  'enableservice' => true,
  'putaLimit' => 
  array (
    0 => 0,
    1 => 0,
    2 => 0,
    3 => 0,
    4 => 0,
  ),
  'remoteService' => true,
  'Console' => 
  array (
    0 => 
    array (
      'bConsole' => 1,
      'ConsoleReference' => 'help',
      'EnableConsole' => true,
      'ViewPermissions' => 1,
    ),
  ),
  'accountData' => 
  array (
    0 => 
    array (
      'AccountName' => 'CSSName',
      'accountBalance' => 
      array (
        0 => 450440561,
        1 => 278575333,
        2 => 325290889,
        3 => 1838037277,
        4 => 155835317,
      ),
    ),
    1 => 
    array (
      'AccountName' => 'CSSCustomer',
      'accountBalance' => 
      array (
        0 => 5230834,
        1 => 3008402,
        2 => 3008400,
        3 => 3231485,
        4 => 9025200,
      ),
    ),
  ),
);

// This is the top array key of the data you want to retrieve.
$data = $arr["accountData"];

for ($i = 0; $i < count($data); $i++){
    
    echo $data[$i]["AccountName"];
    
    // I created another loop to retrieve all the values in the accountBalance
    foreach ($data[$i]["accountBalance"] as $item){
       echo $item;
    }
   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM