user7007720 user7007720 - 1 month ago 6
PHP Question

how to access all array values in php?

This my controller code:

$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'])
);
}


When I print this

print_r($results);


all manufactures displays. but i print this :

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


it displays only one manufacturer. I need to display all manufacturers. How can I do this?

Answer

NOw in foreach you overwrite you variable. You must add to it not overwrite, so:

$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']);