搜索关联数组以返回多个值[重复]

问题描述 投票:0回答:1

Hi Im试图从PHP的关联数组中检索数据。对于我的一生,我不知道该怎么做。

数组的例子是

数组([0] =>数组([productID] => 637[bottleRef] => 10)

[1] => Array
    (
        [productID] => 637
        [bottleRef] => 25
    )

[2] => Array
    (
        [productID] => 637
        [bottleRef] => 50
    )

[3] => Array
    (
        [productID] => 637
        [bottleRef] => 100
    )

[4] => Array
    (
        [productID] => 638
        [bottleRef] => 10
    )

[5] => Array
    (
        [productID] => 638
        [bottleRef] => 25
    )

[6] => Array
    (
        [productID] => 639
        [bottleRef] => 50
    )

.....

我想做的是返回所有具有相同productID的bottleRefs即。对于productID = 638,应返回值10和25。对于productID = 637的输入,应返回值10、25、50和100。

非常感谢您提供任何帮助:)保罗

php arrays associative-array
1个回答
0
投票

我使用“ 600”作为我要搜索的示例ID

    $bottles[] = array ( 'id' => 600, 'bottleref' => 'beer' );
    $bottles[] = array ( 'id' => 600, 'bottleref' => 'corona' );
    $bottles[] = array ( 'id' => 680, 'bottleref' => 'beer' );
    $bottles[] = array ( 'id' => 600, 'bottleref' => 'miller' );
    $bottles[] = array ( 'id' => 630, 'bottleref' => 'beer' );

$find_id = '600';

foreach($bottles as $key => $value){
    if ($value['id'] == $find_id){
        $result[] = $value;
    }
}

if($result){
    var_dump($result);
}
© www.soinside.com 2019 - 2024. All rights reserved.