函数返回传递给它的数组的1/2

时间:2014-11-28 16:10:48

标签: php multidimensional-array

我的函数返回传递给它的数组的1/2。

当我处理这个数组时:

$output2 = 
Array
(
[sel_id] => Array
    (
        [0] => 1
        [1] => 2
    )

[repair_id] => Array
    (
        [0] => 20
        [1] => 1
    )

)

使用

foreach($output2 as $key => $value){
    if(is_array($value)){
        foreach($value as $key2 => $value2){
            echo "$key - $value2<br />";
            $new[$key][] = $value2;
        }
    }

}

以原始(所需)格式返回数组:

Array
(
[sel_id] => Array
    (
        [0] => 1
        [1] => 2
    )

[repair_id] => Array
    (
        [0] => 20
        [1] => 1
    )

) 

但是当我通过我的函数传递它来清理值时:

function cleanValues2($array){
    foreach($array as $key1 => $value1){
        if(is_array($value1)){
            foreach($value1 as $key2 => $value2){
                $trm = trim($value2);
                $strp = strip_tags($trm);
                $fltr1 = filter_var($strp,FILTER_SANITIZE_STRING);
                $fltr2 = filter_var($fltr1,FILTER_SANITIZE_SPECIAL_CHARS);
                $fltr3 = htmlspecialchars($fltr2);
                $strtoup = strtoupper($fltr3);
                $new_array[$key1][] = $strtoup;
            }
            return $new_array;
        }
    }
}

它返回数组的一半:

Array
(
[sel_id] => Array
    (
        [0] => 1
        [1] => 2
    )

) 

我错过了什么?

2 个答案:

答案 0 :(得分:1)

你已经在forloop中添加了return语句,因为只有第一个数组值被迭代。

在循环之后放置此返回。

答案 1 :(得分:0)

将返回移到外部foreach循环之外。

function cleanValues2($array){
        foreach($array as $key1 => $value1){
            if(is_array($value1)){
                foreach($value1 as $key2 => $value2){
                    $trm = trim($value2);
                    $strp = strip_tags($trm);
                    $fltr1 = filter_var($strp,FILTER_SANITIZE_STRING);
                    $fltr2 = filter_var($fltr1,FILTER_SANITIZE_SPECIAL_CHARS);
                    $fltr3 = htmlspecialchars($fltr2);
                    $strtoup = strtoupper($fltr3);
                    $new_array[$key1][] = $strtoup;
                }

            }
        }
        return $new_array;
    }