我有一个数组:
$errors = array(
"data.bedrooms[0].description"=> "Description cannot be blank",
"data.bedrooms[0].rentamount"=> "Rentamount cannot be blank",
"data.bedrooms[0].bondamount"=> "Bondamount cannot be blank",
"data.bedrooms[0].maxallowedoccupants"=> "Max allowed occupants cannot be blank",
"data.bedrooms[0].includedbills"=> "Included Bills cannot be blank",
"data.bedrooms[0].suitabilities"=> "Suitable for cannot be blank",
"data.bedrooms[0].area.value"=> "Area cannot be blank",
"data.bedrooms[1].description"=> "Description cannot be blank",
"data.bedrooms[1].rentamount"=> "Rentamount cannot be blank",
"data.bedrooms[1].bondamount"=> "Bondamount cannot be blank",
"data.bedrooms[1].maxallowedoccupants"=> "Max allowed occupants cannot be blank",
"data.bedrooms[1].includedbills"=> "Included Bills cannot be blank",
"data.bedrooms[1].suitabilities"=> "Suitable for cannot be blank",
"data.bedrooms[1].area.value"=> "Area cannot be blank"
);
我想把它转换成二维数组,看起来像:
$errorsarray = array( 0 => array(
'description'=>'Description cannot be blank',
'rentamount'=>'Rentamount cannot be blank',
'bondamount' => 'Bondamount cannot be blank'
),
1 => array(
'description'=>'Description cannot be blank',
'rentamount'=>'Rentamount cannot be blank',
'bondamount' => 'Bondamount cannot be blank'
)
);
我该怎么做。我尝试了很多不同的解决方案但是我无法获得理想的结果。任何想法?
编辑:这是我有多远:
$newArray = array();
foreach ($errors as $key => $value) {
$parts = substr($key, 13);
$property = substr($parts, 4);
$formnumber = substr($parts,1,1);
$newArray[$formnumber] = array($property => $value);
}
var_dump($newArray);exit;
答案 0 :(得分:2)
您可以使用正则表达式...
$output = [];
foreach ($errors as $key => $value) {
preg_match('/^data\.bedrooms\[(\d+)\]\.(.*)$/', $key, $matches);
$output[$matches[1]][$matches[2]] = $value;
}
var_dump($output);
答案 1 :(得分:0)
你可以试试这个
$errors = array(
"data.bedrooms[0].description"=> "Description cannot be blank",
"data.bedrooms[0].rentamount"=> "Rentamount cannot be blank",
"data.bedrooms[0].bondamount"=> "Bondamount cannot be blank",
"data.bedrooms[0].maxallowedoccupants"=> "Max allowed occupants cannot be blank",
"data.bedrooms[0].includedbills"=> "Included Bills cannot be blank",
"data.bedrooms[0].suitabilities"=> "Suitable for cannot be blank",
"data.bedrooms[0].area.value"=> "Area cannot be blank",
"data.bedrooms[1].description"=> "Description cannot be blank",
"data.bedrooms[1].rentamount"=> "Rentamount cannot be blank",
"data.bedrooms[1].bondamount"=> "Bondamount cannot be blank",
"data.bedrooms[1].maxallowedoccupants"=> "Max allowed occupants cannot be blank",
"data.bedrooms[1].includedbills"=> "Included Bills cannot be blank",
"data.bedrooms[1].suitabilities"=> "Suitable for cannot be blank",
"data.bedrooms[1].area.value"=> "Area cannot be blank"
);
$errorsarray = array();
$i = 0;
$k = 0;
foreach($errors as $key => $val){
$tmpKeyArr = null;
$tmpKeyArr = explode('.', $key);
if($tmpKeyArr[1] == "bedrooms[".$i."]" && $tmpKeyArr[2] == "description"){
$errorsarray[$i]['description'] = $val;
}
if($tmpKeyArr[1] == "bedrooms[".$i."]" && $tmpKeyArr[2] == "rentamount"){
$errorsarray[$i]['rentamount'] = $val;
}
if($tmpKeyArr[1] == "bedrooms[".$i."]" && $tmpKeyArr[2] == "bondamount"){
$errorsarray[$i]['bondamount'] = $val;
}
if($tmpKeyArr[1] == "bedrooms[".$i."]" && $tmpKeyArr[2] == "maxallowedoccupants"){
$errorsarray[$i]['maxallowedoccupants'] = $val;
}
if($tmpKeyArr[1] == "bedrooms[".$i."]" && $tmpKeyArr[2] == "includedbills"){
$errorsarray[$i]['includedbills'] = $val;
}
$k++;
if($k%7 == 0){
$i++;
}
}
echo "<pre>"; print_r($errorsarray);