将数组数组转换为json

时间:2021-11-25 20:21:57

i want to convert this array as shown below

我想转换这个数组,如下所示

array_countries=array("20"=>array("cntryValue"=>"3","cntryLabel"=>"Egypt","value"=>"7","label"=>"Tanta"),
"21"=>array("cntryValue"=>"3","cntryLabel"=>"Egypt","value"=>"1000","label"=>"Other"),
"22"=>array("cntryValue"=>"80","cntryLabel"=>"India","value"=>"0","label"=>"All"),
"23"=>array("cntryValue"=>"80","cntryLabel"=>"India","value"=>"1","label"=>"Ahmedabad"));

into this format:

进入这种格式:

"3":{"Egypt":{"7":Tanta,"1000":"other"}},"80":{"India":{"0":"All","1":Ahmedabad}}

I am using PHP and not able to figure out how can i do this. I have used json_encode. but the results are not correct.I am using PHP as my language.

我正在使用PHP而无法弄清楚我该怎么做。我用过json_encode。但结果不正确。我使用PHP作为我的语言。

thanks in advance

提前致谢

3 个解决方案

#1


1  

Before converting into json, you should change the array:

在转换为json之前,您应该更改数组:

$array_countries_formated = array();

foreach ($array_countries as $item)
{
    $array_countries_formated[$item['cntryValue']][$item['cntryLabel']][$item['value']] = $item['label'];
}

echo $array_countries_formated = json_encode($array_countries_formated, JSON_FORCE_OBJECT);

Result:

{"3":{"Egypt":{"7":"Tanta","1000":"Other"}},"80":{"India":{"0":"All","1":"Ahmedabad"}}}

#2


0  

Try this...

<?php
$array_countries=array("20"=>array("cntryValue"=>"3","cntryLabel"=>"Egypt","value"=>"7","label"=>"Tanta"),
"21"=>array("cntryValue"=>"3","cntryLabel"=>"Egypt","value"=>"1000","label"=>"Other"),
"22"=>array("cntryValue"=>"80","cntryLabel"=>"India","value"=>"0","label"=>"All"),
"23"=>array("cntryValue"=>"80","cntryLabel"=>"India","value"=>"1","label"=>"Ahmedabad"));

$jsonString = json_encode($array_countries);

print_r($jsonString);
?>

Result:

{"20":{"cntryValue":"3","cntryLabel":"Egypt","value":"7","label":"Tanta"},"21":{"cntryValue":"3","cntryLabel":"Egypt","value":"1000","label":"Other"},"22":{"cntryValue":"80","cntryLabel":"India","value":"0","label":"All"},"23":{"cntryValue":"80","cntryLabel":"India","value":"1","label":"Ahmedabad"}}

#3


0  

If I understand your requested output correctly, the following should do the trick:

如果我正确理解您请求的输出,以下应该可以解决问题:

$result = array();
foreach ($array_countries as $country) {
    $cntryValue = $country["cntryValue"];
    if (!array_key_exists($cntryValue, $result)) {
        $result[$cntryValue] = array();
    }
    $result[$cntryValue][$country["cntryLabel"]] = array();
    $result[$cntryValue][$country["cntryLabel"]][$country["value"]] = $country["label"];
}

echo json_encode($result) . "\n";

A little explanation: the array provided ($array_countries) is formatted differently as compared to your requested output. Therefore, it should be converted. That is what the foreach loop does. The formatted result can be converted to json using the json_encode function.

一点解释:与您请求的输出相比,提供的数组($ array_countries)的格式不同。因此,它应该被转换。这就是foreach循环的作用。可以使用json_encode函数将格式化结果转换为json。

#1


1  

Before converting into json, you should change the array:

在转换为json之前,您应该更改数组:

$array_countries_formated = array();

foreach ($array_countries as $item)
{
    $array_countries_formated[$item['cntryValue']][$item['cntryLabel']][$item['value']] = $item['label'];
}

echo $array_countries_formated = json_encode($array_countries_formated, JSON_FORCE_OBJECT);

Result:

{"3":{"Egypt":{"7":"Tanta","1000":"Other"}},"80":{"India":{"0":"All","1":"Ahmedabad"}}}

#2


0  

Try this...

<?php
$array_countries=array("20"=>array("cntryValue"=>"3","cntryLabel"=>"Egypt","value"=>"7","label"=>"Tanta"),
"21"=>array("cntryValue"=>"3","cntryLabel"=>"Egypt","value"=>"1000","label"=>"Other"),
"22"=>array("cntryValue"=>"80","cntryLabel"=>"India","value"=>"0","label"=>"All"),
"23"=>array("cntryValue"=>"80","cntryLabel"=>"India","value"=>"1","label"=>"Ahmedabad"));

$jsonString = json_encode($array_countries);

print_r($jsonString);
?>

Result:

{"20":{"cntryValue":"3","cntryLabel":"Egypt","value":"7","label":"Tanta"},"21":{"cntryValue":"3","cntryLabel":"Egypt","value":"1000","label":"Other"},"22":{"cntryValue":"80","cntryLabel":"India","value":"0","label":"All"},"23":{"cntryValue":"80","cntryLabel":"India","value":"1","label":"Ahmedabad"}}

#3


0  

If I understand your requested output correctly, the following should do the trick:

如果我正确理解您请求的输出,以下应该可以解决问题:

$result = array();
foreach ($array_countries as $country) {
    $cntryValue = $country["cntryValue"];
    if (!array_key_exists($cntryValue, $result)) {
        $result[$cntryValue] = array();
    }
    $result[$cntryValue][$country["cntryLabel"]] = array();
    $result[$cntryValue][$country["cntryLabel"]][$country["value"]] = $country["label"];
}

echo json_encode($result) . "\n";

A little explanation: the array provided ($array_countries) is formatted differently as compared to your requested output. Therefore, it should be converted. That is what the foreach loop does. The formatted result can be converted to json using the json_encode function.

一点解释:与您请求的输出相比,提供的数组($ array_countries)的格式不同。因此,它应该被转换。这就是foreach循环的作用。可以使用json_encode函数将格式化结果转换为json。