我需要帮助!
我有一个像这样的数组,但我不知道如何为多维arry爆炸:
array() {
["id"]=>
string(1) "1"
["offer_id"]=>
string(3) "123"
["product_id"]=>
string(3) "123"
["product_x"]=>
string(3) "123"
["product_x_t"]=>
string(3) "123"
["product_x_z"]=>
string(3) "123"
["product_x_z_a"]=>
string(3) "123"
}
想要那样:
array(){
["id"]=>
string(1) "1"
["offer"]=>
string(3) "123"
["id"]=>
string(3) "123"
["product"]=>
string(3) "123"
["id"]=>
string(3) "123"
["x"]=>
string(3) "123"
["t"]=>
string(3) "123"
["z"]=>
string(3) "123"
["a"]=>
string(3) "123"
但我真的不知道!
答案 0 :(得分:2)
实际上,你不能这样做。值不能同时是字符串和数组。
$value['product']['x'] = "123";
$value['product']['x'] = array('t'=>'some_value', 'z' => 'some_value');
答案 1 :(得分:0)
好吧,我明白了......实际上它不是递归的,但我认为不会有那么多的情况......
function plode($val) {
$arr = array();
foreach($val as $key => $valval) {
$po = explode("_", $key);
$co = count($po);
switch($co){
case 1:
$arr[$key] = $valval;
break;
case 2:
$arr[$po[0]][$po[1]] = $valval;
break;
case 3:
$arr[$po[0]][$po[1]][$po[2]] = $valval;
break;
case 4:
$arr[$po[0]][$po[1]][$po[2]][$po[3]] = $valval;
break;
case 5:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]] = $valval;
break;
case 6:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]] = $valval;
break;
case 7:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]][$po[6]] = $valval;
break;
}
}
return $arr;
}
aray like array的结果(“values_sale_rent_x_y_z_j”=> 100,...)
array(8) {
["id"]=>
string(1) "1"
["offer"]=>
array(1) {
["id"]=>
string(3) "123"
}
["product"]=>
array(1) {
["id"]=>
string(3) "123"
}
["selected"]=>
string(1) "1"
["active"]=>
string(1) "1"
["amount"]=>
string(3) "123"
["payments"]=>
string(4) "sale"
["values"]=>
array(1) {
["rent"]=>
array(1) {
["min"]=>
array(2) {
["discount"]=>
string(1) "0"
["default"]=>
string(1) "0"
}
}
}
}
答案 2 :(得分:0)
决赛:
function plode($val, $bool = false, $deli='_') {
$arr = array();
foreach($val as $key => $valval) {
$po = explode($deli, $key);
$co = count($po);
switch($co){
case 1:
$arr[$key] = $valval;
break;
case 2:
$arr[$po[0]][$po[1]] = $valval;
break;
case 3:
$arr[$po[0]][$po[1]][$po[2]] = $valval;
break;
case 4:
$arr[$po[0]][$po[1]][$po[2]][$po[3]] = $valval;
break;
case 5:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]] = $valval;
break;
case 6:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]] = $valval;
break;
case 7:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]][$po[6]] = $valval;
break;
case 8:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]][$po[6]][$po[7]] = $valval;
break;
case 9:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]][$po[6]][$po[7]][$po[8]] = $valval;
break;
case 10:
$arr[$po[0]][$po[1]][$po[2]][$po[3]][$po[4]][$po[5]][$po[6]][$po[7]][$po[8]][$po[9]] = $valval;
break;
}
}
if($bool) return json_decode(json_encode($arr));
else return $arr;
}