1

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.

8
  • Do it in a for each loop. Commented Dec 5, 2015 at 20:47
  • for-loop + foreach, don't you see above? Commented Dec 5, 2015 at 20:48
  • why are you nesting square brackets inside square brackets, and not something like [x][y]? Commented Dec 5, 2015 at 20:50
  • It's not square bracket, should I go by INDEX INDEX? Commented Dec 5, 2015 at 20:51
  • yes, that would work a lot better for nesting stuff. Commented Dec 5, 2015 at 20:52

2 Answers 2

1

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
                )

        )

)
Sign up to request clarification or add additional context in comments.

4 Comments

Alex, is this the most optimized way? Just curious.
I guess it can be shortened a bit, but don't expect any major increases of performance. Unless you have millions of iterations in the loop this should work just fine.
Another thing before you start thinking about optimization, make sure you know what and where you optimize, so you really need to measure first and then start optimizing.
I edited the answer to include the shorter version without intermediary variables
0

You can use functional iteration to directly return the new array: (Demo)

var_export(
    array_map(
        fn($row) => [
            'user' => [
                'user_id' => $row['user_id'],
                'fullname' => $row['fullname'],
                'photo' => $row['photo'],
                'link' => $row['link']
            ],
            'activity' => [
                'activity' => $row['activity']
            ],
            'article' => [
                'oid' => $row['oid'],
                'url' => $row['url'],
                'article_title' => $row['article_title']
            ]
        ],
        $array
    )
);

Or you can modify the input array directly (without copying the full array) to consume less memory: (Demo)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.