【发布时间】: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