简体   繁体   English

PHP数组问题,array_merge() foreach重复问题

[英]PHP array problem, array_merge() foreach repetition problem

How to solve array problem?如何解决数组问题?

$kuponlar = $core->query("SELECT * FROM kupon WHERE kupon_durum = ?", array(0));
if ($kuponlar) {

        foreach($kuponlar as $data) {

            $mac = json_decode($data['kupon_detay']);

            if (isset($mac)) {
                $output = array();
                foreach ($mac as $key) {
                    if (isset($maclar[$key->mac_kod])) {
                        $maclists=$db->prepare("SELECT * FROM maclar WHERE mac_id=?");
                        $maclists->execute(array($key->mac_id));
                        $maclist=$maclists->fetch(PDO::FETCH_ASSOC);

                        $date = strtotime($maclist['mac_zaman']." ".$maclist['mac_saat']);

                        if ($date < time()) {

                            $idsi = $key->mac_uniq;

                            $skor = $maclar[$key->mac_kod];
                            $sonuc = $core->result($donustur[$key->tahmin], $skor,$key->mac_kod);

                            $resultArray =  (array) $mac->$idsi;

                            $new_data = array('durum' => $sonuc);
                            $kupon = array_replace($resultArray, $new_data);
                            $veri = array($maclist['mac_uniq'] => $kupon);
                            $tekrar = array_replace($veri);

                            $output = array_merge($output, $tekrar);

                            echo "<pre>";
                            print_r($output);
                            echo "</pre>";
                        }
                    }
                }
            }
        }
    }

Desired output期望输出

Array
(
    [5c83cb899ec75] => Array
        (
            [mac_id] => 176
            [mac_kod] => 275
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 11:00
            [mac_slug] => tom-tomsk/mordovia/275
            [mac_handikap] => 0
            [ev_logo] => 4246
            [deplasman_logo] => 9229
            [iddaa_id] => 1348012
            [evsahibi] => Tom Tomsk
            [deplasman] => Mordovia
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 11:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa1e175
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb899f7c7] => Array
        (
            [mac_id] => 177
            [mac_kod] => 276
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 13:00
            [mac_slug] => rotor/sibir-n/276
            [mac_handikap] => 0
            [ev_logo] => 4226
            [deplasman_logo] => 4237
            [iddaa_id] => 1348042
            [evsahibi] => Rotor
            [deplasman] => Sibir N.
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 13:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa7c2a6
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb89a0082] => Array
        (
            [mac_id] => 178
            [mac_kod] => 277
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 14:00
            [mac_slug] => fakel/baltika/277
            [mac_handikap] => 0
            [ev_logo] => 4207
            [deplasman_logo] => 4200
            [iddaa_id] => 1348007
            [evsahibi] => Fakel
            [deplasman] => Baltika
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 14:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603ab01e65
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.18
        )

    [5c83cb89b3343] => Array
        (
            [mac_id] => 210
            [mac_kod] => 278
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 14:00
            [mac_slug] => chertanovo-/tambov/278
            [mac_handikap] => 0
            [ev_logo] => 14278
            [deplasman_logo] => 9159
            [iddaa_id] => 1348025
            [evsahibi] => Chertanovo  
            [deplasman] => Tambov
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 14:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603afe43a2
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 2
            [total] => 2.73
        )

)

Output To Me输出给我

