您好我正在尝试从此数组中取消设置数组元素
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
答案 0 :(得分:1)
尝试删除引号:
unset($array[52]);
答案 1 :(得分: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上测试)
答案 2 :(得分:0)
您可以使用array_shift($array)
来消除数组中的第一个索引