从php中的索引数组创建一个关联的多维数组

从php中的索引数组创建一个关联的多维数组

问题描述:

I have an array which currently looks like this: its from a form with names like name="buyer_id[]"

Array ( 
   [product_id] => 
      Array ( 
       [0] => 1 
       [1] => 2 
       [2] => 4 
           ) 
   [name] => 
     Array ( 
      [0] => Paper Choco 
      [1] => Paper Fan Vanilla 
      [2] => Pink Prom 
           ) 
  [staff_id] => 
    Array ( 
      [0] => 1 
      [1] => 1 
      [2] => 1 
          ) 
  [category] => 
    Array ( 
      [0] => agent 
      [1] => agent 
      [2] => agent 
         ) 
  [date] => 
    Array ( 
      [0] => 2014-08-22 
      [1] => 2014-08-22 
      [2] => 2014-08-22 
         ) 
  [price] => 
    Array ( 
      [0] => 188 
      [1] => 887 
      [2] => 17 
         )

Now i want it to look like the array below so i can pass it into database:

   array(
     array(
      'product_id' => 1 ,
      'name' => 'Paper Choco',
      'category'=>'agent',
      'staff_id'=> 1,
      'date'=> '2014-08-22'
),
   array(
      'product_id' => 2 ,
      'name' => 'Paper Fan Vanilla ',
      'category'=>'agent',
      'staff_id'=> 1,
      'date'=> '2014-08-22'
),
   array(
      'product_id' => 4 ,
      'name' => 'Pink Prom ',
      'category'=>'agent',
      'staff_id'=> 1,
      'date'=> '2014-08-22'
)
);

I believe this is very much possible but i can not figure a way around it yet

我有一个目前如下所示的数组: 来自名称如name =“buyer_id []的表单 “ p>

 数组(
 [product_id] => 
数组(
 [0] => 1 
 [1] => 2 
 [  2] => 4 
)
 [名称] => 
数组(
 [0] => Paper Choco 
 [1] => Paper Fan Vanilla 
 [2] =>  Pink Prom 
)
 [staff_id] => 
数组(
 [0] => 1 
 [1] => 1 
 [2] => 1 
)
 [  category] ​​=> 
数组(
 [0] => agent 
 [1] => agent 
 [2] => agent 
)
 [date] => 
数组 (
 [0] => 2014-08-22 
 [1] => 2014-08-22 
 [2] => 2014-08-22 
)
 [price] =&gt  ; 
数组(
 [0] => 188 
 [1] => 887 
 [2] => 17 
)
  code>  pre> 
 
 现在我希望它看起来像下面的数组,所以我可以将它传递到数据库 : p> 
 
 
  array(
 array(
'product_id'=>  1,
'name'=>  'Paper Choco',
'category'=>'agent',
'staff_id'=>  1,
'date'=>  '2014-08-22'
),
 array(
'product_id'=> 2,
'name'=>'Paper Fan Vanilla',
'category'=>'agent'  ,
'staff_id'=> 1,
'日期'=>'2014-08-22'
),
数组(
'product_id'=> 4,
'name'=  >'粉红色舞会',
'类别'=>'代理',
'staff_id'=> 1,
'日期'=>'2014-08-22'
)
)  ; 
  code>  pre> 
 
 

我相信这很有可能,但我无法想办法呢 p> div>

$result = array();
foreach ($array as $key => $subarray) {
    foreach ($subarray as $i => $value) {
        if (!isset($result[$i])) {
            $result[$i] = array();
        }
        $result[$i][$key] = $value;
    }
}

As an alternative:

$data = array ( 
    'product_id' => array ( 1, 2, 4 ),
    'name' => array ( 'Paper Choco', 'Paper Fan Vanilla', 'Pink Prom' ),
    'staff_id' => array ( 1, 1, 1 ),
    'category' => array ( 'agent', 'agent', 'agent', ),
    'date' => array ('2014-08-22', '2014-08-22', '2014-08-22' ),
    'price' => array ( 188, 887, 17 ),
);

function transpose($array) {
    array_unshift($array, null);
    return call_user_func_array('array_map', $array);
}

$result = transpose($data);
array_walk(
    $result,
    function (&$value) use ($data) {
        $value = array_combine(array_keys($data), $value);
    }
);

var_dump($result);