简体   繁体   中英

PHP: Mapping array results

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?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM