1

I have a form on my website. When I submit it to my PHP script I use var_dump($_POST); to show all the form data that has been submitted. The array follows below.

I'd like to use PHP to run a foreach loop on the following array and group them by the index.

Expected Output:

1 - 111 - 1
2 - 222 - 2
3 - 333 - 3

So each row would have the relevant name, source and level based on the integer in the array element.

Can someone explain how this is done?

array(11) {
  ["source_name0_id"]=>
  string(1) "1"
  ["source_code0_id"]=>
  string(3) "111"
  ["source_level0_id"]=>
  string(1) "1"
  ["source_name1_id="]=>
  string(1) "2"
  ["source_code1_id="]=>
  string(3) "222"
  ["source_level1_id="]=>
  string(1) "2"
  ["source_name2_id="]=>
  string(1) "3"
  ["source_code2_id="]=>
  string(3) "333"
  ["source_level2_id="]=>
  string(1) "3"
  ["submit"]=>
  string(6) "Submit"
}

Thank you!

michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190

2 Answers2

0
$foreach($array /* or in the case $_POST? */ as $key => $value) {
    /// loop...
}

or just

$foreach($array as $value) {
    /// loop...
}

Edit: So, to get the output you're expecting, you do have another easy way, if you know the max ahead of time:

$for($i = 0; $i<$max /* if you have it */; $i++) {
  echo $_POST["source_name" . $i . "_id"] . " - " .
     $_POST["source_code" . $i . "_id"] . " - " .
  $_POST["source_level" . $i . "_id"] . "<br>\n";
}

  • Thanks for this. How do I determine `$max` value? – michaelmcgurk Feb 02 '19 at 17:01
  • 1
    That's the tricky part. The easy way is to edit the script to track the max (or the number of rows submitted) and pass it to $_POST["maxRows"] or something like that. –  Feb 02 '19 at 17:03
  • Actually, better idea. You could consider using [name arrays](https://stackoverflow.com/a/4689190/10957435) instead, and that'd make things a whole lot easier. –  Feb 02 '19 at 17:04
  • 1
    Why are you showing two empty `foreach(){}` constructs as `$foreach(){}`? Not sure this accomplishes the goal of the OP. – jibsteroos Feb 02 '19 at 17:05
  • @Chipster - Great idea, will do! :-) Your edited script works great *however* when I run it, I just get 1 - 111 - 1
    - -
    - -
    – michaelmcgurk Feb 02 '19 at 17:05
  • @michaelmcgurk That's strange. I realize I was missing a dot. Not sure that's the problem though. I just added it. –  Feb 02 '19 at 17:08
  • 1
    @jibsteroos The original post simply asked how to loop through all the element of the array, not necessarily make a given output as it now does. –  Feb 02 '19 at 17:09
  • @Chipster Thanks for all your efforts here. I think I just need to tweak this part and it'll be working: `for($i = 0; $i<$max; $i++) {`. This doesn't give me the expected output. – michaelmcgurk Feb 02 '19 at 17:10
  • 1
    @michaelmcgurk Hm. That's weird. Can you post the echo statement you're using? –  Feb 02 '19 at 17:14
  • My bad, I missed a bit. It works beautifully. Thank you, @Chipster! – michaelmcgurk Feb 02 '19 at 17:17
  • 1
    @michaelmcgurk Yay! I'm glad. –  Feb 02 '19 at 17:20
-1
<?php

$array = [
    "source_name0_id"=> "1",
    "source_code0_id"=> "111",
    "source_level0_id"=> "1",
    "source_name1_id="=> "2",
    "source_code1_id="=> "222",
    "source_level1_id="=> "2",
    "source_name2_id="=> "3",
    "source_code2_id="=> "333",
    "source_level2_id="=> "3",
    "submit"=> "Submit"
];

$output = [];

foreach($array as $key=>$val)
{
    $output[] = [$val, $key, $val];
}

echo '<pre>';
print_r($output);