-1
$foods = array(
 'meat' => array('burgers', 'steak', 'sausages', 'liver'),
 'cake' => array('victorian', 'chocolate', 'fruit', 'fudge')
 );

I want just the meat values in reverse alphabetical order and the function should return an array.

and I don't want the liver to be included in the array.

How can I do that?

I have done this but for some reasons I didn't like it. Looks very simple to me. can any one please suggest me a better way of doing that. This is how I did it.

    function items($arr){

$x = $arr['meat'];
sort($x);
$z = array_reverse($x);
foreach ($z as $v){
 if($v == 'liver')continue;
   $r[] = $v ;   
}
return $r;

}

$foods = array(
'meat' => array('burgers', 'steak', 'sausages', 'liver'),
'cake' => array('viian', 'chocolate', 'fruit', 'fudge')
);

$x = items($foods);
var_dump($x);

?>
4

2 回答 2

1

看看rsort

$meats = $foods['meat'];
rsort($meats);
if ($key = array_search('liver', $meats)) 
     unset($meats[$key]);
于 2013-08-12T11:29:07.403 回答
0

首先,对数组进行排序:

sort($input)

这应该这样做:

$reversed = array_reverse($input);

并删除一些:

for($i=0;$i<count($reversed);$i++) {
    if($reversed[$i] == "liver")
       unset($reversed[$i]);        
}
于 2013-08-12T11:31:47.827 回答