操纵php数组或对象

时间:2014-07-19 02:36:11

标签: php arrays merge

我的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和其他数组函数,但没有什么能帮助我..

任何人都有我的功能或解决方案吗?

谢谢!

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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;
}