#Check if number exists in array

42 messages · Page 1 of 1 (latest)

clever current
#

Hi,

I'm working on a query where I have a 'cpha' number and an 'response' array. I need that for each cpha number, the array with the corresponding answer is returned. The problem is that in this array the data is as text. How do I return the correct data? Is there any function in Laravel for this? I tried using 'exists' but it didn't work.

An example of what is returned to me

#
1 => 
  array (
    'cpha' => '7050704970111921',
    'data_nomeacao' => '14/11/2017',
    'vara' => 
    array (
      'codVara' => 5402,
      'descricaoVara' => '1ª VARA CÍVEL',
    ),
    'motivo' => 
    array (
      0 => '7050704970111921: n.º NIT incorreto.',
      1 => '1850184090291981: n.º NIT incorreto.',
      2 => '4290428090122122: n.º NIT incorreto.',
      3 => '06210030621090382084: n.º NIT incorreto.',
      4 => '3140313590291841: n.º NIT incorreto.',
      5 => '4990498090151701: n.º NIT incorreto.',
      6 => '4990498090151561: n.º NIT incorreto.',
    ),
  ),```  In this case I want you to return the reason with the cpha number 7050704970111921. In this case motivo [0]
mellow citrus
#

Are you talking about making a query against the database looking for this? If so this is less a laravel question, and more an SQL question. (Assuming SQL database). If you're talking about iterating the array and finding a string inside another string, you could always use https://laravel.com/docs/9.x/helpers#method-str-contains and iterate the array.

clever current
#

It would be the second option. This example is my return, but in the 'motivo' result I only need what appears the number identical to the cpha of the variable

mellow citrus
#

Could there be multiple results?

clever current
#

Do not. In the case of this example it would only be the 'motivo' at position 0

mellow citrus
#
$reason = Arr::first($motivoArray, function ($value, $key) use ($cphaNumber) {
    return Str::contains($value, $cphaNumber);
});
#

Something like that will accomplish what you want, I believe.

clever current
#

For this 'cpha' => '7050704970111921', I need just this 'motivo' => array ( 0 => '7050704970111921: n.º NIT incorreto.',

#

understand?

mellow citrus
#

Yes, read what I posted above.

#

It iterates your array, finds the first true state and returns that item in the array if one is there.

clever current
#

would it be this?

#
$reason = Arr::first($req->observacao, function ($value, $key) use ($resposta['numeroCpha']) {
                                return Str::contains($value,$resposta['numeroCpha']);
                            });```
mellow citrus
#

Depends what $req->observacao is.

clever current
#

returned a error

mellow citrus
#

What error?

clever current
#

my cod for you understand better

#
   public function failImp()
    {
        $requisicao = Requisicao::where(
            'situacao',
            Config::get('constants.db.requisicoes.situacao.recebida')
        )
            ->whereMonth('created_at', date('m'))
            ->whereYear('created_at', date('Y'))
            ->orderby('data_inicial', 'asc')
            ->get();

        $dados = [];

        foreach ($requisicao as $req) {

            if ($req->cphas_reject != []) {

                foreach ($req->cphas_reject as $r) {

                    foreach ($req->resposta as $resposta) {

                        if ($r == $resposta['numeroCpha']) {

                            $reason = Arr::first($req->observacao, function ($value, $key) use ($resposta['numeroCpha']) {
                                return Str::contains($value,$resposta['numeroCpha']);
                            });
Log::info($reason);
                            /*array_push($dados, [
                                'cpha' => $resposta['numeroCpha'],
                                'processo'   => $resposta['numeroProcesso'],
                                'data_nomeacao'     => $resposta['dataNomeacao'],
                                'vara'   => $resposta['vara'],
                                'cpf'      => $resposta['numeroCpf'],
                                'nit'      => $resposta['numeroNit'],
                                'advogado'      => $resposta['nomeAdvogado'],
                                'motivo'      => $req->observacao,
                            ]);*/
                        }
                    }
                }
            }
        }
        Log::info($dados);
       // return $dados;
    }```
#

here php $reason = Arr::first($req->observacao, function ($value, $key) use ($resposta['numeroCpha']) { this error syntax error, unexpected token "[", expecting ")"

mellow citrus
#

Ah, you can't pass the value like that in use.

#
$reason = Arr::first($req->observacao, function ($value, $key) use ($resposta) {
                                return Str::contains($value,$resposta['numeroCpha']);
                            });
clever current
#

Sorry to ask again but how should I pass? 😬

mellow citrus
#

I shared above, but pass $resposta and then access $responsa['numeroCpha'] inside the function.

mellow citrus
#

Yes.

clever current
#

same error

mellow citrus
#

Show full error.

clever current
#

this line php $reason = Arr::first($req->observacao, function ($value, $key) use ($resposta) { this error syntax error, unexpected token "[", expecting ")"

mellow citrus
#

That isn't the full error though, post the whole thing so I can see.

clever current
#

this line php return Str::contains($value,$resposta['numeroCpha']);this error in Str Undefined type 'App\Http\Controllers\api\Str'.intelephense(1009)

mellow citrus
#

You gotta include Str and Arr in your class.

clever current
mellow citrus
#

Maybe stale error from before code change, save it and run it and see what happens

clever current
#

Now the error is in importing Arr and Str

mellow citrus
#

Yeah, gotta make sure to import them -

#
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
clever current
#

It worked out! It worked!
Thank you so much!🙏🏿

#

saved my day

#

Thank you!