0

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?

1
  • 1
    What you have tried?. What output you want? What output is coming? Commented Jan 29, 2013 at 14:15

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Tom, thanks for the reply. I'm not sure I follow though: would this method update the array? Also, when I tested it, there was no output and it gave me an error if I didn't add "cost" and "quantity" in the search. Ideally I should be able to search by "item" and/or "cost" and/or "quantity" but if I use only one of those it should also work.
that's fine if you search by the key so "item", will be required. Then run different updates depending on what's set using "isset"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.