John Davidson

PHP efficient way to combine two associative arrays into one multidimensional associative array

0 comments
Message:


This is the first problem:


I have two Associative Arrays, one containing sales persons and one containing clients.


  $salesPersons = array(
array(
"id" => "1",
"name" => "Mr Smith",
"email" => "[email protected]",
"clients" => array()
),
array(
"id" => "2",
"name" => "James Bond",
"email" => "[email protected]",
"clients" => array()
)
);

$clients = array(
array(
"id" => "1",
"name" => "Lucifer Enterprises",
"salesPersonId" => "1"
),
array(
"id" => "2",
"name" => "Charlies Chocolate Factory",
"salesPersonId" => "1"
),
array(
"id" => "3",
"name" => "Geckos Investments",
"salesPersonId" => "2"
),
);



I want to map $salesPersons['id'] to clients['salesPersonId'] by ID and return a multidimensional associative array like this:


  $result_i_want = array(
array(
"id" => "1",
"name" => "Mr Smith",
"email" => "[email protected]",
"clients" => array(
array(
"id" => "1",
"name" => "Lucifer Enterprises",
),
array(
"id" => "2",
"name" => "Charlies Chocolate Factory",
),
)
),
array(
"id" => "2",
"name" => "James Bond",
"email" => "[email protected]",
"clients" => array(
array(
"id" => "3",
"name" => "Geckos Investments",
),
)
)
);

My solution to the first problem


I have solved it using nested foreach-loops and array_push


  $result = array();

foreach ($clients as $c_record) {
foreach ($salesPersons as $s_record) {
if ($c_record['salesPersonId'] == $s_record['id']) {
array_push($s_record['clients'], array(
"id" => $c_record['id'],
"name" => $c_record['name']
));
array_push($result, $s_record);
}
}
}

The remaining problem


This solution doesn't seem to be very efficient.
For each client record I check all sales persons to see if there is a match. I think the number of computations are:


no. of clients * no. of sales persons

I have a huge database and also need to add even more dimensions by mapping projects to the clients and deliverables to the projects. I think this could pose a problem.


Question


Is there a more efficient way to get the same result?



My Answer:
'name' => 'John Doe', 'sales' => 5000 ), array( 'name' => 'Jane Smith', 'sales' => 7000 ) );$clients = array( array( 'name' => 'Client A', 'revenue' => 2000 ), array( 'name' => 'Client B', 'revenue' => 3000 ) );I want to combine these two arrays into one multidimensional associative array where each sales person is associated with their clients. The desired output should look like this:array( array( 'name' => 'John Doe', 'sales' => 5000, 'clients' => array( array( 'name' => 'Client A', 'revenue' => 2000 ), array( 'name' => 'Client B', 'revenue' => 3000 ) ) ), array( 'name' => 'Jane Smith', 'sales' => 7000, 'clients' => array( // clients for Jane Smith ) ));What is the most efficient way to achieve this in PHP?

Rate this post

3 of 5 based on 5097 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us