在具有两个值的数组中搜索

时间:2019-07-18 22:11:49

标签: php arrays

美好的一天,

我正在尝试搜索数组。

我在数组($package)中有两个ID(productId,secondaryProductId),还有其他一些数据:

array (size=7)
  'title' => string 'Compleet' (length=8)
  'productId' => string '102' (length=5)
  'price' => string '45.75' (length=5)
  'secondaryProductId' => string '150' (length=5)
  'secondaryPrice' => string '58.75' (length=5)

我有一个包含数组($availableProducts)的数组:

array (size=2)
  0 => 
    array (size=3)
      'product_id' => int 102
      'description' => string 'some description for 102'
      'order_link' => string 'some/link/102'
  1 => 
    array (size=3)
      'product_id' => int 150
      'description' => string 'some description for 150'
      'order_link' => string 'some/link/150'
  2 => 
    array (size=3)
      'product_id' => int 160
      'description' => string 'some description for 160'
      'order_link' => string 'some/link/160'
  3 => 
    array (size=3)
      'product_id' => int 140
      'description' => string 'some description for 140'
      'order_link' => string 'some/link/140'

我想要实现的是在$availableProducts数组中搜索两个产品ID。

如果它们存在(例如存在描述和链接的102和150),我希望在两个产品ID都位于的第一个数组中设置描述和链接。

赞:

array (size=7)
  'title' => string 'Complete' (length=8)
  'productId' => string '102' (length=5)
  'price' => string '45.75' (length=5)
  'secondaryProductId' => string '150'
  'secondaryPrice' => string '58.75'
  'description' => string 'some description for 102'
  'order_link' => string 'link/for/102'
  'secondaryDescription' => string 'some description for 150'
  'secondaryOrder_link' => string 'some/link/150'

如果其中之一不存在,我希望删除其ID和价格或将其设置为空字符串(无关紧要)。

赞:

array (size=7)
  'title' => string 'Complete' (length=8)
  'productId' => string '102' (length=5)
  'price' => string '45.75' (length=5)
  'secondaryProductId' => string ''
  'secondaryPrice' => string ''
  'description' => string 'some description for 102'
  'order_link' => string 'link/for/102'

到目前为止,我已经尝试过使用foreach循环进行的操作:

$finalArray = array();
foreach ($availableProducts as $product) {
    if ($package['productId'] == $product->product_id) {
        $package['orderLink'] = $product->order_link;
        $finalArray[] = $package;
    }

    if ($package['secondaryProductId'] == $product->product_id) {
        $package['secondaryOrderLink'] = $product->order_link;
        $finalArray[] = $package;
    }
}

但是它不起作用。起初,设置orderLink看起来不错,但随后又翻了一番:

array (size=2)
  0 => 
    array (size=5)
      'productId' => string '102' (length=5)
      'price' => string '35.75' (length=5)
      'secondaryProductId' => string '150' (length=5)
      'secondaryPrice' => string '48.75' (length=5)
      'orderLink' => string 'link/for/102'
  1 => 
    array (size=6)
      'productId' => string '102' (length=5)
      'price' => string '35.75' (length=5)
      'secondaryProductId' => string '150' (length=5)
      'secondaryPrice' => string '48.75' (length=5)
      'orderLink' => string 'link/for/102'
      'secondaryOrderLink' => string 'link/for/150'

我尝试了一下,也尝试了in_array。但是我显然做错了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您在这里(截短的版本)

$availableProducts = [
    0 => [
      'product_id' =>  102,
      'description' =>  'some description for 102',
      'order_link' =>  'some/link/102',
   ], 1 => [
      'product_id' =>  150,
      'description' =>  'some description for 150',
      'order_link' =>  'some/link/150'
   ], 2 => [
      'product_id' =>  160,
      'description' =>  'some description for 160',
      'order_link' =>  'some/link/160'
    ]
 ];

 $array1 = [
  'title' =>  'Compleet',
  'productId' =>  '102',
  'price' =>  '45.75',
  'secondaryProductId' =>  '150',
  'secondaryPrice' =>  '58.75',
];

print_r(
    array_intersect_key(
        array_column($availableProducts, null, 'product_id'),
        array_flip([$array1['productId'], $array1['secondaryProductId']])
   )
);

输出

Array
(
    [102] => Array
        (
            [product_id] => 102
            [description] => some description for 102
            [order_link] => some/link/102
        )

    [150] => Array
        (
            [product_id] => 150
            [description] => some description for 150
            [order_link] => some/link/150
        )

)

Sandbox

  • 注意-仅当product_id$availableProducts中是唯一的时才有效

基本上,array_column($availableProducts, null, 'product_id')将使用product_id作为键来重组数组(没有重复的ID),然后您就可以在数组与具有2个ID的数组之间找到交集。之所以选择它,是因为我懒于将它们设置为密钥,array_intersect_key(显然)必须使用它们。

array_column-对于数组之间的比较非常强大。您可以使用几种巧妙的技巧...

上面显示了如何仅过滤所需的2个,但是,如果您只想更新原始数组(在此示例中为$array1),则很简单:

$res = array_column($availableProducts, null, 'product_id');

$array1['productId'] = $res[$array1['productId']]['description'];
$array1['secondaryProductId'] = $res[$array1['secondaryProductId']]['description'];

输出

Array
(
    [title] => Compleet
    [productId] => some description for 102
    [price] => 45.75
    [secondaryProductId] => some description for 150
    [secondaryPrice] => 58.75
)

Sandbox

以产品ID为键(请参见示例1的输出),我们可以使用该值直接通过product_id获取多维数组的该部分。

显然现在也很容易做到这一点

 $res = array_column($availableProducts, null, 'product_id');

 $array1['productId'] =['description' => $res[$array1['productId']]['description'], 'order_link' => $res[$array1['productId']]['order_link']];
 $array1['secondaryProductId'] =['description' => $res[$array1['secondaryProductId']]['description'], 'order_link' => $res[$array1['secondaryProductId']]['order_link']];

输出:

Array
(
    [title] => Compleet
    [productId] => Array
        (
            [description] => some description for 102
            [order_link] => some/link/102
        )

    [price] => 45.75
    [secondaryProductId] => Array
        (
            [description] => some description for 150
            [order_link] => some/link/150
        )

    [secondaryPrice] => 58.75
)

我不确定您想要的最终结果是什么,但这也许会对您有所帮助。

享受