简体   繁体   English

减少无法正常工作我需要添加例外/排除

[英]Reduce not work correctly i need add exeption / exlusion

i have an array like that in $_POST var;我在 $_POST var 中有一个这样的数组; but need know that in some cases the array is a hugge multilevel from json:但需要知道,在某些情况下,数组是来自 json 的多级数组:

array (
  'idprocess' => 'f-gen-dato1',
  'idform' => 'f-gen-dato2',
)

OR:或者:

array (
  array (
    'idprocess1' => 'f-gen-dato1',
    'idform1' => 'f-gen-dato2',
  ),
  array (
    'idprocess2' => 'f-gen-dato1',
    'idform2' => 'f-gen-dato2',
  )
)

i try to reduce;我尽量减少; any arrays with this:任何具有此功能的数组:

public function ReduARR($Var) {
        $result = $Var;
        if (is_array($Var)) {
            $result = array_reduce($Var, 'array_merge', array());
        }
        return $result;
    }

but i need avoid the array i show you... frist or single level.但我需要避免我向你展示的数组......第一级或单级。 and work only in the second or multilevel.并且只在第二级或多级工作。

i get this error with one lvl:我收到一个 lvl 的错误:

array_merge(): Argument #2 is not an array

My guess is that you wish to merge or reduce some arrays, and you might be trying to write some functions similar to:我的猜测是您希望合并或减少一些数组,并且您可能正在尝试编写一些类似于以下内容的函数:

$arr1 = array(
    'idprocess1' => 'f-gen-dato1',
    'idform1' => 'f-gen-dato2',
);

$arr2 = array(
    'idprocess2' => 'f-gen-dato1',
    'idform2' => 'f-gen-dato2',
);

function finalArray($arr1, $arr2)
{
    if (is_array($arr1) && is_array($arr2)) {
        return mergeTwoArrays($arr1, $arr2);
    }
}

function mergeTwoArrays($arr1, $arr2)
{
    return array_merge($arr1, $arr2);
}

var_dump(finalArray($arr1, $arr2));

for instance.例如。


$arr = array(
    array(
        'idprocess1' => 'f-gen-dato1',
        'idform1' => 'f-gen-dato2',
    ),
    array(
        'idprocess2' => 'f-gen-dato1',
        'idform2' => 'f-gen-dato2',
    ),
);

if (is_array($arr[0]) && is_array($arr[1])) {
    var_dump(array_merge($arr[0], $arr[1]));
}

Output输出

array(4) {
  ["idprocess1"]=>
  string(11) "f-gen-dato1"
  ["idform1"]=>
  string(11) "f-gen-dato2"
  ["idprocess2"]=>
  string(11) "f-gen-dato1"
  ["idform2"]=>
  string(11) "f-gen-dato2"
}

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

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