2

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

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
4

3 回答 3

1

尝试删除引号:

unset($array[52]);

于 2012-12-21T14:08:08.613 回答
1

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

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

发生什么了?

从与 rid 的讨论中,我提供了以下编译的见解。

PHP 手册指出,使用强制转换语法将对象转换为数组(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 上测试)

于 2012-12-21T14:57:37.620 回答
0

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

于 2012-12-21T14:45:33.480 回答