2

I've a PHP multidimensional array like:

array(
    [0] => array("code"=>code1, "value"=>val1, "operation"=>Add),
    [1] => array("code"=>code2, "value"=>val2, "operation"=>Remove),
    [2] => array("code"=>code3, "value"=>val3, "operation"=>Edit)
)

If I know code and value, how can I get the operation array index value corresponding to that entry. Eg: If I pass code1 and val1, then it should return the value Add. I can use foreach(), but I'm looking for some other faster and efficient way to get it.

Can anyone help me? Thanks in advance.

4
  • 1
    There's no faster and efficient way. Commented Mar 9, 2018 at 11:42
  • Do you get the structure like that from an endpoint, or do you build it somewhere? Commented Mar 9, 2018 at 11:42
  • Are there dupicate entries for code, value or operation possible in the array? Commented Mar 9, 2018 at 11:44
  • @PhilippMaurer. No. Duplicate entries are not possible and I'm constructing this array using jquery and passing to php page. Commented Mar 9, 2018 at 11:49

2 Answers 2

2

Simple foreach with a break/return when found will be O(n) in worst case, O(1) in best.

Modifying source array as:

array(
    'code1:val1' => Add,
    'code2:val2' => Remove,
    'code3:val3' => Edit
)

will give you O(1) with accessing like $arr['code1:val1'].

Solution, for example, with array_filter will be O(n) always.

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

1 Comment

Additional information: The reason that "Solutions, for example, with array_filter will be O(n) always." is, that the data-structure you (question author) built in the first place is handling all information as values. Therefore it is not optimized for a search in any way. This solution is an optimal solution, because the search relevant information are used as keys instead, so they can be used in a search.
0

just run a foreach. It will be an O(n) search on worst case.

function getOperation($code,$value){
    foreach($array as $key => $item){
        if($item["code"]===$code && $item["value"] === $value)
            return $item["operation"];
    }
    return;
}

2 Comments

The question author wrote: " I can use foreach(), but I'm looking for some other faster and efficient way to get it." Therefore your answer is not an answer.
okay, I understand you. I think I addressed that however on the first line that say "just run a foreach". Nothing is gonna be faster than that. The code fragment was just an overkill in case someone else would need the fragment. I wasn't looking forward to make my contribution the correct answer for the question but rather a universal solution for anybody else who would find themself in such a scenario. Thank you though for flagging it the right way. It's not a solution to the given question.

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.