【问题标题】:Unsetting array values (after casting an object to an array)取消设置数组值(将对象转换为数组后)
【发布时间】:2012-12-21 13:59:53
【问题描述】:

您好,我正在尝试从该数组中取消设置数组元素

Array(
    [52] => stdClass Object (
        [name] => test
        [company] => sf
        [addr] => sdf
        [email] => sdf
        [phone] => sdf
        [comments] => sdf
        [qty] => 150
        [date] =>
        [brand] => Nike
        [quoteimg] => xx
        [enqimg] => xxx
        [product_name] => t5
        [key] => 52
        [action] => new_product
    )
)

我正在使用unset($array['52']);

但它不起作用,但原因不明。

function ajax_new_product(){
    $r=(array)json_decode(base64_decode($_COOKIE['products']));

    if (isset($_POST['remove']))
        {   print_r($r);
            unset($r[$_POST['key']+0]);
            print_r($r);
        }   
    else 
        if(is_array($r) && !empty($r))
            if (isset ($_POST['key']))
                $r[$_POST['key']]=(array)$_POST;
            else        
                $r[]=(array)$_POST;     
        else 
            if (isset ($_POST['key']))
                $r[$_POST['key']]=(array)$_POST;
            else        
                $r[]=(array)$_POST;     
    setcookie('products',base64_encode(json_encode($r)),time()+60*60*24*30,"/");
}

这段代码:

if (isset($_POST['remove']))
    {   print_r($r);
        unset($r[$_POST['key']+0]);
        print_r($r);
    }

输出

