-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.

9
  • Is there always 2 arrays within the parent array? Commented Feb 19, 2018 at 17:46
  • Why wouldn't you want to iterate with multiple foreach statements? It's the legit way to read 2D+ arrays. Commented Feb 19, 2018 at 17:49
  • foreach ($results['data001'] as $key=>$result) { ... $results['data001'][$key->type] ... } Commented Feb 19, 2018 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); Commented Feb 19, 2018 at 17:53
  • @splash58, then I can print only 'data001'. I want to print data001 and data002 both. Commented Feb 19, 2018 at 17:56

2 Answers 2

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>";
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

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>";
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.