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?

Petr R.
  • 1,247
  • 2
  • 22
  • 30
Ambulare
  • 897
  • 3
  • 10
  • 32

4 Answers4

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

Jason Young
  • 575
  • 3
  • 16
1
foreach ($MyArray as $Key=>$group) {

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

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

    }

}
Aris
  • 4,643
  • 1
  • 41
  • 38
  • $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 Jul 26 '13 at 14:08
  • Maximus2012 - doesn't defining the variable as &$staff (with the ampersand) make it a reference to the original object, rather than a temp copy? – Ambulare Jul 26 '13 at 14:11
  • 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 ? – Maximus2012 Jul 26 '13 at 14:14
  • I don't like using the ampersand in this case. It's not necessary. – Aris Jul 26 '13 at 14:52
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]);
      } 
    }
}
Manikandan S
  • 902
  • 1
  • 8
  • 18
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");
 }
Orangepill
  • 24,500
  • 3
  • 42
  • 63