Array
(
    [5c83cb899ec75] => Array
        (
            [mac_id] => 176
            [mac_kod] => 275
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 11:00
            [mac_slug] => tom-tomsk/mordovia/275
            [mac_handikap] => 0
            [ev_logo] => 4246
            [deplasman_logo] => 9229
            [iddaa_id] => 1348012
            [evsahibi] => Tom Tomsk
            [deplasman] => Mordovia
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 11:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa1e175
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

)
Array
(
    [5c83cb899ec75] => Array
        (
            [mac_id] => 176
            [mac_kod] => 275
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 11:00
            [mac_slug] => tom-tomsk/mordovia/275
            [mac_handikap] => 0
            [ev_logo] => 4246
            [deplasman_logo] => 9229
            [iddaa_id] => 1348012
            [evsahibi] => Tom Tomsk
            [deplasman] => Mordovia
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 11:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa1e175
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb899f7c7] => Array
        (
            [mac_id] => 177
            [mac_kod] => 276
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 13:00
            [mac_slug] => rotor/sibir-n/276
            [mac_handikap] => 0
            [ev_logo] => 4226
            [deplasman_logo] => 4237
            [iddaa_id] => 1348042
            [evsahibi] => Rotor
            [deplasman] => Sibir N.
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 13:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa7c2a6
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

)
Array
(
    [5c83cb899ec75] => Array
        (
            [mac_id] => 176
            [mac_kod] => 275
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 11:00
            [mac_slug] => tom-tomsk/mordovia/275
            [mac_handikap] => 0
            [ev_logo] => 4246
            [deplasman_logo] => 9229
            [iddaa_id] => 1348012
            [evsahibi] => Tom Tomsk
            [deplasman] => Mordovia
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 11:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa1e175
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb899f7c7] => Array
        (
            [mac_id] => 177
            [mac_kod] => 276
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 13:00
            [mac_slug] => rotor/sibir-n/276
            [mac_handikap] => 0
            [ev_logo] => 4226
            [deplasman_logo] => 4237
            [iddaa_id] => 1348042
            [evsahibi] => Rotor
            [deplasman] => Sibir N.
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 13:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa7c2a6
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb89a0082] => Array
        (
            [mac_id] => 178
            [mac_kod] => 277
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 14:00
            [mac_slug] => fakel/baltika/277
            [mac_handikap] => 0
            [ev_logo] => 4207
            [deplasman_logo] => 4200
            [iddaa_id] => 1348007
            [evsahibi] => Fakel
            [deplasman] => Baltika
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 14:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603ab01e65
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.18
        )

)
Array
(
    [5c83cb899ec75] => Array
        (
            [mac_id] => 176
            [mac_kod] => 275
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 11:00
            [mac_slug] => tom-tomsk/mordovia/275
            [mac_handikap] => 0
            [ev_logo] => 4246
            [deplasman_logo] => 9229
            [iddaa_id] => 1348012
            [evsahibi] => Tom Tomsk
            [deplasman] => Mordovia
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 11:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa1e175
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb899f7c7] => Array
        (
            [mac_id] => 177
            [mac_kod] => 276
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 13:00
            [mac_slug] => rotor/sibir-n/276
            [mac_handikap] => 0
            [ev_logo] => 4226
            [deplasman_logo] => 4237
            [iddaa_id] => 1348042
            [evsahibi] => Rotor
            [deplasman] => Sibir N.
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 13:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603aa7c2a6
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.1
        )

    [5c83cb89a0082] => Array
        (
            [mac_id] => 178
            [mac_kod] => 277
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 14:00
            [mac_slug] => fakel/baltika/277
            [mac_handikap] => 0
            [ev_logo] => 4207
            [deplasman_logo] => 4200
            [iddaa_id] => 1348007
            [evsahibi] => Fakel
            [deplasman] => Baltika
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 14:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603ab01e65
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 1
            [total] => 1.18
        )

    [5c83cb89b3343] => Array
        (
            [mac_id] => 210
            [mac_kod] => 278
            [mac_lig] => RU2
            [mac_zaman] => 24.03.2019
            [mac_saat] => 14:00
            [mac_slug] => chertanovo-/tambov/278
            [mac_handikap] => 0
            [ev_logo] => 14278
            [deplasman_logo] => 9159
            [iddaa_id] => 1348025
            [evsahibi] => Chertanovo  
            [deplasman] => Tambov
            [ulke] => Rusya 1. Ligi
            [mac_tarihi] => 24.03.2019 14:00:00
            [sonuc] => 
            [live] => 0
            [mac_uniq] => 5c9603afe43a2
            [count] => 1
            [tahmin] => Ev/Beraber
            [durum] => 2
            [total] => 2.73
        )

)

$output = array_merge($output, $tekrar);

I think I've made a problem here but I get the same result in which way I tried我想我在这里遇到了问题,但我尝试的结果相同

The problem is not in array_merge but in the printing part.问题不在于array_merge而在于打印部分。 You simply need to move the printing out from the foreach loop.您只需要将打印从 foreach 循环中移出即可。 You are printing the $output array every time you iterate over your result set.每次迭代结果集时,您都在打印$output数组。

Do this once after the foreach loop to get the desired output.在 foreach 循环之后执行此操作以获得所需的输出。

foreach ($mac as $key) {
    //..
}
echo "<pre>";
print_r($output);
echo "</pre>";

Update :更新

foreach ($mac as $key) {
    //..
}
// if you want to print only non-empty results
if (!empty($output)) {
    echo "<pre>";
    print_r($output);
    echo "</pre>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM