PHP数组值输出并获得类似的值


PHP - array values out and get similar values

我有一个类似的数组

Array ( [item1] => pack1 [amount1] => 1 [price1] => 25 [amount2] => 1 [price2] => 45 [item3] => pack3 [amount3] => 4 [price3] => 65 [sender] => Submit )

我需要得到item1&数组中的item3,然后用删除上面的项目

str_replace

然后用找到最大的数字

max

只是最难的第一部分,有什么想法吗?

foreach通过数组,如果键包含项,则获取/设置值。还要检查该值,看看它是否大于前一个值,以获得最大

$a = array( "item1" => "pack1", "amount1" => 1, "price1" => 25, "amount2" => 1, "price2" => 45, "item3" => "pack3");
$b = array();
$pattern = '/item[1-9]/';
$max_index = 0;
foreach($a as $key => $value){
    if(preg_match($pattern, $key, $matches, PREG_OFFSET_CAPTURE)){
        $index = str_replace("item","",$key);
        $b[$index] = $value;
        if($index > $max_index){
            $max_index = $index;
        }
    }
}
$max_value = $b[$max_index];
echo $max_value;