PHP,使用数组进行操作
我从帖子中得到如下数组:PHP,使用数组进行操作,php,arrays,Php,Arrays,我从帖子中得到如下数组: '0,id_product' => '217' '0,checked' => '217' '0,price_setup_original' => '1.00' '1,price_setup_original' => '7.00' '1,price_setup_res' => '7.00' '1,price_monthly_original' => '50.00' '2,price_set
'0,id_product' => '217'
'0,checked' => '217'
'0,price_setup_original' => '1.00'
'1,price_setup_original' => '7.00'
'1,price_setup_res' => '7.00'
'1,price_monthly_original' => '50.00'
'2,price_setup_res' => '0.00'
'2,price_monthly_original' => '40.00'
'2,price_monthly_res' => '40.00'
我想像这个例子一样迭代它,需要帮助
array = (
0 -> array(
'id_product' => '217',
'checked' => '217',
'price_setup_original' => '1.00'
),
1 -> array(
'id_product' => '217',
'checked' => '217',
'price_setup_original' => '1.00'
),
);
需要帮助
foreach
将由您选择:
array = (
0 -> array(
'id_product' => '217',
'checked' => '217',
'price_setup_original' => '1.00'
),
1 -> array(
'id_product' => '217',
'checked' => '217',
'price_setup_original' => '1.00'
),
);
$array = [
'0,id_product' => '217' ,
'0,checked' => '217',
'0,price_setup_original' => '1.00' ,
'1,price_setup_original' => '7.00' ,
'1,price_setup_res' => '7.00' ,
'1,price_monthly_original' => '50.00' ,
'2,price_setup_res' => '0.00' ,
'2,price_monthly_original' => '40.00' ,
'2,price_monthly_res' => '40.00',
];
$result = [];
foreach($array as $key=>$item)
{
$key = explode(',', $key);
$result[$key[0]][$key[1]] = $item;
}
foreach
将由您选择:
$array = [
'0,id_product' => '217' ,
'0,checked' => '217',
'0,price_setup_original' => '1.00' ,
'1,price_setup_original' => '7.00' ,
'1,price_setup_res' => '7.00' ,
'1,price_monthly_original' => '50.00' ,
'2,price_setup_res' => '0.00' ,
'2,price_monthly_original' => '40.00' ,
'2,price_monthly_res' => '40.00',
];
$result = [];
foreach($array as $key=>$item)
{
$key = explode(',', $key);
$result[$key[0]][$key[1]] = $item;
}
尝试:
试试:
完成:
<?
$arr = array(
'0,id_product' => '217',
'0,checked' => '217',
'0,price_setup_original' => '1.00',
'1,price_setup_original' => '7.00',
'1,price_setup_res' => '7.00',
'1,price_monthly_original' => '50.00',
'2,price_setup_res' => '0.00' ,
'2,price_monthly_original' => '40.00',
'2,price_monthly_res' => '40.00');
$newArray = array();
foreach($arr as $key => $item){
$tmp = explode(',',$key);
$newArray[$tmp[0]][$tmp[1]]=$item;
}
print_r($arr);
print_r($newArray);
?>
完成:
<?
$arr = array(
'0,id_product' => '217',
'0,checked' => '217',
'0,price_setup_original' => '1.00',
'1,price_setup_original' => '7.00',
'1,price_setup_res' => '7.00',
'1,price_monthly_original' => '50.00',
'2,price_setup_res' => '0.00' ,
'2,price_monthly_original' => '40.00',
'2,price_monthly_res' => '40.00');
$newArray = array();
foreach($arr as $key => $item){
$tmp = explode(',',$key);
$newArray[$tmp[0]][$tmp[1]]=$item;
}
print_r($arr);
print_r($newArray);
?>
如果您想在对象中发布,请尝试以下操作
<?
$arr = array(
'0,id_product' => '217',
'0,checked' => '217',
'0,price_setup_original' => '1.00',
'1,price_setup_original' => '7.00',
'1,price_setup_res' => '7.00',
'1,price_monthly_original' => '50.00',
'2,price_setup_res' => '0.00' ,
'2,price_monthly_original' => '40.00',
'2,price_monthly_res' => '40.00');
foreach($arr as $key => $item){
$tmp = explode(',',$key);
$newArray[$tmp[0]]->{$tmp[1]}=$item;
}
?>
如果您想在对象中发布,请尝试以下操作
<?
$arr = array(
'0,id_product' => '217',
'0,checked' => '217',
'0,price_setup_original' => '1.00',
'1,price_setup_original' => '7.00',
'1,price_setup_res' => '7.00',
'1,price_monthly_original' => '50.00',
'2,price_setup_res' => '0.00' ,
'2,price_monthly_original' => '40.00',
'2,price_monthly_res' => '40.00');
foreach($arr as $key => $item){
$tmp = explode(',',$key);
$newArray[$tmp[0]]->{$tmp[1]}=$item;
}
?>
您想特别更改此数组的结构,还是想要一种通用方法?您已经尝试了什么?您想特别更改此数组的结构,还是想要一种通用方法?您已经尝试了什么?