多维数组展平

时间:2012-09-02 13:09:10

标签: php arrays multidimensional-array

我正在努力让这件事发生..也许有人知道我怎么能实现这个我只做了部分,我也在看php和drupal功能

enter image description here

这是完整图片

http://imageshack.us/photo/my-images/405/array.png/

filed_new(Arr ...)必须成为['value'] = string(11111)

1 个答案:

答案 0 :(得分:1)

您的输入数组:

...
0{
    data{
        nid => 1
        vid => 1
        type => article
        field_new{
            und{
                0{
                    value => 11111
                }
            }
        }
    }
}
1{
    data{
        nid => 2
        vid => 2
        type => article
        field_new{
            und{
                0{
                    value => 33333
                }
            }
        }
    }
}

您想要的数组:

...
0{
    data{
        nid => 1
        vid => 1
        type => article
        value => 11111
    }
}
1{
    data{
        nid => 2
        vid => 2
        type => article
        value => 33333
    }
}

Unset (http://php.net/manual/en/function.unset.php)是我们的朋友。

  

unset()会破坏指定的变量。

使用此代码:

<?php

$loop_count = count($input_arr);
for($i=0;$i<loop_count;$i++){
    //copy the value to the desired place
    $input_arr[$i]["data"]["value"] = $input_arr[$i]["data"]["field_new"]["und"][0]["value"];

    //delete the unwanted portion
    unset($input_arr[$i]["data"]["field_new"]);
} // for loops ENDS

?>

<强>假设:

  • 您的基本/父数组以数字方式编入索引。
  • 在每个数组中,field_new处于同一级别。

请添加您用来制作/获取此阵列的代码,我们可以为您提供更具体的答案。

相关问题