petroz Posted January 27, 2011 Share Posted January 27, 2011 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. Quote Link to comment Share on other sites More sharing options...
joel24 Posted January 27, 2011 Share Posted January 27, 2011 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'; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.