2014-07-19 86 views
1

我的php對象有問題。操縱php數組或對象

解釋: 我有這樣的:

array (size=7) 
'1stObject' => 
    array (size=3) 
     0 => 
     array (size=3) 
      'from' => string '1168498' (length=7) 
      'to' => string '0' (length=1) 
      'inner' => string '0' (length=1) 
     1 => 
     array (size=3) 
      'from' => string '0' (length=1) 
      'to' => string '2379217' (length=7) 
      'inner' => string '0' (length=1) 
     2 => 
     array (size=3) 
      'from' => string '0' (length=1) 
      'to' => string '0' (length=1) 
      'inner' => string '249111' (length=6) 

但我的問題是,我想這樣的:

array (size=7) 
    '1stObject' => 
     array (size=3) 
      'from' => string '1168498' (length=7) 
      'to' => string '2379217' (length=7) 
      'inner' => string '249111' (length=6) 

我怎樣才能做到這一點?

我array_merge測試,以及其他陣列功能,但沒有幫助我..

任何人都有一個功能或我一個解決方案?

謝謝!

+0

什麼是您的原始數據結構看起來像創建您現在的數組? – Robert

+0

所以你只需要'1stObject'數組的第一個孩子? –

回答

0

或者,你可以使用array_filter並清除不需要的。示例:Output

foreach($values as &$object) { 
    $temp = array(); 
    foreach($object as $key => $value) { 
     $value = array_filter($value); // clear zeroes 
     $temp[key($value)] = current($value); // transfer 
    } 
    $object = $temp; 
} 
0

我不知道你有什麼實際的陣列的完整結構,這可能會幫助你

foreach($yourarray['1stObject'] as $subarray) 
{ 
if($subarray['from']!=0) 
$newarray['1stObject']['from'] = $subarray['from']; 

if($subarray['to']!=0) 
$newarray['1stObject']['to'] = $subarray['to']; 

if($subarray['inner']!=0) 
$newarray['1stObject']['inner'] = $subarray['inner']; 
} 

print_r($newarray);