Question

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

Was it helpful?

Solution 2

 foreach($data['pt_documents_data'] as $key => $title){
  echo $title['pt_doc_title']."<br />";
   }

OTHER TIPS

Try this:

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

}

reference $title instead of $data:

foreach($data['pt_documents_data'] as $key => $title){
      echo $title['pt_doc_title'];
   }
foreach($data['pt_documents_data'] as $doc){
  echo $doc['pt_doc_title'];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top