2013-05-07 5 views
5

私は以下のような配列(配列1)を持っています。配列の下のようにstdClassを削除する必要があります。 2.現在、私はforeachループを使ってやっていますが、ループを繰り返すより良い方法がありますか?配列からstdClassオブジェクトを削除する

アレイ1番

array(3) { 
    [0] => object(stdClass)#169 (4) { 
    ["id"] => string(2) "59" 
    ["name"] => string(13) "test" 
    ["email"] => string(21) "[email protected]" 
    ["telephone"] => string(20) "898998989" 
    } 
    [1] => object(stdClass)#190 (4) { 
    ["id"] => string(2) "58" 
    ["name"] => string(13) "test" 
    ["email"] => string(21) "[email protected]" 
    ["telephone"] => string(8) "71877858" 
    } 
    [2] => object(stdClass)#193 (4) { 
    ["id"] => string(2) "34" 
    ["name"] => string(9) "test" 
    ["email"] => string(22) "[email protected]" 
    ["telephone"] => string(13) "3189028092139" 
    } 
} 

配列番号2

array(3) { 
    [0] => array(4) { 
    ["id"] => string(2) "62" 
    ["name"] => string(5) "test" 
    ["email"] => string(22) "[email protected]" 
    ["telephone"] => string(10) "898998989" 
    } 
    [1] => array(4) { 
    ["id"] => string(2) "59" 
    ["name"] => string(13) "test" 
    ["email"] => string(21) "[email protected]" 
    ["telephone"] => string(20) "71877858" 
    } 
    [2] => array(4) { 
    ["id"] => string(2) "58" 
    ["name"] => string(13) "test" 
    ["email"] => string(21) "[email protected]" 
    ["telephone"] => string(8) "3189028092139" 
    } 
} 

は、これは私が何をすべきかです(鋳造)

foreach($moderationContacts as $contact) 
{ 
    $contacts[] = (array)$contact; 
} 
+0

なぜそれを削除しますか? – Bananam00n

+0

ループせずに配列を操作することはできません。どのような解決策でも何らかの形式の暗黙的なループが必要になります。 – deceze

+0

@ Bananam00n - json_encodeを使用してJSONオブジェクトを作成する必要があります – Shaolin

答えて

7

からであればしてみてください

$array = json_decode(json_encode($array), true); 

EDIT: 私はこのケースをテストしてみた、それが動作します:

$stdClass= new stdClass(); 
$stdClass->test = "foo"; 
$array = Array(
    "a" => Array("b","c"), 
    "d" => $stdClass 
); 

$array = json_decode(json_encode($array), true); 

var_dump($array); 

OUTPUT

array 
    'a' => 
    array 
     0 => string 'b' (length=1) 
     1 => string 'c' (length=1) 
    'd' => 
    array 
     'test' => string 'foo' (length=3) 
2

あなたは

を試すことができます
$array = array_map(function ($v) { 
    return (array) $v ; // convert to array 
}, $array); 

またはこのデータはjson使用

$array = json_decode($data,true); 
関連する問題