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

有帮助吗?

解决方案 2

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

其他提示

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'];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top