The results I retrieve from my database contains an array with cars ($resultsCars). The brand of each car has an ID. Var_dumping the array results in the following:
array(2) {
[0]=>
array(2) {
["brand"]=>
string(36) "2cb4c4d6-b706-e411-8ed9-0050568c4808"
["color"]=>
string(5) "black"
}
[1]=>
array(2) {
["brand"]=>
string(36) "2cb4c4d6-b706-e411-8ed9-0050568c4807"
["color"]=>
string(5) "white"
}
}
My goal is to replace the id with the actual name of the brand. For achieving this I will use an array which maps each id to the corresponding car name. Var_dumping this array ($arrData) results into the following:
array(3) {
[0]=>
object(some\path\here)#697 (2) {
["id":"some\path\here":private]=>
string(36) "2cb4c4d6-b706-e411-8ed9-0050568c4806"
["name":"some\path\here":private]=>
string(4) "Audi"
}
[1]=>
object(some\path\here)#697 (2) {
["id":"some\path\here":private]=>
string(36) "2cb4c4d6-b706-e411-8ed9-0050568c4807"
["name":"some\path\here":private]=>
string(8) "Mercedes"
}
[2]=>
object(some\path\here)#697 (2) {
["id":"some\path\here":private]=>
string(36) "2cb4c4d6-b706-e411-8ed9-0050568c4808"
["name":"some\path\here":private]=>
string(3) "BMW"
}
}
For creating a new array based on $resultsCars and with the brand id resolved, I have tried the following code:
$resultsMapped = [];
foreach ($resultsCars as $result) {
$result['brand'] = array_search($result['brand'], $arrData);
$resultsMapped[] = $result;
}
The brand fields in the resulting array, however, contain the boolean false. What am I doing wrong?
$arrDatacontains objects (that contains you values) instead of directly containing the values. I think array_search can't look into objects like that. Furthermore,idandnameareprivatein your object, you may have to use getters to check values... maybe have to make a custom function to search into$arrData2cb4c4d6-b706-e411-8ed9-0050568c4806and is present asidin $arrData$arrDatacontains some private properties