Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Boakye51's avatar

How to loop and fetch this type of array?

Can someone help me to loop and fetch this type of array.

Testing Code

    $chart_data = $client->coins()->getMarketChart('bitcoin', 'usd', 'max');
    $coinchart = collect($chart_data);
    $take_data = $coinchart->except(['market_caps', 'total_volumes'])->all(['prices']);
    dd($take_data);

Code Array Resutlts is

array:1 [▼ "prices" => array:2632 [▼ 0 => array:2 [ …2] 1 => array:2 [ …2] 2 => array:2 [ …2] 3 => array:2 [ …2] 4 => array:2 [ …2] 5 => array:2 [ …2] 6 => array:2 [ …2] 7 => array:2 [ …2] 8 => array:2 [ …2] 9 => array:2 [ …2] 10 => array:2 [ …2] 11 => array:2 [ …2] 12 => array:2 [ …2] 13 => array:2 [ …2] 14 => array:2 [ …2] 15 => array:2 [ …2] 16 => array:2 [ …2] 17 => array:2 [ …2] 18 => array:2 [ …2] 19 => array:2 [ …2] 20 => array:2 [ …2] 21 => array:2 [ …2] 22 => array:2 [ …2] 23 => array:2 [ …2] 24 => array:2 [ …2] 25 => array:2 [ …2] 26 => array:2 [ …2] 27 => array:2 [ …2] 28 => array:2 [ …2] 29 => array:2 [ …2] 30 => array:2 [ …2] 31 => array:2 [ …2] 32 => array:2 [ …2] 33 => array:2 [ …2] 34 => array:2 [ …2] 35 => array:2 [ …2] 36 => array:2 [ …2] 37 => array:2 [ …2] 38 => array:2 [ …2] 39 => array:2 [ …2] 40 => array:2 [ …2] 41 => array:2 [ …2] 42 => array:2 [ …2] 43 => array:2 [ …2] 44 => array:2 [ …2] 45 => array:2 [ …2] 46 => array:2 [ …2] 47 => array:2 [ …2] 48 => array:2 [ …2] 49 => array:2 [ …2] 50 => array:2 [ …2] 51 => array:2 [ …2] 52 => array:2 [ …2] 53 => array:2 [ …2] 54 => array:2 [ …2] 55 => array:2 [ …2] 56 => array:2 [ …2] 57 => array:2 [ …2] 58 => array:2 [ …2] 59 => array:2 [ …2] 60 => array:2 [ …2] 61 => array:2 [ …2] 62 => array:2 [ …2] 63 => array:2 [ …2] 64 => array:2 [ …2] 65 => array:2 [ …2] 66 => array:2 [ …2] 67 => array:2 [ …2]

0 likes
4 replies
Snapey's avatar

Can you understand what you posted?

Boakye51's avatar

Sorry, all that i mean is i want to loop and get the arrays inside the prices.

jlrdw's avatar

See https://laracasts.com/discuss/channels/code-review/how-to-get-a-record Has example.

An array needs to be consistent.

Also when asking, display data properly formated, here is an example: quote

array:3 [▼
  "status" => 200
  "data" => array:7 [▼
    "id" => 5
    "salescode" => "sales1"
    "username" => "Sales1"
    "role" => "sales"
    "parent_id" => 10
    "created_at" => "2020-07-04 19:57:23"
    "updated_at" => "2020-07-03 02:13:18"
  ]
  "super" => array:2 [▼
    0 => array:1 [▼
      0 => array:5 [▼
        "id" => 10
        "parent_id" => 11
        "salescode" => "tl"
        "username" => "Leader"
        "role" => "leader"
      ]
    ]
    1 => array:2 [▼
      0 => array:5 [▼
        "id" => 11
        "parent_id" => 12
        "salescode" => "sp01"
        "username" => "Supervisor"
        "role" => "supervisor"
      ]
      1 => array:5 [▼
        "id" => 26
        "parent_id" => 12
        "salescode" => "sp02"
        "username" => "Supervisor"
        "role" => "supervisor"
      ]
    ]
  ]
]

unquote

If I wanted the super data from above, I would:

$s = $this->array_flatten($decoded["super"]);

//then

$keys = array_keys($s);

//then

    for ($i = 0; $i < count($s); $i++) {
        foreach ($s[$keys[$i]] as $key => $value) {
            echo $key . " : " . $value . "<br>";
        }
        echo "-----------------------------------";
        echo "<br>";
    }

Which gives:

id : 10
parent_id : 11
salescode : tl
username : Leader
role : leader
-----------------------------------
id : 11
parent_id : 12
salescode : sp01
username : Supervisor
role : supervisor
-----------------------------------
id : 26
parent_id : 12
salescode : sp02
username : Supervisor
role : supervisor
-----------------------------------

The array_flatten part is something I fount on stackoverflow a while back:

   public function array_flatten($a, $flat = []) {
        $entry = [];
        foreach ($a as $key => $el) {
            if (is_array($el)) {
                $flat = $this->array_flatten($el, $flat);
            } else {
                $entry[$key] = $el;
            }
        }
        if (!empty($entry)) {
            $flat[] = $entry;
        }
        return $flat;
    }

But there are other ways. And in your blade use your ul li or however you display.

Note, $decoded came from some json data, converted to array via:

$decoded = json_decode($mydata, true);
lucasrueda's avatar

Can you show the output of $take_data->toArray( ) ?

Please or to participate in this conversation.