utkarsh2k2
utkarsh2k2

Reputation: 1096

How to convert an array into multi dimensional array

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;

Upvotes: 1

Views: 49

Answers (2)

D Coder
D Coder

Reputation: 572

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

Upvotes: 0

James
James

Reputation: 111

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

Upvotes: 2

Related Questions