数组重构PHP

时间:2015-12-05 20:46:23

标签: php

我遇到了一些麻烦,试图想出一种重组阵列的方法,并将其存储到新阵列中。

这是我的输出:

Array
(
    [0] => Array
        (
            [user_id] => 0
            [activity] => edited
            [oid] => 62487513549577588
            [article_title] => What if Universal Solutions existed?
            [url] => http://127.0.0.1/article.php?id=62487513549577588
            [fullname] => Peter Anderson
            [photo] => http://127.0.0.1/uploads/0/147885940.png
            [link] => http://127.0.0.1/peter.anderson
        )

    [1] => Array
        (
            [user_id] => 0
            [activity] => edited
            [oid] => 776286559146635
            [article_title] => Mathematics 101: Introduction
            [url] => http://127.0.0.1/article.php?id=776286559146635
            [fullname] => Peter Anderson
            [photo] => http://127.0.0.1/uploads/0/147885940.png
            [link] => http://127.0.0.1/peter.anderson
        )

)

我想要的是什么:

Array
(
    [0] => Array
        (
            [user] => Array 
                    (
                        [user_id] => 0
                        [fullname] => Peter Anderson
                        [photo] => http://127.0.0.1/uploads/0/147885940.png
                        [link] => http://127.0.0.1/peter.anderson
                    )
            [activity] => Array
                     (
                        [activity] => edited
                     )
            [article] => Array
                  (
                    [oid] => 776286559146635
                    [url] => http://127.0.0.1/article.php?id=776286559146635
                    [article_title] => Mathematics 101: Introduction
                  ) 
        )
)

这是我到目前为止所尝试的内容:

$keys = array_keys($a); 
        for($i = 0; $i < count($a); $i++){
            foreach($a[$keys[$i]] as $key => $value){

                if($key == "action"){
                    $newArr[$i] = array("action" => array($key => $value));
                }
....

我不是其他可能的东西,array_map();不做我最初的想法。

1 个答案:

答案 0 :(得分:1)

只需使用foreach并构建最终数组

foreach($items as $item){
    $user['user_id'] = $item['user_id'];
    $user['fullname'] = $item['fullname'];
    $user['photo'] = $item['photo'];
    $user['link'] = $item['link'];

    $activity['activity'] = $item['activity'];

    $article['oid'] = $item['oid'];
    $article['url'] = $item['url'];
    $article['article_title'] = $item['article_title'];

    $result[] = array(
        'user' => $user,
        'activity' => $activity,
        'article' => $article
        );
}

print_r($result);

编辑:添加更短版本,不使用中间变量

foreach($items as $item){
    $result[] = array(
        'user' => array(
            'user_id' => $item['user_id'],
            'fullname' => $item['fullname'],
            'photo' => $item['photo'],
            'link' => $item['link']
        ),
        'activity' => array(
            'activity' => $item['activity']
        ),
        'article' => array(
            'oid' => $item['oid'],
            'url' => $item['url'],
            'article_title' => $item['article_title']
        )
    );
}

会输出类似这样的内容

Array
(
    [0] => Array
        (
            [user] => Array
                (
                    [user_id] => 0
                    [fullname] => Peter Anderson
                    [photo] => http://127.0.0.1/uploads/0/147885940.png
                    [link] => http://127.0.0.1/peter.anderson
                )

            [activity] => Array
                (
                    [activity] => edited
                )

            [article] => Array
                (
                    [oid] => 62487513549577588
                    [url] => http://127.0.0.1/article.php?id=62487513549577588
                    [article_title] => What if Universal Solutions existed?
                )

        )

    [1] => Array
        (
            [user] => Array
                (
                    [user_id] => 0
                    [fullname] => Peter Anderson
                    [photo] => http://127.0.0.1/uploads/0/147885940.png
                    [link] => http://127.0.0.1/peter.anderson
                )

            [activity] => Array
                (
                    [activity] => edited
                )

            [article] => Array
                (
                    [oid] => 776286559146635
                    [url] => http://127.0.0.1/article.php?id=776286559146635
                    [article_title] => Mathematics 101: Introduction
                )

        )

)