简体   繁体   English

清洁阵列而不破坏结构

[英]Cleaning Array without destroying structure

I want to clean my array without destroying the structure. 我想在不破坏结构的情况下清理阵列。 So I want to delete the values of each element. 所以我想删除每个元素的值。 The problem is, that the array has more dimensions. 问题是,数组具有更大的尺寸。

[array]
   [id] = "llll"
   [innerArray]
      [name] = "namenamename"
   [name] = "kdkdfk"
   [arryme]
      [id] = 121212
      [arraytarra] = "dd"

Should be 应该

[array]
   [id] = ""
   [innerArray]
      [name] = ""
   [name] = ""
   [arryme]
      [id] = 0
      [arraytarra] = ""

This is my beginning: 这是我的开始:

private function cleanArray($array) {

    $return = $array;

    foreach($array as $key => $value) {

        if(is_Array($array[$key])) $this->cleanArray($array[$key]);
        else $return[$key] = "";
    }

}

Final version, thanks to bwoebi: 最终版本,感谢bwoebi:

 /****************
 * cleanArray()
 ****************
 *   cleans an array: deletes the values but let the keys
 */
private function cleanArray($array) {

    foreach($array as &$value) {

        if(is_Array($value)) $value = $this->cleanArray($value);
        else if(is_Int($value)) $value = 0;
        else if(is_Bool($value)) $value = false;
        else $value = "";
    }

    return $array;

}
private function cleanArray($array) {

    foreach($array as &$value) {

        if(is_Array($value))
            $value = $this->cleanArray($value);
        else
            $value = "";
    }

    return $array;

}

Your beginning nearly does it (only small modifications were needed), but you don't have to copy the array. 您的开始几乎可以做到这一点(只需要很小的修改),但是您不必复制数组。 As you don't have a reference operator in your parameter list, the array will be passed by value. 由于您的参数列表中没有引用运算符,因此该数组将按值传递。

You also can simply use the array values by reference in your foreach loop. 您也可以在foreach循环中通过引用简单地使用数组值。 And don't forget: save the returned array into a variable. 并且不要忘记:将返回的数组保存到变量中。 And return your final array also. 并返回您的最终数组。 You don't return anything because it's named $return . 您不会返回任何东西,因为它名为$return

I think the answer would be a recursive function the sets each value null or a null string. 我认为答案将是将每个值设置为null或null字符串的递归函数。 Here's an example of a recursive function that does something different, but should be a decent starting place for you. 这是一个递归函数的示例,其功能有所不同,但对您来说应该是一个不错的起点。

http://tecbrat.blogspot.com/2013/03/recursive-function-to-find-substrings.html http://tecbrat.blogspot.com/2013/03/recursive-function-to-find-substrings.html

I think you can loop inside your array and create a new array while iterating, preserving keys and updating values as NULL 我认为您可以在数组内部循环并创建新数组,同时迭代,保留键并将值更新为NULL

$new_array = array();
foreach($array as $key => $data)
{
    $new_array[$key] = NULL;
    if(is_array($data))
    {
        foreach($data as $keys => $val)
        {
            $new_array[$key][$keys] = NULL;
        }
    }
}

var_dump($new_array);

this will output 这将输出

array (size=4)
  'id' => null
  'innerArray' => 
    array (size=1)
      'name' => null
  'name' => null
  'arryme' => 
    array (size=2)
      'id' => null
      'arraytarra' => null

Live Demo 现场演示

Here's my code. 这是我的代码。 Performance wise it's not optimal but looks great as art of code. 在性能方面,它不是最佳选择,但看起来像代码艺术。

function cleanArray($a) 
{
    return is_array($a) ? array_map("cleanArray", $a) : "";
}
$b = array_map("cleanArray", $array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM