PHP数组和保存键的最高值

时间:2016-12-27 15:09:47

标签: php

我有阵列

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    inflater.inflate(R.menu.home, menu);
    searchItem = menu.findItem(R.id.menu_item_search_places);
    searchItem.setIcon(FontIconDrawable.inflate(getContext(), R.xml.ic_search));
    searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnSuggestionListener(this);
    searchView.setOnQueryTextListener(this);
    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
                // my stuff are here
           return true;
        }
    });
}

我需要从点数最高的数组中获取Array ( [1] => Array ( [message_id] => 1, [points] => 3, ) [2] => Array( [message_id] => 2, [points] => 2, ) [3] => Array( [message_id] => 3, [points] => 2, ) ) 值。

2 个答案:

答案 0 :(得分:0)

您可以在数组中循环并检查点以找到最高点

$points = 0;
foreach($array as $val) {
    if($val['points'] > $points) {
        $points = $val['points'];
        $message_id = $val['message_id'];
    }
}

echo $message_id;
//prints 1

Live sample

答案 1 :(得分:0)

你可以这样做:

$array = array(
    1 => array(
        'message_id' => 1,
        'points' => 3,
    ),
    2 => array(
        'message_id' => 2,
        'points' => 2,
    ),
    3 => array(
        'message_id' => 3,
        'points' => 2,
    ),
);

$highest = 0;

foreach ($array as $key => $arr) {
    if($arr['message_id'] > $highest) {
        $highest = $arr['message_id'];
    }
}

echo "Highest: " . $highest;

$highest号码的输出为:3。

See working example