从$ data array codeigniter

时间:2015-07-08 04:57:49

标签: php mysql arrays codeigniter

我有一个关联数组$ data ['arr1'],它由查询填充。我需要迭代数组,检查每个记录的特定值,如果记录存在,从数组中删除该记录。这是我的代码:

    foreach($data['arr1'] as $key => $row) {

        // code for checking the value in a record. Cannot access the element here. I have 
           tried to print with these statements
        echo $data['arr1'] -> Firstname;
        echo $data['arr1'] [0];
        echo $data['arr1'][$key];
        echo [$key]; // gives me the values of index 0,1,2,3

        if(//check is true)
        {
            unset($data['arr1'][$key]);  //works fine, removes elements as needed
        }

    }

如何访问元素以进行检查?我认为它是data['arr1']$key的组合,但我找不到正确的组合。此外,当我执行print_r($data['arr1'])时,它会正确打印整个数组。为:

Array ( [0] => stdClass Object ( [Id] => 107 [assessmentTime] => 0000-00-00 00:00:00  [Gender] => 1 [DOB] => 2009-05-06 12:00:00 [Village] => Bhains [Tehsil] => 1 [UnionCouncil] => 2 [Snap] => 582864.jpg  [WhyWeDisable] => [HasDeleted] => 0 [CallerID] => 0 [vTime] => 0000-00-00 00:00:00 [CaseId] => 98 ) [1] => stdClass Object ( [Id] => 57 [FirstName] => ..............

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试

foreach( $data as $obj ){
  echo $obj->id;
  ..etc.
}

您将一组具有多维数组的对象混淆。

或者您可以手动输出

echo $data[0]->id;

如果你只需要看第一个说话。

无论如何,当你打印整个阵列时,这里的赠品就是这一点。

 Array ( [0] => stdClass Object (

查看它所说的对象。我们使用[ key ]访问的$obj->property个对象访问的数组。