9
Array
(
    [sEcho] => 1
    [iTotalRecords] => 7521
    [iTotalDisplayRecords] => 1
    [aaData] => Array
        (
            [0] => Array
                (
                    [0] => Nordic Capital Buys SiC Processing
                    [1] => 2010-06-21/nordic-capital-buys-sic-processing
                    [2] => PEHub Media
                    [3] => Business
                    [4] => completed
                    [5] => Nordic Capital has acquired a 70% stake in SiC Processing AG, a German industrial recycling company, from Frog Capital. No sale price was disclosed.  SiC Processing’s founding family retains a 25% holding, while former lead investor Zouk Ventures retains a 5% stake.

                    [6] => Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States
                )

        )

)

echo json_encode($myArr);

{"sEcho":"1","iTotalRecords":7521,"iTotalDisplayRecords":"1","aaData":[[" Nordic Capital Buys SiC Processing</a></div>"," 2010-06-21/nordic-capital-buys-sic-processing</div>","PEHub Media","Business","completed",null," Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States]]}

Note the null in the middle of the string after completed

Why is this, what escape/manipulation do I need to perform in order to encode this?

I have tried, addslashes

3
  • Works for me. Check your PHP configuration, I think json_* stuff can be disabled. Commented Jul 5, 2010 at 15:38
  • Please provide the PHP code used to construct the example array (and please check your questions carefully when posting - examples provided should do what you intend them to!) Commented Jul 5, 2010 at 16:06
  • 1
    Can we get a var_export of the source array please? Commented Jul 5, 2010 at 16:07

4 Answers 4

24

From the manual:

Note that if you try to encode an array containing non-utf values, you'll get null values in the resulting JSON string. You can batch-encode all the elements of an array with the array_map function:

$encodedArray = array_map(utf8_encode, $myArr);
echo json_encode($encodedArray);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks seengee, perfect. I actually used array_map(htmlentities... for my situation, but this saved the day.
It should be noted that suggesting utf8_encode() is not usually a great idea, as it blindly assumes the input is ISO-8859-1. If you're facing the problem, it's important that you first figure out what your character set IS, and only THEN convert (php.net/iconv can help with the second part if you're NOT dealing with ISO-8859-1).
1

Actually it doesn't return null, http://codepad.org/A34KdUf5.

Maybe your PHP version doesn't support json_encode().

2 Comments

Please check my question again i have updated with full code, as json_encode is working with rest of array.
In copying and pasting, you probably inadvertently put it in UTF-8 form, while the original string likely wasn't.
1

Works for me on 5.2.13. Ensure you're using at least PHP 5.2.0 and that PHP wasn't compiled with --disable-json. You may also want to check that error reporting (and/or logging) is enabled.

1 Comment

Please check my question again i have updated with full code, as json_encode is working with rest of array.
0

The simpler way is $store_name = utf8_encode(name_of_varaible) but please make sure that your character set is ISO-8859-1.

Comments

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.