如何从chekboxes将单个数组转换为multidimention数组

时间:2016-03-24 07:15:44

标签: php arrays

如果我在复选框html中有这样的数组:

Array
(
   [0] => 1
   [1] => 2
)

如何使这个数组像这样

Array (
  array(
     'ID_REQUEST' => 1 //push new element (constant)
     'ID_TIPE' => 1 // this is from first elemnt of array below
  ),   
  array(
     'ID_REQUEST' => 1 //push new element (constant)
     'ID_TIPE' => 2 // this is from second elemnt of array below
  ),   

)

先谢谢。

3 个答案:

答案 0 :(得分:1)

试试这个

$new_array= array();
for($i=0;$i<count($first_array);$i++)
{
    $newdata =  array ('ID_REQUEST' => 'constant1',
    'ID_TIPE' => $first_array[$i]
);
    array_push($new_array[$i],$newdata);
}

答案 1 :(得分:1)

$new_arr =array();
foreach($your_array as $k=>$v){
    $temp['ID_REQUEST'] = 1;
    $temp['id_type'] = $v;
    array_push($new_arr,$temp);
}

print_r($new_arr);

希望它有所帮助:)

答案 2 :(得分:1)

最短的一个:

// $arr is your initial array
$new_arr = array_map(function($v){ return ['ID_REQUEST' => 1, 'ID_TIPE' => $v]; }, $arr);