PHP关联数组 [英] PHP associative Array

查看:164
本文介绍了PHP关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP关联数组

$asd['a'] = 10;
$asd['b'] = 1;
$asd['c'] = 6;
$asd['d'] = 3;

我想这个排序其价值的基础,并获得键值为第4个值。

i want to sort this on basis of its value and to get the key value for the first 4 values.

如何能做到这一点在PHP ???

how can i do that in php ???

推荐答案

ASORT()应保持索引关系

asort($asd);

在这之后,一个简单的foreach可以让你在未来四年值

After that, a simple foreach can get you the next four values

$i = 0;
foreach ($asd as $key=>$value)
{
  if ($i >= 4) break;
  // do something with $asd[$key] or $value
  $i++;
}

这篇关于PHP关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