Suppose I have these 2 arrays of objects:
A:
[
[0] => (stdClass) {
id => '0',
....
},
[1] => (stdClass) {
id => '1',
....
},
,
[2] => (stdClass) {
id => '2',
....
}
]
B:
[
[0] => (stdClass) {
id => '0',
name => 'Hello',
a_id => '2'
....
},
[1] => (stdClass) {
id => '1',
name => 'World',
a_id => '2'
....
},
[2] => (stdClass) {
id => '2',
name => 'foo',
a_id => '0'
....
}
]
When B.a_id = A.id, the object from B belongs to A, and I want to add the object in an array to A, like this:
A:
[
...,
[2] => (stdClass) {
id => '2',
mapped_objs => [
[0] => (stdClass) {
id => '0',
name => 'Hello',
a_id => '2'
....
},
[1] => (stdClass) {
id => '1',
name => 'World',
a_id => '2'
....
},
]
}
...,
]
Is there an efficient algorithms or functions in PHP that will solve this problem? Do I have to do this in O(n^2)?