2

I have a associative array $data['arr1'] which is populated by a query. I need to iterate through the array , check each record for a specific value, and if the record is present , remove that record from the array. Here is my code:

    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
        }

    }

How can I access the element for checks? I think it is a combination of data['arr1'] and $key but i cannot find a correct combination. Also when I do print_r($data['arr1']) it prints the whole array correctly. as:

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] => ..............

Any help would be much appreciated.

8
  • you need to remove one element from the array?? Commented Jul 8, 2015 at 4:59
  • yes, and the element is removed fine. But i need to access that element before removing it, And I cannot do it Commented Jul 8, 2015 at 5:01
  • first of all dont use echo on arrays, use print_r($array), var_export($array), or var_dump( $array ); but before these do echo '<pre>'; for formatting, so echo '<pre>'; var_export($data); Commented Jul 8, 2015 at 5:01
  • prin_r($array) prints the whole array in one go. I need to print each record separately. How I do that? Commented Jul 8, 2015 at 5:03
  • print_r($array[$key]); unless it say stdclass, then it is an object and you use -> So you do print_r($data[0]->id); to get the id, basically print_r($data[0]); or print_r($data[1]); gets the object, then you access its property as standard with the arrow -> Commented Jul 8, 2015 at 5:04

1 Answer 1

1

Try

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

You are confusing an array of objects with a multi-dimensional array.

or you can output them manually as

echo $data[0]->id;

If you only need to look at say the first one.

Anyway the give away here is this bit, when you print the whole array out.

 Array ( [0] => stdClass Object (

See where it says object. Arrays we access with [ key ] objects we access with $obj->property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.