0

As the title says. I'm trying to store every value from $_POST['spell'.$j.'icon'.$i] inside a 2d array and I'm curious if there is any way to make this array look the same way so $spell[$j]icon[$i]

$spellicon=array();
for($i=1;$i<=$_POST['champ_number']; $i++){
    for($j=1; $j<=$noofspellschamp[$i]; $j++){
        $spellicon[$j][$i]=$_POST['spell'.$j.'icon'.$i];

    }
}

EDIT Bit of code from the previous site. Data is nested so I need it to be in 2d array.

for($i=1;$i<=$champ_number; $i++){
                    echo $_POST['champno'.$i].'<br/>';
                    //echo '<input type="hidden" value="'.$_POST['champno'.$i].'" name="champno'.$i.'">';
                    $champno[$i] = $_POST['champno'.$i];
                    //echo '<input type="hidden" value="'.$_POST['noofspellschamp'.$i].'" name="noofspellschamp'.$i.'">';
                    $noofspellschamp[$i] = $_POST['noofspellschamp'.$i];
                    for($j=1; $j<=$_POST['noofspellschamp'.$i]; $j++){
                            echo '<select name="spell'.$j.'icon'.$i.'">';
                                echo '<option value="Passive">Passive</option>';
                                echo '<option value="Q" selected>Q</option>';
                                echo '<option value="W">W</option>';
                                echo '<option value="E">E</option>';
                                echo '<option value="R">R</option>';
                            echo '</select>';
                        if($i==1&&$j==1){
                            echo '<input type="text" name="spell'.$j.'title'.$i.'" placeholder="Spell '.$j.' name" required  autofocus><br/>';
                            }
                        else{
                        echo '<input type="text" name="spell'.$j.'title'.$i.'" placeholder="Spell '.$j.' name" required><br/>';
                        }

Edit 2 Print_R($_POST)

Array (
  [formid] => 6
  [champ_number] => 2
  [spell1icon1] => Q
  [spell1title1] => spell1
  [champno1spellno1] => 1
  [description111] => sakdop
  [change111] => buff
  [spell1icon2] => Q
  [spell1title2] => sdkop
  [champno2spellno1] => 1
  [description121] => sadas
  [change121] => buff
  [noofspellschamp] => {"1":"1","2":"1"}
  [champno] => {"1":"Garen","2":"Katarina"}
  [patch] => 0.03
 )
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
higeat
  • 55
  • 6
  • 1
    Can you post the output from `print_r($_POST)` so we can better understand what you're working with, and an example of what you want the structure of the output array to be? This very unclear right now. – Michael Berkowski Jan 02 '15 at 22:54
  • I've added the code from previous site where the $POST comes from – higeat Jan 02 '15 at 22:59
  • It would _really_ help to see the `$_POST` array though, as you receive it on the script you're doing this loop with. Please show `print_r($_POST)`, so we don't have to mentally parse and substitute variables into the originating form to make sense of what it does. – Michael Berkowski Jan 02 '15 at 23:16

2 Answers2

2

There's no such thing as $spell[$j]icon[$i] in PHP. The approach you're using($spellicon[$j][$i]) is the only one in standard PHP (I mean, without classes).

But it'll be better if you do it like this: $spellicon[$i][$j], because it's the champion that has the spells, and not the other way around.

g.carvalho97
  • 332
  • 1
  • 9
1

Code below is simplification. Check included topic. Just use

$_POST['spellicon'][$j][$i]

or

$_POST['spell'][$j]['icon'][$i]

and the you will have same naming convention as your variable.

Considering form building it will be cleaner and you can group your data with good manner. If you don't know how to send multidimensional arrays via POST, check this topic

Community
  • 1
  • 1
Arius
  • 1,387
  • 1
  • 11
  • 24