繁体   English   中英

PHP foreach覆盖结果Joomla 2.5

[英]PHP foreach overwriting results Joomla 2.5

我每个人目前正在为使用jvectormap的Joomla 2.5创建一个组件,由于某种原因,我将所有大洲和国家/地区都放在json中,但仅添加了最新添加的公司,而不是较旧的公司...我认为可能是与我的foreach语句有关...但实际上并不能完全确定:

我的模型文件:

public function getLand()
{
$land_id = JRequest::getInt('id');

$db = $this->getDbo();
$query = $db->getQuery(true);

$query->select('*, a.continent_name,continent_facts');
$query->from('#__vectormap_countries AS t')
        ->join('LEFT', '#__vectormap_continents AS a USING(continent_id)')
        ->where('t.published = 1');

$db->setQuery($query);

$rows = $db->loadObjectList();

return $rows;
}

public function getFirm(){
    $firm_id = JRequest::getInt('id');

    $db = $this->getDbo();
    $query = $db->getQuery(true);

    $query->select('*');
    $query->from('#__vectormap_firms AS t')
            ->where('t.published = 1');

    $db->setQuery($query);

    $firms = $db->loadObjectList();

    return $firms;
}

然后回显数组的部分是我的view.json.php:

public function display($tpl = null)
{
$land = $this->get('Land');
$firm = $this->get('Firm');
$response = array();

foreach ($land as $row) {
        $response[$row->country_code] = array(
                    'path' => $row->country_svgpath,
                    'name' => $row->country_name,
                    'continent' => $row->continent_name,
                    'fact' => $row->continent_facts
        );
};

foreach ($firm as $firms) {
        $response['firms'] = array (
            'firm_name' => $firms->firm_companyname,
            'firm_latitude' => $firms->firm_latitude,
            'firm_longitude' => $firms->firm_longitude
            );
        };
echo json_encode($response);
}

并且在第一个foreach中返回所有内容,但是在第二个foreach中,它仅返回最新的公司,而不是全部。

请毫不犹豫地询问是否需要更多详细信息。.非常感谢。 谢谢 :)

将第二个循环更改为:

$response['firms'] = array();
foreach ($firm as $firms) {
        $response['firms'][] = array (
            'firm_name' => $firms->firm_companyname,
            'firm_latitude' => $firms->firm_latitude,
            'firm_longitude' => $firms->firm_longitude
            );
        };

这样, $response['firms']将成为列出所有公司的索引数组。

第二个foreach中的键是固定的,如$response['firms'] ,因此以后的运行将覆盖以前的运行。 您可以尝试如下操作:

foreach ($firm as $firms) {
    $response[$firms->firm_companyname] = array (
        'firm_latitude' => $firms->firm_latitude,
        'firm_longitude' => $firms->firm_longitude
        );
    };

这将为您提供公司名称作为关键字-您需要在每次循环运行中进行更改。 您还可以有一个递增的整数:

$firmcount = 0;
foreach ($firm as $firms) {
    $response['firms' . $firmcount++] = array (
        'firm_name' => $firms->firm_companyname,
        'firm_latitude' => $firms->firm_latitude,
        'firm_longitude' => $firms->firm_longitude
        );
    };

暂无
暂无

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

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