Maison  >  Article  >  développement back-end  >  Comment convertir un tableau en tableau json en php

Comment convertir un tableau en tableau json en php

藏色散人
藏色散人original
2020-11-20 09:03:192805parcourir

Comment convertir un tableau en tableau json en PHP : créez d'abord un exemple de fichier PHP ; puis définissez un tableau ordinaire ; et enfin utilisez la méthode "json_encode($array);" dans un tableau json.

Comment convertir un tableau en tableau json en php

Recommandé : "Tutoriel vidéo PHP"

Conversion mutuelle entre les tableaux php et les tableaux json

Le code est le suivant :

$array = array( //普通数组
              "name" => "Tom",
              "age" => 22
         );
echo "第一组:普通PHP数组转json数组——".json_encode($array); //json数组
echo "<br />";
//*注:urlencode()函数原理就是首先把中文字符转换为十六进制,然后在每个字符前面加一个标识符%。
//urldecode()函数是用于解码已编码的URL字符串,其原理就是把十六进制字符串转换为中文字符
$arr = array(  //中文数组
       &#39;0&#39; => urlencode(&#39;信息工程系&#39;),
       &#39;1&#39; => urlencode(&#39;机械工程系&#39;)
       );
echo "第二组:PHP包含中文的数组转json数组——".urldecode(json_encode($arr));
echo "<br />";
//*注:json数组的键与值若有中文时,中文只能用双引号“”,不能使用单引号,外面使用花括弧,并用单引号括起来
echo "第三组:json数组转PHP数组——";
$json =&#39;{"a":"哈哈","b":"我","c":"数","d":"一","e":3,"f":2,"g":1,"h":"别生气了"}&#39;;    
var_dump(json_decode($json,true));

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn