0

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?

6
  • the problem is that $arrData contains objects (that contains you values) instead of directly containing the values. I think array_search can't look into objects like that. Furthermore, id and name are private in your object, you may have to use getters to check values... maybe have to make a custom function to search into $arrData Commented May 21, 2015 at 8:00
  • i did not see the Brand Name present in the array Commented May 21, 2015 at 8:01
  • @karvin.developer the brand is 2cb4c4d6-b706-e411-8ed9-0050568c4806 and is present as id in $arrData Commented May 21, 2015 at 8:02
  • plus $arrData contains some private properties Commented May 21, 2015 at 8:02
  • oh! now i got it thnx @ Random Commented May 21, 2015 at 8:03

3 Answers 3

2

You are using array_search, which will return the index of the matched array element, not the element itself. More so, the brands array contains objects, with private varibles, so to access them you must have a getter function, and you can't access them as an array.

for example, you can't do this:

$arrData[0]['id']

If the object variables will be public, or you are using StdClass you can access them like this:

$arrData[0]->id

Otherwise, you must implement a getter function, and then you can use:

$arrData[0]->getId()

You can use array_map function, to map elements from one array to the other. Using array_map, you can use a callback function that will map the brand to the car.

For example, in case you have a getter function:

$arrData = [...] // Contains the brands array
$func = function($car) {
    foreach ($arrData as $brand) {
        if ($car['brand'] === $brand->getId()) {
            $car['brand'] = $brand; break;
        }
    }
    return $car;
};
array_map($func, $resultsCars);

After that, your $resultsCars array will have the brand object instend of the brand ID string.

Sign up to request clarification or add additional context in comments.

Comments

0

change the first line $resultsMapped = []; to $resultsMapped= array(); ..

1 Comment

Those are equivalent.
0

First change the $resultsMapped=[] declartion to $resultsMapped=array(); then change

foreach ($resultsCars as $result) {
     $result['brand'] = array_search($result['brand'], $arrData);
     $resultsMapped[] = $result;
 }

To

foreach ($resultsCars as $result) {
     $result['brand'] = array_search($result['id'], $arrData);
     $resultsMapped[] = $result;
 }

hope this will solve your problem

1 Comment

this is just an derivative description to make you understand which value is needed where you have put the appropriate key and value since we solve the query based on total assumption

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.