基于键将5个数组与php组合在一起

I have 5 arrays like this

print_r($_POST['ponumber']);
print_r($_POST['man_part_number']);
print_r($_POST['model']);
print_r($_POST['damaged']);
print_r($_POST['serial_number']);
print_r($_POST['skid']);

which produces

Array
(
    [0] => 111111
    [1] => 111111
    [2] => 111111
    [3] => 111111
)
Array
(
    [0] => 11C2555
    [1] => 11C2555
    [2] => 11C2555
    [3] => 11C2555
)
Array
(
    [0] => FORMS PRINTER 2500 SERIES
    [1] => FORMS PRINTER 2500 SERIES
    [2] => FORMS PRINTER 2500 SERIES
    [3] => FORMS PRINTER 2500 SERIES
)
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
)
Array
(
    [0] => CNCQ202070
    [1] => CNCQ210800
    [2] => MY5203B1KQ
    [3] => CN1863Q0PQ
)
Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1
)

I need to combine them based on keys so that I get 4 arrays total that look like this

Array
(
    [0] => 111111
    [1] => 11C2555
    [2] => FORMS PRINTER 2500 SERIES
    [3] => 0
    [4] => CNCQ202070
 )

and so on...

It needs to match each key in each array and combine them into one array. I was trying array_combine and array_merge but I don't think that was the right way to go. Anyone?

$items = array();

for($x = 0; $x < count($_POST['ponumber']); $x++){
    $item = array($_POST['ponumber']);
    $item[] = $_POST['ponumber'][$x];
    $item[] = $_POST['man_part_number'][$x];
    $item[] = $_POST['model'][$x];
    $item[] = $_POST['damaged'][$x];
    $item[] = $_POST['serial_number'][$x];
    $item[] = $_POST['skid'][$x];
    $items[] = $item;
}

Will give an array of the arrays you need. Note that this assumes that all arrays are the same size. You may need to add some checks if this is not the case.

Have you tried using a nested foreach loop to make five new arrays?

Granted it is long and tedious but it could be a start if you need it in a rush?

you can use this:

$counter = 0;
$temp = array();
 if(isset($_POST)){ // check if $_POST isset.

foreach($_POST as $key=>$myarr)
    {
       foreach($myarr as $k=>$value)
       {

          $temp[$counter] = $value;
          break;
       }
       $counter++;
    }
    }

to check out you can do:

var_dump($temp);

Hope that I helped

If you can structure the form as such, you'll get a more friendly $_POST array:

<form>
  <input name="data[0][ponumber]" />
  <input name="data[0][man_part_number]" />
  <input name="data[0][model]" />
  <input name="data[0][damaged]" />
  <input name="data[0][serial_number]" />
  <input name="data[0][skid]" />

  <input name="data[1][ponumber]" />
  <input name="data[1][man_part_number]" />
  <input name="data[1][model]" />
  <input name="data[1][damaged]" />
  <input name="data[1][serial_number]" />
  <input name="data[1][skid]" />
</form>

$_POST will look like:

'data' => array(
  0 => array(
    'ponumber' => '111111',
    'man_part_number' => '11C2555',
    'model' => 'FORMS PRINTER 2500 SERIES',
    'damaged' => '0',
    'serial_number' => 'CNCQ202070',
    'skid' => '1',
  ), ...
)