-1
我有一個應用程序必須從提供商獲取一些數據。 爲此,我在db中有一些數據,並通過服裝MVC模型中的模型加載它,並嘗試獲取數組中的所有數據(assoc),它的工作原理完全正確。PHP的foreach循環只返回最後一個元素?
最後我想收集所有的列表並將列表從類傳遞給控制器。
我之前爲這個類中的其他參數做過,但現在當我想收集它時,它只返回最後一條記錄!
這裏是代碼:
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;
> }
> }
> }
s不會在這裏'$ temp ['snapshots'] [$ r_image ['distribution']] = $ r_image;'是當前'快照'的某種標識嗎?您正在覆蓋相同的快照。 – Jeff
好主意,讓我檢查一下! – nima
看起來,但我想分類的名稱,它只是成爲一個列表中的索引!這是真的嗎? – nima