Svedr
Svedr

Reputation: 589

Output Json Array inside a PHP array

I am having a slight issue in optimizing my code to output the values in the Json Array stored inside a PHP array.

This is the sample array

Array
(
    [0] => Array
    (
        [0] => ["string 1", "string 2", "string 3"]
    )

    [1] => Array
    (
        [0] => ["string 4", "string 5", "string 6"]
    )
    .....
)

This is the code I am using and it works

  $q = EstateTypes::query()->lists('column_value');

  $array1 = json_decode($q[0], true);
  $array2 = json_decode($q[1], true);
  $array3 = json_decode($q[2], true);

  foreach ($array1 as $key => $value) {
      echo $value . "<br>";
  }
   foreach ($array2 as $key => $value) {
      echo $value . "<br>";
  }
  foreach ($array3 as $key => $value) {
      echo $value . "<br>";
  }

I hope you see that the problem is this code is duplicating itself a lot. I've been trying to solve it properly but have been unable to. Would be glad for any help or further pointers on what I should do.

Upvotes: 0

Views: 699

Answers (1)

Michael Malov
Michael Malov

Reputation: 1887

$q = EstateTypes::query()->lists('column_value');

foreach($q as $item){
    $array = json_decode($item, true);
    foreach ($array as $value) {
        echo $value . "<br>";
    }
}

Upvotes: 3

Related Questions