WagnerMatosUK
WagnerMatosUK

Reputation: 4429

How to find values in an array and update the array to keep only these values

I have an array with values like these:

[oranges] => Array
    (
        [cost] => 0.56
        [quantity] => 6
    )

[pears] => Array
    (
        [cost] => 0.34
        [quantity] => 2
    )

I want to perform a search, get the search values using $_REQUEST (I've got to use $_REQUEST), then update the original array to display only the values the match the search. How can I do that?

PS: I need to be able to search the array based on item (oranges, pears, etc) and/or price and/or qty.

With my search form, the $_REQUEST is a variable with the in the following format:

Array
(
[item] => apple
[qty] => 2
[price] => 
)

Any suggestions?

Upvotes: 0

Views: 55

Answers (1)

Millard
Millard

Reputation: 1157

hello you might want to handle this a bit better but something like this will do it:

$array[$_REQUEST['item']] = array(
                              "cost" => $_REQUEST['price'], 
                              "quantity" => $_REQUEST['qty']
                            );

Where $array equals the array you want to change.

Again I would lay this out a bit better and add some validation. (also avoid using request its a big security issue)

Upvotes: 3

Related Questions