php foreach循环只返回最后一个元素?

时间:2016-08-27 12:49:48

标签: php loops foreach

我有一个必须从提供商处获取一些数据的应用。 为此,我在db中有一些数据并通过服装MVC模型中的模型加载它并尝试获取数组(assoc)中的所有数据,它完全正确。

最后我想收集所有列表并将列表从类传递给控制器​​。

我之前在这个类中已经完成了其他参数,但现在当我想收集它时它只返回最后一条记录!

这是代码:

    public function getDetails()
{

    $provider = [];

    $provider['local']['images'] = $this->loadModel('images')->getProviderImages($this->provider_id);
    $provider['local']['applications'] = $this->loadModel('images')->getProviderApplications($this->provider_id);
    $provider['local']['plans'] = $this->loadModel('plans')->getProviderPlans($this->provider_id);
    $provider['local']['dataCenters'] = $this->loadModel('regions')->getProviderRegions($this->provider_id);
    $provider['local']['snapshots'] = $this->loadModel('snapshots')->getProviderSnapshots($this->provider_id);

    try {
        $provider['remote']['images'] = $this->getImages()->images;
        $provider['remote']['plans'] = $this->getPlans()->plans;
        $provider['remote']['dataCenters'] = $this->getDataCenters()->data_centers;

        if (!$provider['remote']['images'] || !$provider['remote']['plans'] || !$provider['remote']['dataCenters']) {
            throw new Exception ("Can't connect to provider. creating provider details array failed!");
        }
    } catch (Exception $e) {
        log_error($e);

        return false;
    }
    /***********/


    $temp = [];
    foreach ($provider['local']['images'] as $key => $image) {
        foreach ($provider['remote']['images'] as $r_key => $r_image) {
            if ($image['is_active'] == true) {
//                                r($r_image);

if ($image['slug'] == $r_image['slug']) {
                    $temp['images'][$image['os_name']]['name'] = $image['name'];
                    $version = $image['os_version'];
                    $version .= (!empty($image['os_architecture'])) ? '-' . $image['os_architecture'] : '';
                    $temp['images'][$image['os_name']]['versions'][$version] = $image;
                    $temp['images'][$image['os_name']]['versions'][$version]['dataCenters'] = $r_image['regions'];

                    if ($image['image_id'] != $r_image['id']) {
                        $this->loadModel('images')->updateImageID($this->provider_id, $image['slug'], $r_image['id']);
                    }
                    break;
                }
            }
        }
    }//die(r($temp));
    foreach ($provider['local']['snapshots'] as $key => $snapshot) {
        foreach ($provider['remote']['images'] as $r_key => $r_image) {
            if ($snapshot['snapshot_id'] == $r_image['id']) {

                $temp['snapshots'][$r_image['distribution']] = $r_image;
                $temp['snapshots'][$r_image['distribution']]['service_provider'] = $snapshot['service_provider_id'];
                $temp['snapshots'][$r_image['distribution']]['os_name'] = strtolower($r_image['distribution']);

                break;
            }
        }
    }//die(r($temp));





       foreach ($provider['local']['applications'] as $key => $app) {
            foreach ($provider['remote']['images'] as $r_key => $r_image) {
                if ($app['is_active'] == true) {

//                r($r_image);
                    //echo "{$image['image_id']} == {$r_image->id} | ";
                    if (empty($r_image['slug'])) {
                        if ($app['image_id'] == $r_image['id']) {
                            $temp['applications'][$app['app_name']]['name'] = $app['name'];
                            $temp['applications'][$app['app_name']]['os'][$app['os_name']][$app['os_version'] . '-' . $app['os_architecture']] = $app;
                            $temp['applications'][$app['app_name']]['os'][$app['os_name']][$app['os_version'] . '-' . $app['os_architecture']]['dataCenters'] = $r_image['regions'];
                            break;
                        }
                    } elseif ($app['slug'] == $r_image['slug']) {
                        $temp['applications'][$app['app_name']]['name'] = $app['name'];
                    $temp['applications'][$app['app_name']]['os'][$app['os_name']][$app['os_version'] . '-' . $app['os_architecture']] = $app;
                    $temp['applications'][$app['app_name']]['os'][$app['os_name']][$app['os_version'] . '-' . $app['os_architecture']]['dataCenters'] = $r_image['regions'];

                    if ($app['image_id'] != $r_image['id']) {
                        $this->loadModel('images')->updateImageID($this->provider_id, $app['slug'], $r_image['id']);
                    }
                    break;
                }
            }
        }
    }//die();


    foreach ($provider['local']['plans'] as $key => $plan) {
        foreach ($provider['remote']['plans'] as $r_key => $r_plan) {
            //echo "{$plan['slug']} == {$r_plan->slug} | ";
            if ($plan['slug'] == $r_plan['slug'] && $r_plan['available']) {
                $temp['plans'][$plan['slug']] = $plan;
                $temp['plans'][$plan['slug']]['dataCenters'] = $r_plan['regions'];

                break;
            }
        }
    }

    // sort by plan's price
    uasort($temp['plans'], function ($a, $b) {
        return $a['hourly_price'] > $b['hourly_price'];
    });

    foreach ($provider['local']['dataCenters'] as $key => $dc) {
        foreach ($provider['remote']['dataCenters'] as $r_key => $r_dc) {
            //echo "{$dc['slug']} == {$r_dc->slug} | ";
            if ($dc['slug'] == $r_dc['slug'] && $r_dc['available']) {
                $temp['dataCenters'][$dc['country']][$dc['city']][$dc['slug']] = $dc;
                $temp['dataCenters'][$dc['country']][$dc['city']][$dc['slug']]['plans'] = $r_dc['sizes'];
                $temp['features'][$dc['slug']] = $r_dc['features'];
                break;
            }
        }
    }

    return $temp;
}

我的问题在于以下几点:

>         foreach ($provider['local']['snapshots'] as $key => $snapshot) {
>            foreach ($provider['remote']['images'] as $r_key => $r_image) {
>                if ($snapshot['snapshot_id'] == $r_image['id']) {
>
>                   $temp['snapshots'][$r_image['distribution']] = $r_image;
>                    $temp['snapshots'][$r_image['distribution']]['service_provider'] = $snapshot['service_provider_id'];
>                    $temp['snapshots'][$r_image['distribution']]['os_name'] = strtolower($r_image['distribution']);
>
>                    break;
>                }
>            }
>        }

1 个答案:

答案 0 :(得分:0)

最后我发现了awnser,我应该使用一些变量作为索引,如下所示:

$temp['snapshots'][$snapshot['id']] = $r_image;

它有效,