1

I have an array:

$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" 
);

I want to convert it into a two dimensional array which should look like :

$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'
    )
);

How do I do that. I've tried many different solutions but I cannot get the desired result. Any Ideas?

EDIT: This is how far i've got:

$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;
4
  • to save your time, if you able rewrite the code that was responsible to produce the original array just do it. Commented Oct 23, 2017 at 11:27
  • why have maxallowedoccupants key been missed? Commented Oct 23, 2017 at 11:32
  • why have suitabilities key been missed? Commented Oct 23, 2017 at 11:33
  • @Roman i just gave you an idea how it should look. here all key values are needed Commented Oct 23, 2017 at 11:35

2 Answers 2

2

You could use a regular expression...

$output = [];

foreach ($errors as $key => $value) {

    preg_match('/^data\.bedrooms\[(\d+)\]\.(.*)$/', $key, $matches);

    $output[$matches[1]][$matches[2]] = $value;
}

var_dump($output);
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.