基于键 PHP 的值总和

我们在 PHP 中有类似下面的数组,并且想要在新数组中创建一个新数组,应该有唯一的键,并且应该添加来自同一键的值。


Array

(

    [0] => Array

        (

            [58] => 32.00

        )


)

Array

(

    [0] => Array

        (

            [58] => 34.00

        )


)

Array

(

    [0] => Array

        (

            [57] => 26.00

        )


)

Array

(

    [0] => Array

        (

            [57] => 27.00

        )


)

Array

(

    [0] => Array

        (

            [56] => 16.00

        )


)

Array

(

    [0] => Array

        (

            [56] => 17.00

        )


)

我们想在上述数组的帮助下创建新数组。应添加相同键的值。新数组的输出应如下所示。


Array

(

    [58] => 66

    [57] => 53

    [56] => 33

)

代码如下


minmaxarray 的输出是


Array

(

    [0] => Array

        (

            [min] => 35.00

            [max] => 50.00

            [price] => 26.00

        )


    [1] => Array

        (

            [min] => 50.00

            [max] => 80.00

            [price] => 29.00

        )


    [2] => Array

        (

            [min] => 80.00

            [max] => 100.00

            [price] => 32.00

        )


    [3] => Array

        (

            [min] => 100.00

            [max] => 150.00

            [price] => 34.00

        )


    [4] => Array

        (

            [min] => 150.00

            [max] => 180.99

            [price] => 36.00

        )



$shippingPrice = array();

            foreach ($minmaxarray as $key => $value) {

                if($price >  $value['min'] && $price <  $value['max'])

                {

                    if($value['price'])

                    {


                    $shippingPrice[][$smethodid] = $value['price'];


                    break;

                    }

                }



            }

            echo '<pre>'; print_r ($shippingPrice);

谢谢


holdtom
浏览 80回答 3
3回答

汪汪一只猫

如果您的数组结构始终与您发布的相同,那么此代码将为您工作:$example_array = array(&nbsp; array( '0' => 32.00 ),&nbsp; array( '2' => 32.00 ),&nbsp; array( '2' => 32.00 ));$new_array = array();// loop through every item in nested foreachforeach( $example_array as $value ) {&nbsp; foreach( $value as $key => $number )&nbsp; {&nbsp; &nbsp; // if the array key not exist, calculate with 0, otherwise calculate with the actual value&nbsp; &nbsp; $new_array[$key] = isset($new_array[$key]) ? $new_array[$key] + $number : 0 + $number;&nbsp; }}print_r( $new_array );// prints Array ( [0] => 32 [2] => 64 )您的数组的代码示例:$shippingPrice = array();// loop through every item in nested foreachforeach( $minmaxarray as $value ) {&nbsp; foreach( $value as $key => $number )&nbsp; {&nbsp; &nbsp; // if the array key not exist, calculate with 0, otherwise calculate with the actual value&nbsp; &nbsp; $shippingPrice[$key] = isset($shippingPrice[$key]) ? $shippingPrice[$key] + $number : 0 + $number;&nbsp; }}echo '<pre>';print_r( $shippingPrice );

繁花不似锦

你可以试试这段代码function array_flatten($array) {&nbsp;&nbsp; &nbsp; if (!is_array($array)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return FALSE;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; $result = array();&nbsp;&nbsp; &nbsp; foreach($array as $key=> $value){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($value)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$result = array_merge($result, array_flatten($value));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$key] = $value;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp;return $result;&nbsp;}&nbsp;您通过数组调用此函数,然后函数将返回您预期的数组。array_flatten($array);

ITMISS

您可以使用array_walk_recursive而不是 2foreach循环array_walk_recursive($arr,&nbsp;function($v,$k)&nbsp;use&nbsp;(&$r){ &nbsp;&nbsp;$r[$k]&nbsp;=&nbsp;isset($r[$k])&nbsp;?&nbsp;($r[$k]+$v)&nbsp;:&nbsp;$v; &nbsp;&nbsp;});工作演示:-&nbsp;https://3v4l.org/MMDEv
打开App,查看更多内容
随时随地看视频慕课网APP