简体   繁体   English

PHP:映射数组结果

[英]PHP: Mapping array results

The results I retrieve from my database contains an array with cars ( $resultsCars ). 我从数据库中检索到的结果包含一个包含汽车的数组( $resultsCars )。 The brand of each car has an ID. 每辆汽车的品牌都有一个ID。 Var_dumping the array results in the following: Var_dumping数组将导致以下结果:

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. 我的目标是将ID替换为品牌的实际名称。 For achieving this I will use an array which maps each id to the corresponding car name. 为了实现这一点,我将使用一个将每个ID映射到相应汽车名称的数组。 Var_dumping this array ( $arrData ) results into the following: Var_dumping此数组( $arrData )的结果如下:

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: 为了创建一个基于$resultsCars并解析了品牌ID的新数组,我尝试了以下代码:

 $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 . 但是,结果数组中的brand字段包含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. 您正在使用array_search,它将返回匹配的数组元素的索引,而不是元素本身。 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. 更重要的是,brands数组包含带有私有变量的对象,因此要访问它们,您必须具有getter函数,并且不能将它们作为数组访问。

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: 如果对象变量将是公共变量,或者您正在使用StdClass,则可以按以下方式访问它们:

$arrData[0]->id

Otherwise, you must implement a getter function, and then you can use: 否则,必须实现getter函数,然后可以使用:

$arrData[0]->getId()

You can use array_map function, to map elements from one array to the other. 您可以使用array_map函数将元素从一个数组映射到另一个数组。 Using array_map, you can use a callback function that will map the brand to the car. 使用array_map,您可以使用将品牌映射到汽车的回调函数。

For example, in case you have a getter function: 例如,如果您具有getter函数:

$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. 之后,您的$ resultsCars数组将具有品牌ID字符串的品牌对象。

change the first line $resultsMapped = []; 更改第一行$ resultsMapped = []; to $resultsMapped= array(); 到$ resultsMapped = array(); .. ..

First change the $resultsMapped=[] declartion to $resultsMapped=array(); 首先将$resultsMapped=[] resultsMapped $resultsMapped=[]声明更改为$resultsMapped=[] resultsMapped $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 希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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