"Illegal string offset" error when calling usort in bank data

0

I am trying to sort the result by a specific column of an array of bank data.

 function cmp($a, $b) {
 return strcmp($a['usu_codigo'], $b['usu_codigo']);
  };

usort($usu_info_coluna, "cmp");

Giving var_dump to $usu_info_Coluna , it returns:

array(21) { ["usu_codigo"]=> int(1) ["usu_nome"]=> string(5) "LUANN" ["usu_senha"]=> string(60) "$2y$10$I4RqdKD/cOwRDNpFgtIbWeVirNIfxHPREMEklGaBuONGRZMtQfUgq" ["usu_sobrenome"]=> string(5) "SOUSA" ["usu_cpf"]=> string(11) "12345678998" ["usu_rg"]=> string(9) "123456789" ["usu_nasc"]=> string(10) "2018-10-22" ["usu_endereco"]=> string(4) "AV 2" ["usu_numero"]=> string(2) "97" ["usu_bairro"]=> string(8) "BLOCO D2" ["usu_cep"]=> string(8) "11900000" ["usu_cidade"]=> string(11) "REGISTRO-SP" ["usu_uf"]=> string(2) "SP" ["usu_tel"]=> string(10) "1338226293" ["usu_cel"]=> string(11) "13997821923" ["usu_genero"]=> string(9) "Masculino" ["usu_situacao"]=> string(5) "ativo" ["usu_email"]=> string(22) "[email protected]" ["usu_indicador_codigo"]=> NULL ["usu_datacadastro"]=> string(10) "2018-10-22" ["usu_nivel"]=> string(3) "adm" } 

What did I miss using usort? I changed and changed the usort code, but it always gives me a different error, and I believe this was the closest I got.

UPDATE 0 : This is the prepared query I use for the query:

$_SESSION['codigo'] = 1; 
$usu_codigo = $_SESSION['codigo'];
$usu_situacao = 'ativo';

$stmt3 = $conexao->prepare('SELECT * FROM esc_usuarios WHERE usu_indicador_codigo = ?');
$stmt3->bind_param('i', $usu_codigo);
$stmt3->execute();
$usu_ult5_cad = $stmt3->get_result();

Displaying Through a Table:

<?php

$limit = 2;
while($limit -- && $coluna_ult5 = $usu_ult5_cad->fetch_array()){

    ?>
    <tbody>
    <tr>
      <th><?php echo $coluna_ult5['usu_codigo']; ?><br></th>
      <td><?php echo $coluna_ult5['usu_nome']; ?></td>
      <td><?php echo $coluna_ult5['usu_sobrenome']; ?><br></td>
    </tr>                                               
<?php }

? >

    
asked by anonymous 25.10.2018 / 14:54

2 answers

2

The first step of all is to try to translate the error to know what it is.

Illegal string offset is returned when you attempt to get a offset of a string, passing string per parameter.

See:

$a = 'wallace';

var_dump($a[0]); // string 'w'

var_dump($a['string']); // Erro: Illegal string offset 'string' on line 1

See the test on ideone .

This is because the [] operator works for both array , and string in PHP. In the case of string you can get the character according to the last position.

However, if you pass a string, it generates an error. This is something that PHP allows for strings and few people know.

What's happening inside your function is basically this.

When you pass your array to usort , it will get the items of your array from 2 and 2 and pass them by the usort callback parameter.

Exemplifying the case of your array , it looks like this:

  ['a' => 1, 'b' => 2, 'c' => 3]

If you pass this array to usort , callback would put 1 on $a and 2 on $b .

And so it's giving Illegal string offset error. Because you assumed that within the callback you were passing array , but you are actually passing strings or integers.

To be able to work the way you're trying to do, your array should look like this:

  [
        ['usu_codigo' => 1, /** resto do array **/],
        ['usu_codigo'   => 2, /** resto do array **/],
        ['usu_codigo'   => 3, /** resto do array **/],
  ]

See an example on ideone

    
25.10.2018 / 17:54
-1

Edit: As corrected in the comments, the error "Illegal string offset" happens when you try to access the index of a string using another string (as explained better in the other answer ).

And the " Undefined offset " error (which I had mistakenly mistaken before) happens when you try to access an index that does not exist in the array. In this error you can use " isset " to check and test the value of the array before performing any operation.

    
25.10.2018 / 15:48