php中的键,值和数组

时间:2014-01-25 06:51:12

标签: php symfony

我正在使用symfony2.3,

我想知道如何在id基础上获得rewardPoints以及这个id所持有的位置 数组在0到4之间?

喜欢:

我已经有了Logged User Id: -

 id = 8; 

在数组中我得到了这个: -

Array
(
 [0] => Array
    (
        [rewardPoints] => 790
        [id] => 1
    )

 [1] => Array
    (
        [rewardPoints] => 35
        [id] => 8
    )

 [2] => Array
    (
        [rewardPoints] => 25
        [id] => 14
    )

 [3] => Array
    (
        [rewardPoints] => 10
        [id] => 15
    )

 [4] => Array
    (
        [rewardPoints] => 5
        [id] => 16
    )

)

抱歉我的英文。

感谢。

1 个答案:

答案 0 :(得分:2)

希望这会对你有所帮助

$counter = 0;
foreach($yourArray as $subArray){
     if($subArray['id'] == 8){         // here set the id what you need
        $position =   $counter;
         $revertPoint = $subArray['rewardPoints']; //here you set the rewardPoints into variable
      }
      $counter++;
}

echo $revertPoint;//result 
echo $position; // position in main array