相同的json数组删除过程针对不同的值位置产生不同的结果数组 [英] Same json array delete process produces different result array for different position of value

查看:94
本文介绍了相同的json数组删除过程针对不同的值位置产生不同的结果数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json数组.其中我有3个值.当我删除第一个或第二个值时,删除后的结果数组是一种格式.但是,当我删除最后一个值时,结果数组总是会变化.我通过将其转换回字符串来回显该数组.

I have a json array. in which i have 3 values. when i delete first or second value the resultant array after deletion is one format. But when i delete the last value always the resultant array changes. i am echoing the array by converting it back to string.

当我删除除最后一个值以外的任何值时,例如第一个或第二个值或任何其他值

When i delete any value except the last one, that is like 1st or 2nd or any other value

result = {"1":{"name":"cat"},"2":{"name":"elephant"}}

result = {"1":{"name":"cat"},"2":{"name":"elephant"}}

我在这里删除了第一个值-狗

Here i deleted first value - dog

$animals = '{
 "0":{"name":"dog"},
 "1":{"name":"cat"},
 "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);
$del_value = "elephant";
$loginArray = array('name' => $del_value);
if (in_array($loginArray, $animals)) {
    foreach ($animals as $key => $value) {
        if (in_array($del_value, $value)) {
            unset($animals[$key]);
        }
    }
}
$animals_string = json_encode($animals);
echo $animals_string;

但是当我删除最后一个值时,它将创建不同的格式

but When i delete the last value it creates different format

result = [{{name:" dog},{" name:" cat}]

result = [{"name":"dog"},{"name":"cat"}]

在这里我删除了最后一个值-大象

Here i deleted last value - elephant

$animals = '{
 "0":{"name":"dog"},
 "1":{"name":"cat"},
 "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);
$del_value = "elephant";
$loginArray = array('name' => $del_value);
if (in_array($loginArray, $animals)) {
    foreach ($animals as $key => $value) {
        if (in_array($del_value, $value)) {
            unset($animals[$key]);
        }
    }
}
$animals_string = json_encode($animals);
echo $animals_string;

推荐答案

此行为是预期的.

PHP支持2种类型的数组:

PHP supports 2 type of array:

  1. 关联性:其中索引可以是字符串或非顺序整数
  2. 数字索引:其中索引是从0开始的连续整数

但是JSON仅支持第二种类型的数组,因此,当您将关联数组转换为JSON时,您会得到一个对象.

However JSON only supports the 2nd type of array, therefore when you convert an associative array to JSON you get an object instead.

示例:

print_r(json_encode([ 1,2,3,4 ])); //Prints [1,2,3,4]
print_r(json_encode([ 0=> 1, 1=>2, 2=>3, 3=>4 ])); //also prints [1,2,3,4]
print_r(json_encode([ "a" => 1, "b" => 2 ])); //Prints {"a":1,"b":2}
print_r(json_encode([ 1 => "a" , 2 => "b" ])); //Prints {"1":"a","2":"b"}

通过删除原始键将PHP数组(任何类型)强制为JSON数组:

Force PHP array (of any type) to JSON array by dropping original keys:

$animals_string = json_encode(array_values($animals)); //Will always have form : [{"name": "X" },{"name":"Y"}]

将PHP数组(任何类型)强制为JSON对象:

Force PHP array (of any type) to JSON object:

$animals_string = json_encode((object)$animals); //Will always have form : {"i1":{"name":"X"},"i2":{"name":"Y"}}

在您的特定情况下的问题是,您的JSON对象被转换为一个PHP数字索引数组,该数组在被编码回它时将成为一个JSON数组:

The problem in your particular case is that your JSON object gets translated into a PHP numerically indexed array which when encoded back it becomes a JSON array:

$animals = '{
   "0":{"name":"dog"},
   "1":{"name":"cat"},
   "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);

echo json_encode($animals); 

回声

[{"name":"dog"},{"name":"cat"},{"name":"elephant"}]

[{"name":"dog"},{"name":"cat"},{"name":"elephant"}]

如果删除除最后一个索引以外的一个索引,则将使PHP数组具有关联性,该关联将转换为JSON对象.

If you remove one of the indexes other than the last then this makes the PHP array associative which gets translated to a JSON object.

这就是PHP的工作方式:如果您想要不同的输出,则需要对其进行明确说明,如下所示:

This is is how PHP works if you want a different output you need to be explicit about it like below:

$animals = '{
 "0":{"name":"dog"},
 "1":{"name":"cat"},
 "2":{"name":"elephant"}
 }';
$animals = json_decode($animals, true);
$del_value = "elephant";
$loginArray = array('name' => $del_value);
if (in_array($loginArray, $animals)) {
    foreach ($animals as $key => $value) {
        if (in_array($del_value, $value)) {
            unset($animals[$key]);
        }
    }
}
$animals_string = json_encode((object)$animals);
echo $animals_string;  //Always the same format

我希望这会有所帮助.

这篇关于相同的json数组删除过程针对不同的值位置产生不同的结果数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