How to access the internal level array values in a foreach loop in following scenario?

107 Views Asked by At

I've following array called $data:

Array
(
    [op] => edit
    [pt_id] => 4
    [form_submitted] => yes
    [submit] => Update
    [pt_documents_data] => Array
        (
            [0] => Array
                (
                    [pt_doc_title] => Test Document
                    [pt_doc_id] => 6
                    [pt_doc_file_iname] => 
                )

            [1] => Array
                (
                    [pt_doc_title] => New Joining
                    [pt_doc_id] => 7
                    [pt_doc_file_iname] => 
                )

            [2] => Array
                (
                    [pt_doc_title] => Hallo Jolly
                    [pt_doc_id] => 
                    [pt_doc_file_iname] => FAQ.doc
                )

        )

)

Now I want to access every array and elements contained in it coming under the subarray [pt_documents_data]. I tried to print the first value using foreach but I'm not able to print it. Not understanding where I'm going wrong. Can anyone please help me in accessing the internal arrays one by one? Thanks in advance. My attempt is as follows:

   foreach($data['pt_documents_data'] as $key => $title){
      echo $data[$key]['pt_doc_title']; die;
   }

Actually it is expected to print the value Test Document but it's not printing anything. In a same manner I want to access every element from all the arrays coming under array [pt_documents_data].

4

There are 4 best solutions below

0
Suhel Meman On BEST ANSWER
 foreach($data['pt_documents_data'] as $key => $title){
  echo $title['pt_doc_title']."<br />";
   }
1
user2936213 On

Try this:

foreach($data['pt_documents_data'] as $key => $title){
  echo $title['pt_doc_title'];

}

0
Bhadra On
foreach($data['pt_documents_data'] as $doc){
  echo $doc['pt_doc_title'];
}
0
robi On

reference $title instead of $data:

foreach($data['pt_documents_data'] as $key => $title){
      echo $title['pt_doc_title'];
   }