Richard
Richard

Reputation: 733

Php creating associative array from another array

I am using an array that I get from a database and I would like to create an associative array from that one.

Here is the code I am using:

        print_r($data_sites);
            // Format for easy use
        $data_sites_formatted = array();
        foreach ($data_sites as $key => $row) {
            
            if (empty($data_formatted[$row->project_loe_id])) {
                $data_sites_formatted[$row->project_loe_id] = array();
            }
            $data_sites_formatted[$row->project_loe_id][$row->name] = array();
            $data_sites_formatted[$row->project_loe_id][$row->name]['id'] = $row->id;
            $data_sites_formatted[$row->project_loe_id][$row->name]['quantity'] = $row->quantity;
            $data_sites_formatted[$row->project_loe_id][$row->name]['loe_per_quantity'] = $row->loe_per_quantity;

        }
        print_r($data_sites_formatted);

Here is the information I receive from the database:

Illuminate\Support\CollectionObject ( [items:protected] => Array ( 
[0] => stdClass Object ( [id] => 1 [project_loe_id] => 1 [name] => site [quantity] => 20 [loe_per_quantity] => 1 ) 
[1] => stdClass Object ( [id] => 2 [project_loe_id] => 1 [name] => switches [quantity] => 5 [loe_per_quantity] => 3 ) 
[2] => stdClass Object ( [id] => 3 [project_loe_id] => 2 [name] => site [quantity] => 20 [loe_per_quantity] => 1 ) 
[3] => stdClass Object ( [id] => 4 [project_loe_id] => 2 [name] => ap [quantity] => 5 [loe_per_quantity] => 3 ) 
[4] => stdClass Object ( [id] => 5 [project_loe_id] => 2 [name] => wireless [quantity] => 5 [loe_per_quantity] => 3 ) ) )

And when I do the transformation, here is the result:

Array ( 
[1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) 
[2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) ) 

I lost 3 lines and I really have no idea why. I can see when I look into details step by step that it deletes some lines from 1 iteration to the next:

Iteration:0
   Array ( 
   [1] => Array ( [site] => Array ( [id] => 1 [quantity] => 20 [loe_per_quantity] => 1 ) ) ) 
Iteration: 1
   Array ( 
   [1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) ) 
Iteration: 2
   Array ( 
   [1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) 
   [2] => Array ( [site] => Array ( [id] => 3 [quantity] => 20 [loe_per_quantity] => 1 ) ) ) 
Iteration: 3
   Array ( 
   [1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) 
   [2] => Array ( [ap] => Array ( [id] => 4 [quantity] => 5 [loe_per_quantity] => 3 ) ) ) 
Iteration: 4
   Array ( 
   [1] => Array ( [switches] => Array ( [id] => 2 [quantity] => 5 [loe_per_quantity] => 3 ) ) 
   [2] => Array ( [wireless] => Array ( [id] => 5 [quantity] => 5 [loe_per_quantity] => 3 ) ) ) 

You can see from iteration 0 to iteration 1, it deleted the line from iteration 0 and in iteration 1 I should have 2 lines.

Upvotes: 0

Views: 35

Answers (1)

IMSoP
IMSoP

Reputation: 97968

You have a typo here:

        if (empty($data_formatted[$row->project_loe_id])) {
            $data_sites_formatted[$row->project_loe_id] = array();
        }

$data_formatted is never defined (you meant to write $data_sites_formatted) so empty($data_formatted[$row->project_loe_id]) is always true, and always runs the next line, replacing anything previously added to $data_sites_formatted[$row->project_loe_id] with an empty array. So all you end up with is the last item to be added in each group.

Upvotes: 1

Related Questions