Parameter usage of json_decode and var_export in php

Source: Internet
Author: User

This article mainly describes the usage of the second parameter of json_decode and var_export in php. If you need to know the usage of json_decode and var_export in php, you can also refer to the usage section.

Both json_decode and var_export have the second parameter. I have never noticed it before. T_T. Besides, this var_export function is also the first time I know about it. The basics are still not solid.

Json_decode
(PHP 5> = 5.2.0, PECL json: 1.2.0-1.2.1)
Json_decode-encode strings in JSON format
Description
Mixed json_decode (string $ json [, bool $ assoc])
Accept a JSON string and convert it to a PHP variable

In the past, json was used as the data format for ajax processing. The data format returned by the APIS provided by the partner today is json. At that time, I was still thinking that the returned data is an object, it can be directly used in javascript, but how can I convert this object into an array in php? After scratching your ears for half a day, I decided to read the manual. I don't know. I was shocked. It turns out there is a second parameter. If it is set to true, the decoded data will become an array. Happy.

When array is a continuous array starting from 0, the result of json_encode is a string enclosed [].

When array is an array that does not start from 0 or is not continuous, the result of json_encode is a string in the key-value mode enclosed {}.

The Code is as follows: Copy code

$ Test = array ();
$ Test [] = 1;
$ Test [] = 1;
$ Test [] = 1;
DEBUG (json_encode ($ test); Result:
[1, 1]


$ Test = array ();
$ Test [] = 1;
$ Test [] = 1;
$ Test [] = 1;
Unset ($ test [0]);
DEBUG (json_encode ($ test ));

Result:

{"1": 1, "2": 1} 2. When the string is in the [, 1] mode, the result parsed by json_decode is an array by default,

When the string is in the {"1": 1, "2": 1} mode, the result parsed by json_decode is an object by default, in this case, you can set its second parameter to true to force it to return an array.

 

3. Because php cannot distinguish between a one-dimensional array and a two-dimensional array, the above situation occurs, because it is recommended to set the second parameter to true when using json encoding.


--------------------------------------------------------------------------------
If you want to store the retrieved data in a file, the data format is an array, and the previous methods are serialize. When you get the data, deserialize it. Today, I am lazy, I want to know if I can directly store arrays in files?
But the array format is really difficult to fight, especially the associated array, the subscript, if I know it all, should I be exhausted? Ask Google's eldest brother.
Var_export
(PHP 4> = 4.2.0, PHP 5)
Var_export-String Representation of the output or returned variable
Description
Mixed var_export (mixed $ expression [, bool $ return])

This function returns the structure information about the variables passed to this function. It is similar to var_dump (). The difference is that the returned representation is legal PHP code.
You can set the second parameter of the function to TRUE to return the expression of the variable.
See? This function is awesome. var_dump has been used before. I don't know the name of var. It's good to say.

The Code is as follows: Copy code


$ Handle = fopen ($ file_name, 'W + ');
Fwrite ($ handle, '<? Php '. "n".' $ datas = '. var_export ($ result). "; n?> ");
Fclose ($ handle );

When the result is used, there is still a problem. This function outputs the array content to the page, and there is nothing in the file. Isn't that troublesome? If this is the case, why should I use it? It is displayed on the page that I use <pre>. It's depressing. I think this function also has a second parameter, which works the same way as json_decode. It seems that my carelessness is still very serious.

 

The Code is as follows: Copy code
$ Handle = fopen ($ file_name, 'W + ');
Fwrite ($ handle, '<? Php '. "n".' $ datas = '. var_export ($ result, true). "; n?> ");
Fclose ($ handle );


Instance

The Code is as follows: Copy code

$ Res = yblog_mspconfiginit ("ratings ");
Var_dump ($ res );
Var_export ($ res );

/* Result: resource (1) of type (yahoo_yblog) NULL */

 

For example:

The Code is as follows: Copy code


$ Res = fopen('status.html ', 'R ');
Var_dump ($ res );
Var_export ($ res );

/* Result: resource (2) of type (stream) NULL */

Var_export must return valid php code, that is, the Code returned by var_export can be directly assigned a variable as a php code. And this variable will get the same type value as var_export.

However, when the variable type is resource, it cannot be simply copied. Therefore, when the var_export variable is resource type, var_export returns NULL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.