Reputation: 1102
Please help me. I do not know what is the best way to data join these data with PHP code.
Here are my data.
ArrayA(array('id'=>'1','Date'=>'datehere'),array('id'=>'2','Date'=>'datehere'));
ArrayB(array('ArrayAId'=>'1','personal infos'=>'other data'),array('ArrayAId'=>'1','personal infos 2'=>'other data'),array('ArrayAId'=>'2','personal infos'=>'other data'),array('ArrayAId'=>'2','personal infos 2'=>'other data'));
I wanted to show all ArrayA
items with their associate ArrayB
Items as the following.
ArrayA-item1
ArrayB item1 with ArrayA-item1 Id
ArrayB item2 with ArrayA-item1 Id
ArrayA-item2
ArrayB item3 with ArrayA-item2 Id
ArrayB item4 with ArrayA-item2 Id
The thing is ArrayB
includes around 100 rows for each row from ArrayA
.
So when I tried to get 50 ArrayA
Items, ArrayB
got around 5000 rows.
That is why I cannot use the following approach due to performance issue.
for(ArrayA)
{
for(ArrayB){
//Get all associate rows according to current ArrayA.
}
}
The above code make my page dead because it have to loop 5000 rows for 50 times. So please help me and point to the correct direction to display all these data.
Thanks in advance. Kindly let me know if you need more details.
Upvotes: 1
Views: 82
Reputation: 1102
I achieve to solve the problem with array_filter. I shared the code in case if someone encounter the problem like I do.
for($i=0; $i < count($mainArray) ; $i++){
$currentId = mainArray[i]["id"];
$output = array_filter($subArray, function ($value) use ($currentId) { return $value['mainId'] == $currentId; });
foreach($output as $data)
{
//print data
}
}
Upvotes: 0
Reputation: 21
Instead of going for loops try array functions.
You could use array functions with callback such as array_intersect_uassoc()
array_combine()
etc..
Check PHP manual page on array functions. You could craft your answer with these functions
Upvotes: 2