数组中的数据相乘

时间:2019-04-04 13:14:45

标签: php arrays

我将数据保留在Array中,我想将这些数据彼此相乘,但未成功。我找到了Array_product函数,但无法成功运行。您认为问题是什么?

$kuponlar = $_SESSION['kuponlar'];

$maclar = $kuponlar['maclar'];

我要相乘的数据:

echo array_product($maclar['oran']));

array_product在我想做时不起作用。是否有找不到该问题的建议?

Array
(
    [maclar] => Array
        (
            [5ca4fb869b043] => stdClass Object
                (
                    [mac_id] => 881
                    [mac_kod] => 657
                    [mac_lig] => HIR
                    [mac_zaman] => 04.04.2019
                    [mac_saat] => 19:00
                    [mac_slug] => rudes/istra/657
                    [mac_handikap] => 1
                    [ev_logo] => 12644
                    [deplasman_logo] => 6776
                    [iddaa_id] => 1308487
                    [evsahibi] => Rudes
                    [deplasman] => Istra
                    [ulke] => Hırvatistan 1. Ligi
                    [mac_tarihi] => 04.04.2019 19:00:00
                    [sonuc] => 
                    [live] => 0
                    [mac_uniq] => 5ca4fb869b043
                    [count] => 1
                    [tahmin] => Ev Kazanır
                    [oran] => 2.85
                    [durum] => 0
                )

            [5ca4fb869bf78] => stdClass Object
                (
                    [mac_id] => 882
                    [mac_kod] => 658
                    [mac_lig] => HOL
                    [mac_zaman] => 04.04.2019
                    [mac_saat] => 19:30
                    [mac_slug] => psv/zwolle/658
                    [mac_handikap] => -1
                    [ev_logo] => 2836
                    [deplasman_logo] => 2869
                    [iddaa_id] => 1306687
                    [evsahibi] => PSV
                    [deplasman] => Zwolle
                    [ulke] => Hollanda Eredivisie Ligi
                    [mac_tarihi] => 04.04.2019 19:30:00
                    [sonuc] => 
                    [live] => 0
                    [mac_uniq] => 5ca4fb869bf78
                    [count] => 1
                    [tahmin] => Ev Kazanır
                    [oran] => 1.10
                    [durum] => 0
                )

            [5ca4fb869cde3] => stdClass Object
                (
                    [mac_id] => 883
                    [mac_kod] => 660
                    [mac_lig] => İTA
                    [mac_zaman] => 04.04.2019
                    [mac_saat] => 20:00
                    [mac_slug] => sassuolo/chievo/660
                    [mac_handikap] => 
                    [ev_logo] => 665
                    [deplasman_logo] => 578
                    [iddaa_id] => 1333704
                    [evsahibi] => Sassuolo
                    [deplasman] => Chievo
                    [ulke] => İtalya Serie A Ligi
                    [mac_tarihi] => 04.04.2019 20:00:00
                    [sonuc] => 
                    [live] => 0
                    [mac_uniq] => 5ca4fb869cde3
                    [count] => 1
                    [tahmin] => Beraberlik
                    [oran] => 3.20
                    [durum] => 0
                )

        )

    [summary] => Array
        (
            [total] => 1
            [count] => 0
        )

)

2 个答案:

答案 0 :(得分:2)

您需要乘以数组中每个对象的属性oran

$maclar = $kuponlar['maclar'];
$mul = 1;
foreach($maclar as $key => $value){
     $mul *=  $value->oran;    
}

答案 1 :(得分:1)

在$ maclar数组中没有'oran'索引,首先应该创建一个'oran'数组,然后在该数组上使用array_product。 例如:

foreach($maclar as $key=>$object){
    $oran[] = $object->oran;
}

echo array_product($oran);