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.