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

时间:2019-03-24 23:19:31

标签: php json

如何解决数组问题?

@Input()
$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>";
                        }
                    }
                }
            }
        }
    }

输出给我

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
        )

)
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
        )

)

我认为我在这里遇到了问题,但是尝试得到的结果相同

1 个答案:

答案 0 :(得分:1)

问题不在array_merge中,而在打印部分中。您只需要将打印内容从foreach循环中移出即可。每次迭代结果集时,您都在打印$output数组。

在foreach循环之后执行一次此操作,以获取所需的输出。

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

更新

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