1

I have a three dimensional array that looks like this

Array(
[Group 1] => Array
    (
        [0] => Array
            (
                [category] => Group1
                [firstname] => John
                [lastname] => Johns
                [image] => /mysite.etc/jj.jpg
            )
        [1] => Array
            (
                [category] => Group1
                [firstname] => John
                [lastname] => James
                [image] => /mysite.etc/jj2.jpg
            )
    )
[Group 2] => Array
    (
        [0] => Array
            (
                [category] => Group2
                [firstname] => John
                [lastname] => Jackson
                [image] => NULL
            )
        [1] => Array
            (
                [category] => Group2
                [firstname] => John
                [lastname] => Jimson
                [image] => /mysite.etc/jj4.jpg   
            )
    )...etc)

I'm trying to loop through the array and remove any people (i.e. the second level of the array) who do not have a value in the [image] cell.

I've tried

foreach($MyArray as $Key=>&$group){ 
    foreach($group as &$staff){ 
        if(!file_exists($staff['image'])){
            unset($staff);
        }
    } 
}

but this does not remove the array items with no image. The loop is correctly identifying the staff with no image as if I include a bit of code to echo them onto the page, this works. It's just not unsetting them from the $MyArray array.

Can anyone help me achieve this?

4 Answers 4

5
foreach($MyArray as $Key=>$group){
     foreach($group as $k=>$staff){ 
         if( !file_exists($staff['image'])) {
             unset($MyArray[$Key][$k]);
          }
     }
}

//you should know the the $group and $staff is temp variables

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

Comments

1
foreach ($MyArray as $Key=>$group) {

    foreach ($group as $k=>$staff) {

         if( empty($staff['image']) || !file_exists($staff['image'])) {
              unset($MyArray[$key][$k]);
         }

    }

}

4 Comments

$staff is a temp variable. This code will not affect the original array which is what the OP is trying to do. Look at Jason Yang's answer below.
Maximus2012 - doesn't defining the variable as &$staff (with the ampersand) make it a reference to the original object, rather than a temp copy?
I think it does but I am not entirely sure about that. You actually don't need to do it that way though as indicated by answers from Jason and Aris. However, are you getting any error message when you use &$staff ?
I don't like using the ampersand in this case. It's not necessary.
0

The condition should be following like this.

foreach($MyArray as $Key=>&$group){            
    foreach($group as $staffkey=>$staff){
      if( $staff['image'] == null)) 
      {
        unset($MyArray[$key][$staffkey]);
      } 
    }
}

Comments

0

You can use array_filter for this:

With a closure: available in php 5.3

 foreach($groups as &$users){
     $users = array_filter($users, function ($a) { return isset($a["image"]) && file_exists($a["image"]); });
 }

Without closures

 function hasImage($a){ return isset($a["image"]) && file_exists($a["image"]); }
 foreach($groups as &$users){
     $users = array_filter($users, "hasImage");
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.