PHP two-dimensional array modifier key name

Source: Internet
Author: User
Tags object object php online

Recently encountered a problem, is about JSON data submission, always quote "Object Object" error, check the evening need for information, most of the argument is the JSON data format is not standardized caused by errors. It is generally recommended that the datatype type be commented out. But none of them have been tried. Finally, I suspect the use of the PHP version of the Json_encode function, and so on, are not resolved. The main I submitted in the background is a one-dimensional array, and there is no key value of a one-dimensional array. So some colleagues say that my JSON data format is not canonical and must have keys and values. This I key egg ache, originally used thinkphp the way to find out the data is a two-dimensional array, processed data processing into a one-dimensional array, that my one-dimensional array can be used exactly, but using the Json_encode function to convert out there is no key, only the value. Always error. Let me get a two-bit array. This is the norm. So I went to a two-dimensional array to a one-dimensional array, the data is processed and converted to two-bit array, but after the conversion of the two-bit array key name No, I hurt the egg online find a way to share to everyone. Nonsense can not be looked at.

This is the code:

1 $ar=Array(2   Array(1 = ' a ', 2 =, 3 =, 4 = ' long ', 5 = ' zzz ', 6 = ' kkk ', 7 = ' ooo '),3   Array(1 = ' B ', 2 =, 3 =, 4 = ' King ', 5 = ' lll ', 6 = ' TTT ', 7 = ' PPP '),4   Array(1 = ' C ', 2 =, 3 =, 4 = ' quit ', 5 = ' QQQ ', 6 = ' xxx ', 7 = ' CCC '),5 );6 $kname=Array(' StaffID ', ' wage ', ' Name ', ' work ', ' Type ');7  8 functionFoo (&$v,$k,$kname) {9   $v=Array_combine($kname,Array_slice($v, 1,-1));Ten } One   A Array_walk($ar, ' foo ',$kname); - Print_r($ar);

Here is the output:

1 Array2 (3[0] = =Array4         (5[StaffID] = 506[Wage] = 607[Name] + =Long8[Work] =zzz9[Type] = =KKKTen         ) One   A[1] = =Array -         ( -[StaffID] = 60 the[Wage] = 70 -[Name] + =King -[Work] =lll -[Type] = =TTT +         ) -   +[2] = =Array A         ( at[StaffID] = 70 -[Wage] = 80 -[Name] + =quit -[Work] =QQQ -[Type] = =XXX -         ) in   -)

Finally, share to everyone a PHP online code debugging tool, sometimes inconvenient to run PHP code, you can write in this tool debugging, and then write in the project, it is convenient, but there are errors when the error will not be. But very satisfied, almost forgot, this is the tool address http://tool.lu/coderunner/

And finally, did not say I this error how to solve, this error is due to PHP cache problem, will be added to the JSON data space, resulting in JSON data is not standardized error, before the data returned to add a Ob_end_clean ();

PHP two-dimensional array modifier key name

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.