Array is not as array

Asked

Viewed 131 times

1

I have the following code

<?php
while($data = $QuerySearch->fetch(PDO::FETCH_ASSOC)){

                $js[] = $data;


        }

            $r = array();
            $arr = array();

        foreach($js as $k=>$v){

            foreach($v as $col=>$c){

                $arr[] = $c;
            }

            array_push($r, $arr);



        }

        $r = array_map('htmlentities', $arr);

        if(isset($r)){

            echo 'Existe<br>';

            if(is_array($arr)){

                echo 'Tb existe';
            }else{

                echo 'nao existe arr';
            }
        }else{

            echo 'nao existe';
        }

        echo print_r($r);
?>

As you can see, the array is being formed, but it does not display the array. When I put one echo in place of arr[] it normally displays the data, now I put a echo json_encode($arr); it gets empty, and in the checks I did above displays like this:

Existe
Não existe arr

I mean, there is $arr[] but he says it’s not a array()

  • 1

    And this line here: $arr = ''; at the end of foreach, she’s converting $arr in string nay?

  • @Cahe Really, only when running 1 loop, I need that type, zeroes the Indice pq will create + arrays. to look like arr[0] => data, arr[1] =>data... I put $arr[] = array(); displays an empty array

  • @Cahe I gave print_r($arr) and he shows me all the data, but I put json_encode it shows nothing.

  • What do you want to do with $r? Shouldn’t be the $r that you check on if/Else below? That line $arr = ''; is erasing the $arr, take it off.

  • @Sergio did everything you said and when I give echo json_encode($r) he does not appear, now I put print_r($r)it appears the data I need

  • And if you do var_dump($r); what appears?

  • @Sergio appears the same things as the print_r . The strange thing is that the others json_encode() works properly, only I use it like this json_encode(array('indice'=>'valor')); forwardness of json_encode($r);

  • There should be a kind of answer {"indice":"valor"}. You can enter the code related to this json_encode, so you can see how it is doing. Now there is no json_encode in the question...

  • Htmlentities also does not support accentuation without passing ISO 8859 encoding parameters... And a detail, please do not modify the original code with error in the question, if you add the corrected below...

Show 4 more comments

1 answer

2


Set the charset on the connection with utf8.

PHP says:
This function only works with UTF-8 encoding data

$db->query("SET NAMES 'utf8'");
  • yours is working, only it only displays the last array because it loops a list and the indices are equal and I needed them to look like this array('0'=>dados1, '1'=>dados2) where data1 and data2 are the Dice and value returned from the records

  • The only thing I’ve been through is how to fix the data for the json to run, maybe your array is breaking at some point. I will test here with any array

  • as it is not separating if it is the list1, Lista2 so its indices are numbered from 1 to 44 being q was 1-22 and from 23-44 so change the index of the main array

  • Comment on the following line $arr = ''; she’s at the end of foreach and always this zeroing. When I commented the result was: Existe, Tb existe

  • Actually, that line has already been drawn. Now it is. &#xA; $r = array();&#xA; $arr = array();&#xA;&#xA; foreach($js as $k=>$v){&#xA;&#xA; foreach($v as $col=>$c){&#xA;&#xA; $arr[] = $c;&#xA; }&#xA;&#xA; array_push($r, $arr);&#xA;&#xA;&#xA;&#xA; }&#xA;&#xA; $r = array_map('htmlentities', $arr);

  • Then update the code in your question to make it easier

  • All right, I updated.

  • I tested your code as is and the return was Existe, Tb existe. Realize that $r and $arr are the same thing, there is no way to fall into if denial.

  • Show! That’s right, the encoding. Just went to change and it all worked out! Thanks @Papacharlie

  • Arrange, then take a look at DB encodings to maintain a pattern

  • All right, can dx.

Show 7 more comments

Browser other questions tagged

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