Okay, so I am trying to use a restful api. And when I put my results into a foreach loop, I get that the variable in that foreach is undefined. What do I need to do instead?
My results are coded as $modules
and when I var_dump
this, I receive the following array:
string(42) "array ( 1 => 'Emails', 2 => 'Pages', )"
Now, When I try to place this in my foreach loop like so:
<?php foreach($modules as $module => $name): ?>
<?php echo $module; ?>
<?php endforeach; ?>
I get this error:
Invalid argument supplied for foreach()
Btw, I am using codeigniter and the $modules
are passed from a controller to a view.
$data['modules'] = codeToGetArray
update
The code that is supposed to be making the array is as follows:
function list_get()
{
$this->load->model('api/api_m', 'api_m');
$modules = $this->api_m->list_modules();
$mods = array();
foreach($modules as $module)
{
$mods[$module->id] = $module->name;
}
$modules = $mods;
if($modules)
{
$this->response($modules, 200);
}
else
{
$this->response(array('error' => 'Couldn\'t fin any modules!'), 404);
}
}