Make a foreach with a JSON array

Published 5 months ago by Dev0ps

code

array:51 [▼
  0 => {#925 ▼
    +"year": "404 BC"
    +"text": "Peloponnesian War: Lysander's Spartan armies defeated the Athenians and the war ends."
    +"html": "<a href="https://wikipedia.org/wiki/Peloponnesian_War" title="Peloponnesian War">Peloponnesian War</a>: <a href="https://wikipedia.org/wiki/Lysander" title="Lys ▶"
    +"links": array:4 [▶]
  }
  1 => {#930 ▼
    +"year": "775"
    +"text": "The Battle of Bagrevand puts an end to an Armenian rebellion against the Abbasid Caliphate. Muslim control over Transcaucasia is solidified and its Islamization ▶"
    +"html": "The <a href="https://wikipedia.org/wiki/Battle_of_Bagrevand" title="Battle of Bagrevand">Battle of Bagrevand</a> puts an end to an <a href="https://wikipedia.or ▶"
    +"links": array:6 [▶]
  }
  2 => {#937 ▶}
  3 => {#943 ▶}
  4 => {#945 ▶}
  5 => {#949 ▶}
  6 => {#953 ▶}
  7 => {#956 ▶}
  8 => {#960 ▶}
  9 => {#964 ▶}
  10 => {#968 ▶}
  11 => {#971 ▶}
  12 => {#975 ▶}
  13 => {#982 ▶}
  14 => {#984 ▶}
  15 => {#992 ▶}
  16 => {#994 ▶}
  17 => {#1001 ▶}
  18 => {#1003 ▶}
  19 => {#1005 ▶}
  20 => {#1009 ▶}
  21 => {#1011 ▶}
  22 => {#1016 ▶}
  23 => {#1019 ▶}
  24 => {#1023 ▶}
  25 => {#1025 ▶}
  26 => {#1033 ▶}
  27 => {#1040 ▶}
  28 => {#1042 ▶}
  29 => {#1045 ▶}
  30 => {#1051 ▶}
  31 => {#1054 ▶}
  32 => {#1057 ▶}
  33 => {#1060 ▶}
  34 => {#1064 ▶}
  35 => {#1070 ▶}
  36 => {#1074 ▶}
  37 => {#1078 ▶}
  38 => {#1081 ▶}
  39 => {#1085 ▶}
  40 => {#1090 ▶}
  41 => {#1093 ▶}
  42 => {#1097 ▶}
  43 => {#1101 ▶}
  44 => {#1104 ▶}
  45 => {#1108 ▶}
  46 => {#1112 ▶}
  47 => {#1116 ▶}
  48 => {#1120 ▶}
  49 => {#1124 ▶}
  50 => {#1126 ▶}
]

i tried

 foreach($TimeQuoute['year']['text'][0]['html'] as $group) {
        echo $group['year'][0]['text'];
Cronix
Cronix
5 months ago (798,940 XP)

Convert it to a php array, and iterate over that. http://php.net/manual/en/function.json-decode.php

Dev0ps

i don't understand plz give me example

jlrdw
jlrdw
5 months ago (252,680 XP)

Are you serious, there is an example in the given link of a foreach loop.

  • Scroll down
Cronix
Cronix
5 months ago (798,940 XP)

Did you look at the examples in the php manual I linked to. It converts a json object to a php array, which then php can iterate over. You can't directly iterate over a json object in php. It needs to be something php understands, like an array. PHP doesn't speak json.

jlrdw
jlrdw
5 months ago (252,680 XP)

@Dev0ps of course you could write your own code to foreach over anything, but would require a lot of special string functions to just get the data. As example

<?php

namespace App\Helpers;

class DBS
{

    public static function __callStatic($method, $params)
    {
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        
        if (isset($trace[1]['class'])) {
            $class = $trace[1]['class'];
            $slh = strrpos($class, "\") + 1;
        } else {
            return null;
        }
        $model = "\App\Models\SMVC" . "\" . substr($class, $slh, -10) . "Model";
        $instance = new $model();

        return call_user_func_array(array($instance, $method), $params);
    }

}

A special function to figure out which class did the call.

Notice the

$slh = strrpos($class, "\") + 1;

But why when PHP has the nice json_decode built in.

The backslash \\ is double, forum code shows only one sorry.

Dev0ps

i paste its code on my controller error in this line

  $model = "\App\Models\SMVC" . "\" . substr($class, $slh, -10) . 

please tell me where i need to add it

jlrdw
jlrdw
5 months ago (252,680 XP)

No No that was an example of a string function, use what @Cronix said json-decode

Sorry for confusion.

I was just saying you could theoretically write a special function using php string functions to convert almost anything. But no need php has it built in.

arthurvillar

"Json Array" does not exist in the practical sense. Think of Json as just a string of text that looks like an object or array. For that reason, it is ready to be used as such when converted (decoded) into the language you want to use it with.

Please sign in or create an account to participate in this conversation.