如何在ci中从这个数组中提取数据

时间:2015-02-21 06:19:59

标签: php arrays codeigniter codeigniter-2 associative-array

Array ( 
      [0] => Array (
             [name] => Chennai [id] => 98 ) 
      [1] => Array ( 
             [name] => India [id] => 99 ) 
      [2] => Array ( 
             [name] => South India [id] => 100 )
      [3] => Array ( 
             [name] => North India [id] => 101 )
      [4] => Array ( 
             [name] => Delhi [id] => 102 ) 
)

这是我的数组,我试图提取这样的数据,它显示我错误

foreach ($name as $key => $value) {
    echo $value->name;
}

2 个答案:

答案 0 :(得分:4)

您可以将循环更改为:

     foreach ($name as $key => $value) {
       echo $value['name'];
     }

答案 1 :(得分:1)

实际上你有一个数组,同时将它作为一个对象检索。除了改变之外,你的所有代码都是完美的,

foreach ($name as $key => $value)
{
    echo $value['name'];  //you wrote it $value->name which is wrong
}