0

When trying to send an array, and I know that the array contains data because I checked with var_dump, the json_encode does not send any data. This is my code:

for($i=0;$i<count($dados_atividades)-1;$i++)
    {
        $arr[$i+1]['Descricao'] = $dados_atividades[$i]['Descricao'];
        $arr[$i+1]['DataInicioPrevista'] = $dados_atividades[$i]['DataInicioPrevista'];
        $arr[$i+1]['DataConclusaoPrevista'] = $dados_atividades[$i]['DataConclusaoPrevista'];
    }
    if(count($arr)>0) {
        echo json_encode($arr);
    }else{
        $arr = array();
        $arr[0]['Descricao'] = 'N/A';
        echo json_encode($arr);
    }

As you can see in the picture, the response does not contain any php echo without var_dump

and this is my code with var_dump:

for($i=0;$i<count($dados_atividades)-1;$i++)
    {
        $arr[$i+1]['Descricao'] = $dados_atividades[$i]['Descricao'];
        $arr[$i+1]['DataInicioPrevista'] = $dados_atividades[$i]['DataInicioPrevista'];
        $arr[$i+1]['DataConclusaoPrevista'] = $dados_atividades[$i]['DataConclusaoPrevista'];
    }
    if(count($arr)>0) {
    var_dump($arr);
        echo json_encode($arr);
    }else{
        $arr = array();
        $arr[0]['Descricao'] = 'N/A';

        echo json_encode($arr);
    }

and as you can see in the picture, the array contains data:

with var_dump

What can I do to fix this?

5
  • Is json_encode returning false? Use php.net/manual/en/function.json-last-error.php to determine the cause, if any. Commented Jan 11, 2016 at 12:54
  • Could it be possible that the echo and json_encode is working but it isnt outputting to the page you are seeing? Also, try a var_dump(json_encode($arr)) and see what happens Commented Jan 11, 2016 at 12:59
  • @MartinCurrah The point is that it works with small arrays, with 10 sizes +/- Commented Jan 11, 2016 at 13:12
  • @MartinCurrah with var_dump (json_encode ($ arr)), give me bool (false) Commented Jan 11, 2016 at 13:14
  • SHow us what goes on just before you start the piece of code you have shown us Commented Jan 11, 2016 at 13:43

2 Answers 2

1

To expand upon the previous answer you need to encode the array data to UTF-8

function utf8_converter($array)
{
    array_walk_recursive($array, function(&$item, $key){
        if(!mb_detect_encoding($item, 'utf-8', true)){
            $item = utf8_encode($item);
        }
    });

    return $array;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reminder, but this code actually generates 3 Warnings
give me warning like this: "b>Warning</b>: htmlentities() expects parameter 1 to be string,"
I have updated the code. Run $arr through this function before echoing it
0

Instead of echoing out the json each time round the loop, build the array in the loop and then echo the json once it has completed.

The browser is expecting to see one reply to the AJAX call and not multiple replies.

for($i=0;$i<count($dados_atividades)-1;$i++)
{
        $arr[$i+1]['Descricao'] = $dados_atividades[$i]['Descricao'];
        $arr[$i+1]['DataInicioPrevista'] = $dados_atividades[$i]['DataInicioPrevista'];
        $arr[$i+1]['DataConclusaoPrevista'] = $dados_atividades[$i]['DataConclusaoPrevista'];
    }
}

if(count($arr) == 0) {
    $arr = array();
    $arr[]['Descricao'] = 'N/A';
}

$json_string = json_encode($arr,JSON_UNESCAPED_UNICODE);

if ( json_last_error() > 0 ) {
    file_put_contents('json_debug.txt', json_last_error_msg() );
} else {
    echo $json_string;
}
exit;

It sounds like you are getting a Malformed UTF-8 characters, possibly incorrectly encoded error, so change this line like so

$json_string = json_encode($arr,JSON_UNESCAPED_UNICODE);

2 Comments

Same thing, because the array contains data
same error in the txt file: Malformed UTF-8 characters, possibly incorrectly encoded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.