1

Thank you for looking at and taking time to answer my question!

I would like to use a multi-dimensional array in a form on my site and then access it using the keys; Like so.

<form method="post" action="somescript.php">
    <input type="text" name="name[1][title]">
    <input type="text" name="name[1][descritpion]">
</form>

I would then like to access both the index, since that is the ID, and the value of the given _post'ed element. Something like

$keys = array_keys($_POST['name']);
 foreach($keys as $id)
 {
      echo "title: " . $_POST['name']['title'][$id];
      echo "description: " . $_POST['name']['description'][$id];
      echo "id: " . $id;
 }

Now, the above looks nice and it prints out the correct $id but thats about it. I assume I am putting the "title" or "description" array calls in the wrong place but cannot figure it out. Could someone kindly point me in the right direction?

2
  • i'm not sure on where you want to go with thatm but it seems like there's some missing Quotes on your array keys in $_POST["description"]["title"][$id] Commented Sep 7, 2011 at 17:42
  • Ah your right, although they are present in my code, just not my example above. Thanks for spotting it tho! Commented Sep 7, 2011 at 17:48

1 Answer 1

3

In your PHP, you are looking for $_POST['name']['title'][$id], but in your HTML you have name[1][title].

These are not the same array. You either need to change the HTML to name[title][1], or the PHP to $_POST['name'][$id]['title'].

It doesn't matter which you use, just be consistent, though I suggest using name[title][1] as that may be easier to work with.

Sign up to request clarification or add additional context in comments.

1 Comment

Superstar, soon as I thought about it I realised you was right.

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.