Output values from multidimensional array in php

Hi Everybody,

I am learning php multidimension array and i really need help to output the values. I have but could not get it right, any help will be amazing; here is my code.

[php] <?php

session_start();

if (!isset($_SESSION['images'])) {
  
   $_SESSION['images'] = array();
}

if (isset($_POST['submit'])) {
  
$test[]= $_POST['name'];
$test[]= $_POST['qty'];

 $_SESSION['images'][] = $test; 

}

  foreach ($_SESSION['images'] as $nom) {
      
      foreach ($nom as $val) {
        echo $val["name"];
      }
  }
?>

[/php]
I want to output the values like this one:

name quantity
church 1

Thanks in advance.

Well, first, you can’t retrieve a key without setting that key. That means that you can not echo a
key such as $val[“name”] without setting it first.

Next, if you use blank keys, you need to use them to retrieve the data. What I am talking about is your
assignments of the posted values for name and qty. You do not set a key when you assign them to the
$test[] array. Therefore, you are just placing values into it and no keys. This is actually a good thing as
it saves a lot of storage, but, you would need to use a different format to retrieve the data. You can do
this with multiple indices such as $test[][]…

Arrays are normally saved using the “key”==> “value” format. So, if you wish to be able to pull the values
for a “key” you need to store that key name also. Like $test[“name”]=$_POST[“name”]; Then, you can get
it back the way that you did in your code.

To preview your data, you can display the arrays using the print_r function or the var_dump function.
Just print_r($_SESSION[“images”]); or var_dump($_SESSION[“images”]); These show your layout of
your arrays, one just shows the data and one shows the structure. I would suggest debugging it a little
and see what you are storing into your array and I think it will make better sense to you.

Where is your form code?

Sponsor our Newsletter | Privacy Policy | Terms of Service