Jump to content

modifying elements in an array


petroz

Recommended Posts

Hi Guys,

 

I have an array that contains a few records from mysql.

 

The array looks like so

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => New Product
            [url] => New-Product
            [title] => Title Used On Heading
            [description] => Write a 3 to 5 Paragraph summary of product
            [preview_image] => 0
            [body_image] => 0
            [status] => 1
        )

    [1] => Array
        (
            [id] => 2
            [name] => New Product
            [url] => New-Product
            [title] => Title Used On Heading
            [description] => Write a 3 to 5 Paragraph summary of product
            [preview_image] => 0
            [body_image] => 0
            [status] => 0
        )
)

 

I am trying to modify the preview_image value on each of the arrays in this array like so;

//modify the products array
	foreach($data['raw_products'] as $data['products'])
	{
		//set the preview image row
		$data['products']['preview_image'] = anchor('admin/list_products/#', "View IMG", array("onHover" => "admin.previewIMG('".$data['products']['preview_image']."')", "class" => "noLink"));
		//set the body image row
		$data['products']['body_image'] = anchor('admin/list_products/#', "View IMG", array("onHover" => "admin.bodyIMG('".$data['products']['body_image']."')", "class" => "noLink"));

	}

 

It works fine for one record, but then when I print_r($data['products']) I only see one record. Any help would be greatly appreciated.

Link to comment
Share on other sites

your loop is setting the value of one array index "$data['products']['preview_image']"

if you want to have

 

$data['products']['preview_image'][x] WHERE x is automatically incremented 1,2,3,4,5,6 etc you need to do

//modify the products array
foreach($data['raw_products'] as $data['products'])
{
//set the preview image row
$data['products']['preview_image'][] = anchor('admin/list_products/#', "View IMG", array("onHover" => "admin.previewIMG('".$data['products']['preview_image']."')", "class" => "noLink"));
//set the body image row
$data['products']['body_image'][] = anchor('admin/list_products/#', "View IMG", array("onHover" => "admin.bodyIMG('".$data['products']['body_image']."')", "class" => "noLink"));
}

 

or if you're looing to set the first index from the foreach key,

//modify the products array
foreach($data['raw_products'] as $current)
{
//set the preview image row
$data[$current]['preview_image'] = anchor('admin/list_products/#', "View IMG", array("onHover" => "admin.previewIMG('".$data['products']['preview_image']."')", "class" => "noLink"));
//set the body image row
$data[$current]['body_image'] = anchor('admin/list_products/#', "View IMG", array("onHover" => "admin.bodyIMG('".$data['products']['body_image']."')", "class" => "noLink"));
}

 

and if you want to edit an existing array outside of the foreach's scope, look here and so something like

foreach ($data['raw_products'] as &$current) {
//whatever code her will write to the $data['raw_products'] array
$current = 'whatever';
}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.