Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following array.

Array
(
    [0] => Array
        (
            [product] => p1
            [item] => q1
            [date] => d1
            [status] => N
        )

    [1] => Array
        (
            [product] => p2
            [item] => q2
            [date] => d2
            [status] => Y
        )

    [2] => Array
        (
            [product] => p1
            [item] => q3
            [date] => d3
            [status] => N
        )
)

From this array, I want date from above with the given product and value.

For Example, I have product = p2 and item = q2 as a string. Now from this array, I want a date from this array which has product = p2 and item = q2.

So I need output from the above example is: d2

I tried array_column and array_search but can't find value from product and item both.

Please help me out with this in PHP.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can iterate over your products and check required criteria with if statement.

foreach ($products as $product) {
    if ('p2' === $product['product'] && 'q2' === $product['item']) {
        var_dump($product['date']);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...