如何访问PHP中的所有数组值?

时间:2016-10-24 13:06:54

标签: php

这是我的控制器代码:

$this->load->model('catalog/manufacturer');
$data['categories'] = array();
$results = $this->model_catalog_manufacturer->getManufacturers();
foreach ($results as $result) {
    $data['manufacturer'] = array(     
        'name' => $result['name'],
        'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
    );
}

我打印时

print_r($results);

所有制造商展示。但我打印这个:

print_r($data['manufacturer'] = array( 
'name' => $result['name'], 
'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']) 
));exit; 

它只显示一个制造商。我需要显示所有制造商。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

在foreach中你覆盖变量。你必须添加它不会覆盖,所以:

$data['manufacturers'] = array();
foreach ($results as $result) {
    //$data['manufacturers'][] - it add not overwrite
    $data['manufacturers'][] = array(     
        'name' => $result['name'],
        'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id'])
    );
}
print_r($data['manufacturers']);