0

给定以下 php 数组

$a = array(
    array('a'=>'111','b'=>'two','c'=>'asdasd'),
    array('a'=>'111','b'=>'one','c'=>'sdvsdfs'),
    array('a'=>'111','b'=>'three','c'=>'vbndfgn'),
    array('a'=>'222','b'=>'nine','c'=>'dfhfnd')
);

如何仅返回每个数组键“a”的最后一个数组?

期望的结果:

$new = array(
    array('a'=>'111','b'=>'three','c'=>'vbndfgn'),
    array('a'=>'222','b'=>'nine','c'=>'dfhfnd')
);
4

1 回答 1

3

如果我是你,我会尝试以更好的格式存储它,以便更轻松地检索它。但是,如果您坚持使用格式,请尝试:

$a = array(
    array('a'=>'111','b'=>'two','c'=>'asdasd'),
    array('a'=>'111','b'=>'one','c'=>'sdvsdfs'),
    array('a'=>'111','b'=>'three','c'=>'vbndfgn'),
    array('a'=>'222','b'=>'nine','c'=>'dfhfnd')
);

$tmp = array();
foreach ($a as $value) {
    $tmp[$value['a']] = $value;
}

$new = array_values($tmp);

print_r($new);
于 2012-11-16T22:04:01.093 回答