1

I have the following code sample:

$arr = array(
  array(
    'packid'=> '1', 'total' => '30'),
  array(
    'packid'=> '2', 'total' => '20')
);

$arr is in a loop

 foreach ($pack->memberPack as $memPack) {
            if($memPack->packId = $search_arr_key)
            //get the `total` value then do something
        }

and $memPack object like this:

array(1) {
["memberPack"]=>
array(1) {
  [0]=>
  object(MemberPack)#290 (6) {
    ["errors"]=>
    NULL
    ["attributes":"ActiveRecord\Model":private]=>
    array(3) {
      ["memberPackId"]=>
      int(1)
      ["packId"]=>
      int(1)
      ["memberId"]=>
      int(14369)
    }  
  }
}

}

How can i search the key packId equal to $memPack->packId and get the match total? (ex. if packId =2 and get the value 20 )

2
  • Do you mean besides a loop? Commented Dec 30, 2016 at 3:18
  • updated the question, the $arr is in a loop Commented Dec 30, 2016 at 3:34

3 Answers 3

1

For better performance, here is a good implementation.

$array = array_combine(array_column($arr, 'packId'), array_column($arr, 'total'));
$total = $array[$memPack->packId];
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are looking for a loop that finds the result according to the packid you select.

<?php

$arr = array(
  array(
    'packid'=> '1', 'total' => '30'),
  array(
    'packid'=> '2', 'total' => '20')
);

$whateverPackidYouWant = 2;
$answer;

for ($i = 0; $i < count($arr); $i++) {

if ($arr[$i]['packid'] == $whateverPackidYouWant) {
   $answer = $arr[$i]['total'];
}

}

echo $answer;

?>

Please let me know if this is what you were looking for.

Comments

1

Simply iterate the array and check if the subarray contains the matching packid and total items. Break the loop on the first match:

$packid = 2;

foreach ($arr as $k => $v) {
  if ($v['packid'] == $packid && isset($v['total'])) {
    $total = $v['total'];
    break;
  }
}

Another approach (less optimal, but may look elegant to some people):

$key = array_search($packid, array_column($arr, 'packid'));
if ($key !== false && isset($arr[$key]['total'])) {
  $total = $arr[$key]['total'];
}

where array_column builds an array from the packid values ([1, 2]) in the order that matches the order of subarrays in $arr; array_search returns the index of the first occurrence of $packid in the array of packid values (and thus, the key of the subarray in $arr). The rest of the code fetches the total.

I have added this example for completeness. Don't use it as it is suboptimal.

2 Comments

THX, but I wonder why the second approach is less optimal ? can see some statistical number?
@DoranWu, array_column builds an array from all the items in $arr (O(n)). Then array_search, in worst case, iterates all the items in the generated array (O(n)). Thus, in the worst case it is a O(2n) operation. And the first approach (foreach) breaks the loop on the first match which is O(n) in worst case. Besides, the code with array_search and array_column is less readable, and even less flexible

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.