array_search() expects Parameter 2 to be array, null Given in

Asked

Viewed 406 times

-1

I am consuming a Web Service for the first time, I created a function to scroll through the columns of a variable, find the value I need, using the array_search find another value in a different variable and replace the names the big problem is that, sometimes when reload the page it returns errors in several lines, sometimes in column arrays others in array_search or foreach:

array_column() expects Parameter 1 to be array, null Given in

on the line:
$loc = array_column($local, 'codigo');

array_search() expects Parameter 2 to be array, null Given in

on the lines:
$cat = array_column($categoria, 'codigo');
$categorizacao = $categoria[$key4]['nome'];
$key3 = array_search($result['local'], $loc);

and returned me the error:

Invalid argument supplied for foreach() in

on the line:
foreach($resultado as $result){

i tried to add several lines with is_array, but this is definitely not right, and the error still happens, I wonder if I can add more than one variable in is_array, to use only one, or a correct way to apply in my code, I’m beginner in php, I count on your help!

$retorno_resultados = array();

$colunas =  array_column($equipe, 'codigo'); 

$loc =  array_column($local, 'codigo');

$cat =  array_column($categoria, 'codigo');

foreach((array)$modalidade as $modali){

    if(!is_array($modali)){

        }else{

$mod =  array_column($modalidade, 'codigo');

    }

}

foreach((array)$campeonato as $camp){
    if(!is_array($camp)){

        }else{

            $key4 = array_search($camp['categoria'], $cat);
            $categorizacao = $categoria[$key4]['nome']; // <-- pega o valor

            $key5 = array_search($camp['modalidade'], $mod);
            $modalidadez = $modalidade[$key5]['nome']; // <-- pega o valor

    foreach((array)$resultado as $result){
        if(!is_array($result)){

        }else{

            // pesquisa chave do mandante
            $key1 = array_search($result['mandante'], $colunas);
            $mandante = $equipe[$key1]['nome']; // <-- pega o valor

            // pesquisa chave do visitante
            $key2 = array_search($result['visitante'], $colunas);
            $visitante = $equipe[$key2]['nome']; // <-- pega o valor

            // pesquisa chave do local
            $key3 = array_search($result['local'], $loc);
            $localizacao = $local[$key3]['nome']; // <-- pega o valor

            $data = $result['data'];
            $data = date('d/m/Y', strtotime($data));

            $i = 0;

$columns

 {
"codigo": "26",
"nome": "Xingu SA",
"nomeabreviado": "XINGU",
"url": "xingu-sa",
"telefone": "",
"email": "",
"endereco": "",
"complemento": null,
"bairro": "",
"cidade": "",
"uf": "",
"cep": ""
},
  "Total de registros: 36"
],

$mod

{
"codigo": "256",
"nome": "Xadrez",
"tipo": "6"
},
  "Total de registros: 47"
],

$Loc

{
"codigo": "17",
"nome": "Villare SCS",
"modalidade": "16",
"telefone": "",
"email": "",
"endereco": "",
"complemento": null,
"bairro": "",
"cidade": "",
"uf": "",
"cep": ""
},
  "Total de registros: 21"
],

$cat

{
"codigo": "36",
"nome": "Sub 10",
"ordem": "340"
},
  "Total de registros: 36"
],
  • The error is quite clear, the 2nd parameter that is passing in the array_search is not an array and should be. Now not knowing which line gave the error, nor which values have the data arrays $colunas, $loc and $cat is hard to answer.

  • the variables $columns, $Loc, $cat, take values return in json, are many, I will add the last two of each, and the rows with error, thanks for the answer!

  • @Isac edited the question, that’s what we need?

  • I believe the error is due to this last line of JSON returns &"Total entries: 36" so I used is_array as a condition.

  • I wonder if you put json directly into the variable and it is a text or if you actually have an array. var_dump($categoria); and var_dump($cat); give what ?

  • so, what I wanted to do was take two values, for example, $columns['code'], if it equals $result['sender'], bring me the name of the team, which is in $columns ($team, 'name');, this function is only to replace id by name.

  • I gave the var_dump in the code, if you want to have a look it is printed in the home of this url http://circuitoescolar.com.br/

  • denied my question, I’m researching for two days, but I could not solve because I’m really beginner, I just need an explanation...

Show 3 more comments

1 answer

3


  • in case it would be like this? $key5 = array_search($mod, $camp['modality']);

  • if I invert the order of the parameters, it returns the same error in the second...

  • Do I happen to be able to compare more than one variable within a single is_array? for example if(! is_array($camp, $result)){

  • no, but you can do two checks using the logical operator E (&&) if(! is_array($camp) && ! is_array($result)){}

  • in the search array, the first parameter is the value you want to search for, and the second parameter is the array

  • Got it, very good, thanks for the help!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.