0

如果我改变$resultado_frontend使用array_filter它只在函数内部改变它,如果在它是一个空数组$resultado_frontend之后尝试访问。array_filter

$resultado_frontend = [];

   $contas_b2w = array_filter($contas->toArray(), function($conta) use($id_admin, $resultado_frontend, $json_tiao) {

        if($conta['id_mkt'] == 3){

                $sku_existe = Anuncio::where([['id_admin', $id_admin], ['conta_id', $conta['id']]])->where('json_jcontrole->sku',  $json_tiao['sku'])->first();

                if($sku_existe){            

                    $resultado_frontend[] = ["essa conta já possui o sku cadastrado para este marketplace"];
                   dd($resultado_frontend); // here the string is inserted into the array


                }else{

                    return $conta;
                }

        }

    });

   dd($resultado_frontend); //If a remove the "dd" inside the closure and let  code reach this "dd", the array will be empty. How can I get around this?
4

1 回答 1

0

我不确定总体目标到底是什么,所以可能有更好的方法来做到这一点,但是如果你想$resultado_frontend在闭包内部进行修改,你需要use引用它,否则它将按值传递给闭包.

... use($id_admin, &$resultado_frontend, $json_tiao) { ...
于 2019-09-09T18:06:24.447 回答