phparraysmultidimensional-array

Restructure a 2d array to segment row values into associatively keyed subarrays within the row


I am having some trouble, trying to figure out a way to restructure an array, and store it into a new one.

Input array:

$a = [
    [
        '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'
    ],
    [
        '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'
    ]
];

What I desire:

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
                  ) 
        )
    ...
)

This is what I've tried so far:

$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));
        }
        ...

I am not aware of what other possibilities there are. array_map() doesn't seem to do what I initially thought.


Solution

  • Just use a foreach and build your final array

    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);
    

    EDIT: added shorter version, without using intermediary variables

    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']
            )
        );
    }
    

    Will output something like this

    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
                    )
    
            )
    
    )