views:

533

answers:

6

I have the following array that I need to recursively loop through and remove any child arrays that have the key 'fields'. I have tried array filter but I am having trouble getting any of it to work.

$myarray = array(
    'Item' => array(
        'fields' => array('id', 'name'),
        'Part' => array(
            'fields' => array('part_number', 'part_name')
        )
    ),
    'Owner' => array(
        'fields' => array('id', 'name', 'active'),
        'Company' => array(
            'fields' => array('id', 'name',),
            'Locations' => array(
                'fields' => array('id', 'name', 'address', 'zip'),
                'State' => array(
                    'fields' => array('id', 'name')
                )
            )
        )
    )    
);

This is how I need it the result to look like:

$myarray = array(
    'Item' => array(
        'Part' => array(
        )
    ),
    'Owner' => array(
        'Company' => array(
            'Locations' => array(
                'State' => array(
                )
            )
        )
    )    
);
A: 

Give this function a shot. It will remove the keys with 'fields' and leave the rest of the array.

function unsetFields($myarray) {
    if (isset($myarray['fields']))
        unset($myarray['fields']);
    foreach ($myarray as $key => $value)
        $myarray[$key] = unsetFields($value);
    return $myarray;
}
Travis
+2  A: 

If you want to operate recursively, you need to pass the array as a reference, otherwise you do a lot of unnecessarily copying:

function recursive_unset(&$array, $unwanted_key) {
    unset($array[$unwanted_key])
    foreach ($array as &$value) {
        if (is_array($value)) {
            recursive_unset($value, $unwanted_key);
        }
    }
}
soulmerge
A: 

My suggestion:

function removeKey(&$array, $key)
{
    if (is_array($array))
    {
        if (isset($array[$key]))
        {
            unset($array[$key]);
        }
        if (count($array) > 0)
        {
            foreach ($array as $k => $arr)
            {
                removeKey($array[$k], $key);
            }
        }
    }
}

removeKey($myarray, 'Part');
inakiabt
A: 

Recursively walk the array (by reference) and unset the relevant keys.

clear_fields($myarray);
print_r($myarray);

function clear_fields(&$parent) {
  unset($parent['fields']);
  foreach ($parent as $k => &$v) {
    if (is_array($v)) {
      clear_fields($v);
    }
  }
}
cletus
A: 
function sanitize($arr) {
    if (is_array($arr)) {
     $out = array();
     foreach ($arr as $key => $val) {
      if ($key != 'fields') {
       $out[$key] = sanitize($val);
      }
     }
    } else {
     return $arr;
    }
    return $out;
}

$myarray = sanitize($myarray);

Result:

array (
  'Item' => 
  array (
    'Part' => 
    array (
    ),
  ),
  'Owner' => 
  array (
    'Company' => 
    array (
      'Locations' => 
      array (
        'State' => 
        array (
        ),
      ),
    ),
  ),
)
artlung
A: 

you want array_walk

function remove_key(&$a) {
   if(is_array($a)) {
      unset($a['fields']);
      array_walk($a, __FUNCTION__);
   }
}
remove_key($myarray);
stereofrog