Array
(
    [52] => stdClass Object
        (
            [name] => test
            [company] => sf
            [addr] => sdf
            [email] => sdf
            [phone] => sdf
            [comments] => sdf
            [qty] => 150
            [date] => 
            [brand] => Nike
            [quoteimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7503-195x196.jpg
            [enqimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7503-65x66.jpg
            [product_name] => t5
            [key] => 52
            [action] => new_product
        )

    [49] => stdClass Object
        (
            [name] => 
            [company] => 
            [addr] => 
            [email] => 
            [phone] => 
            [comments] => 
            [qty] => 150
            [date] => 
            [brand] => Nike
            [quoteimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/ts21-164x196.png
            [enqimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/ts21-65x66.png
            [product_name] => t3
            [key] => 49
            [action] => new_product
        )

    [44] => stdClass Object
        (
            [name] => 
            [company] => 
            [addr] => 
            [email] => 
            [phone] => 
            [comments] => 
            [qty] => 150
            [date] => 
            [brand] => Nike
            [quoteimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7501-195x196.jpg
            [enqimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7501-65x66.jpg
            [product_name] => Polo
            [key] => 44
            [action] => new_product
        )

)
Array
(
    [52] => stdClass Object
        (
            [name] => test
            [company] => sf
            [addr] => sdf
            [email] => sdf
            [phone] => sdf
            [comments] => sdf
            [qty] => 150
            [date] => 
            [brand] => Nike
            [quoteimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7503-195x196.jpg
            [enqimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7503-65x66.jpg
            [product_name] => t5
            [key] => 52
            [action] => new_product
        )

    [49] => stdClass Object
        (
            [name] => 
            [company] => 
            [addr] => 
            [email] => 
            [phone] => 
            [comments] => 
            [qty] => 150
            [date] => 
            [brand] => Nike
            [quoteimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/ts21-164x196.png
            [enqimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/ts21-65x66.png
            [product_name] => t3
            [key] => 49
            [action] => new_product
        )

    [44] => stdClass Object
        (
            [name] => 
            [company] => 
            [addr] => 
            [email] => 
            [phone] => 
            [comments] => 
            [qty] => 150
            [date] => 
            [brand] => Nike
            [quoteimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7501-195x196.jpg
            [enqimg] => http://verycreative.info/cristian/custompolos/wp/wp-content/uploads/2012/12/DESIGN_WONT_SAVE_THE_WORLD_TEE_AA_7501-65x66.jpg
            [product_name] => Polo
            [key] => 44
            [action] => new_product
        )

)
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/veryinfo/public_html/cristian/custompolos/wp/wp-content/themes/custompolos/functions.php:32) in <b>/home/veryinfo/public_html/cristian/custompolos/wp/wp-content/themes/custompolos/functions.php</b> on line <b>48</b><br />
0

【问题讨论】:

  • 不要在数字数组索引中使用引号/撇号,即 $array[52] 因为 52 是一个数字,但 $array['someKey'] 因为 someKey 是一个字符串。其次,数组必须在变量中,例如 $array = array(52 => $object);变量名为 $array 吗?确保你运行 unset($variableName[52]);而不是只说 $array.
  • 一些代码会很有用。
  • 稍微充实一下:unset($array['52'+0]);
  • 抱歉代码乱七八糟,但我已经尝试了很多东西
  • @rid 谢谢你的澄清,我知道,但我们不关心 PHP 的实现细节。该数组清楚地通过数字索引进行索引,因此最好这样访问它。

标签: php arrays object casting json


【解决方案1】:

返回的对象似乎并没有真正使用(array) 转换完全转换为数组。然而,取消设置仅对数字索引有影响。

要修复它,请使用json_decode()第二个参数,它首先会返回一个真正的数组,并且一切正常。

会发生什么?

从与 rid 的讨论中,我提供以下综合见解。

The PHP Manual 声明使用(array) 转换语法将对象转换为数组会使数字索引无法访问,但是 var_dump 似乎暗示了一些不同的东西:

$a = (array) json_decode('{"a": 123, "5": 234}');
var_dump($a);
// array(2) {
//   ["a"]=> int(123)
//   ["5"]=> int(234)
// }

当他们试图从数组中删除一个数字索引条目时,出现了 OP 问题,在这种情况下,这将尝试 unset($a[5]); 这根本不会影响数组。事实上,这段代码表明什么都没有:

var_dump(isset($a['5'])); // bool(false)
var_dump($a['5']);        // NULL
$a["5"] = 1111;    
var_dump($a['5']);        // 1111

那么,一切都好吗?好吧...差不多,让我们在最近的分配之后再冒险一次 var_dump

var_dump($a);
// array(3) {
//   ["a"]=>int(123)
//   ["5"]=>int(234)
//   [5]=>int(1111)
// }

这很可疑,但我们已经在上面确定$a[5] 返回正确的新1111。而且我们无论如何也无法访问那个隐藏的五个。正确的? ……对吧?

foreach($a as $k=>$e) echo "$k -> $e\n";
// a -> 123
// 5 -> 234
// 5 -> 1111

这当然是错误的。错了……

所以我认为,最好不要将对象转换为当前 PHP 版本的数组,而是使用 foreach 来遍历它们并以这种方式创建一个全新的数组。也就是说,如果您一开始就无法获得真正的数组。

(在 PHP 5.4.8 Win 上测试)

【讨论】:

  • 确实如此。 the documentation 中有一个部分在谈论这个:“如果将对象转换为数组,则结果是一个数组,其元素是对象的属性。键是成员变量名称,有一些值得注意的例外:整数属性是无法访问 [...]"
  • @rid 有趣的是,整数属性是可访问的 :) 感谢您的链接,我正在寻找该部分...
  • @dualed,这还不是一半......它们似乎既不可读也不可写,因为您无法访问它们。看哪:$a = (array) json_decode('{"a": 123, "5": 234}'); var_dump($a["5"]); $a["5"] = 1111; var_dump($a, $a["5"]);!经过 10 年的 PHP,它从未停止让我感到惊讶。显然每天都有一些关于 PHP 的新知识要学习。
  • @rid 我只是在想“我真的想知道吗?”,尽管至少访问 5 会产生 1111...
  • @dualed,它只在分配它之后才这样做,因为它是5...的新种类 ...(仅在PHP中)
【解决方案2】:

尝试删除引号:

unset($array[52]);

【讨论】:

    【解决方案3】:

    您可以使用 array_shift($array) 来消除数组中的第一个索引

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 2019-07-29
      • 1970-01-01
      • 2019-04-26
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多