-1

I have php array like this which generated from php controller.

Array
(
  [data001] => Array
    (
      [0] => stdClass Object
        (
          [page_id] => 204725966262837
          [type] => WEBSITE01
        )

      [1] => stdClass Object
        (
          [page_id] => 163703342377960
          [type] => COMMUNITY02
        )
      )

  [data001] => Array
    (
      [0] => stdClass Object
        (
          [page_id] => 204725966000037
          [type] => WEBSITE02
        )

      [1] => stdClass Object
        (
          [page_id] => 163703342377960
          [type] => COMMUNITY02
        )
      )
)

I want to echo these two array within same foreach loop. I Tried like this but it didn't worked.

foreach ($results as $result) {
    echo $result->type; 
    echo "<br>";
} 

But I didn't want to use foreach ($results['data001'] as $result) Because I Have to write two foreach loops.

user3732708
  • 623
  • 1
  • 9
  • 20
  • Is there always 2 arrays within the parent array? – GrumpyCrouton Feb 19 '18 at 17:46
  • Why wouldn't you want to iterate with multiple `foreach` statements? It's the legit way to read 2D+ arrays. –  Feb 19 '18 at 17:49
  • `foreach ($results['data001'] as $key=>$result) { ... $results['data001'][$key->type] ... }` – splash58 Feb 19 '18 at 17:52
  • @aendeerei because I wannt to print various group by values for one record. in my conroller side `$job_category_data = array( 'common' => $this->auth_model->get_random_job_categories("JobCategoryId"), 'location1' => $this->auth_model->get_random_job_categories("Title") ); $data['job_categories'] = $job_category_data; $this->load->view('index', $data);` – user3732708 Feb 19 '18 at 17:53
  • @splash58, then I can print only 'data001'. I want to print data001 and data002 both. – user3732708 Feb 19 '18 at 17:56
  • @GrumpyCrouton , yes – user3732708 Feb 19 '18 at 17:57
  • @user3732708 Sorry `{ ... $results['data002'][$key]->type and $result` – splash58 Feb 19 '18 at 17:58
  • I didn't get it @splash58 – user3732708 Feb 19 '18 at 18:06
  • Possible duplicate of [Loop through a multidimensional array](https://stackoverflow.com/questions/18499518/loop-through-a-multidimensional-array) – Richard Feb 19 '18 at 20:38

2 Answers2

0

If you have this Array Try this,

<?php
// Multidimensional array
$masterArray = array(
    "0" => array(
        "page_id" => "204725966262837",
        "type" => "WEBSITE01",
    ),
    "1" => array(
        "page_id" => "163703342377960",
        "type" => "COMMUNITY02",
    ),
    "2" => array(
        "page_id" => "204725966000037",
        "type" => "WEBSITE02",
    ),
    "3" => array(
        "page_id" => "204725966000037",
        "type" => "WEBSITE02",
    )
);
// Printing all the keys and values one by one

$keys = array_keys($masterArray); 

 for($i = 0; $i < count($masterArray); $i++) {

    foreach($masterArray[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
}
?>
yaxe
  • 367
  • 5
  • 26
Mohit Kumar
  • 952
  • 2
  • 7
  • 18
0

Here is with just one foreach, this works for you?

foreach ($results as $result) {
    for ($i=0; $i < count($result); $i++) { 
        echo $result[$i]->type; 
        echo "<br>";
    }
}