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

(array)返されたオブジェクトは、キャストを使用して実際には完全に配列に変換されていないようです。ただし、設定解除は数値インデックスにのみ影響します。

これを修正するには、の2番目のパラメーターを使用します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た。そして、とにかくその隠された5つにアクセスすることはできません。右?... 右?

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 に答える
1

引用符を削除してみてください:

unset($array[52]);

于 2012-12-21T14:08:08.613 に答える
0

を使用してarray_shift($array) 、配列から最初のインデックスを削除できます

于 2012-12-21T14:45:33.480 に答える