Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
convert one dimensional array to multidimesional

Hi I have following array list I need to convert it to something as follow group it by company and show categories as second array.

 [0] => Array
  (
    [company_id] => 1
    [avatar] => '092EF7DB-B478-2EF5-1CC5-4A46D285B5F0.jpg'
    [comp_name] => Hotel 1
    [categories] => Array
    (
      [cat_+category_id] => Reception
      [cat_+category_id] => Accommodation
    )

[1] => Array
      (
        [company_id] => 8
        [avatar] => ''
        [comp_name] => Hotel 2
        [categories] => Array
        (
          [cat_+category_id] => Reception

        )
etc 

original array below

0 => {#359 ▼
  +"category_id": 10
  +"company_id": 1
  +"cat": "Reception"
  +"comp_name": "Hotel 1"
  +"avatar": "092EF7DB-B478-2EF5-1CC5-4A46D285B5F0.jpg"
}
1 => {#354 ▼
  +"category_id": 12
  +"company_id": 1
  +"cat": "Accomodation"
  +"comp_name": "Hotel 1"
  +"avatar": "092EF7DB-B478-2EF5-1CC5-4A46D285B5F0.jpg"
}
2 => {#358 ▼
  +"category_id": 10
  +"company_id": 8
  +"cat": "Reception"
  +"comp_name": "Hotel 2"
  +"avatar": null
}
3 => {#361 ▼
  +"category_id": 10
  +"company_id": 23
  +"cat": "Reception"
  +"comp_name": "Hotel 3"
  +"avatar": null
}
4 => {#365 ▼
  +"category_id": 12
  +"company_id": 23
  +"cat": "Accomodation"
  +"comp_name": "Hotel 3"
  +"avatar": null

can you please advise how to achieve it in php thank you Dany

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Suppose $originalArr is your original one-dimensional array, the solution to convert your one-dimensional array to multi-dimensional array would be like this:

$resultArr = array();
foreach($originalArr as $arr){
    if(!isset($resultArr[$arr['company_id']])){
        $resultArr[$arr['company_id']] = array('company_id' => $arr['company_id'], 
                                            'avatar' => $arr['avatar'],
                                            'comp_name' => $arr['comp_name'],
                                            'categories' => array('cat_' . $arr['category_id'] => $arr['cat'])
                                        );
    }else{
        $resultArr[$arr['company_id']]['categories']['cat_' . $arr['category_id']] = $arr['cat'];
    }
}
$resultArr = array_values($resultArr);

// display $resultArr array
var_dump($resultArr);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!