I have 3 arrays. These inter-related. I want to combine these statements.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$merge = array();
foreach($posts_user as $data){
foreach ($posts as $post){
if($data['post_id'] == $post['id'] ){
foreach ($users as $user){
if($data['user_id'] == $user['id']){
$post['user'] = $user;
$merge['post'][] = $post;
}
}
}
}
}
print_r($merge);
I want to be as follows. How can I do it?
I want results.
$merge = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3',
'user' => array (
'id' => '2',
'name' => 'Selena'
),
),
);
Is there another alternative? For example; How do I do with them?
array_walk_recursive(), ArrayIterator(), RecursiveArrayIterator()
I tried to do.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$newuser = array();
foreach($post_user as $data){
foreach ($user as $users){
if($data['user_id'] == $users['id']){
$newuser[] = $users;
}
}
}
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($post),1);
$mi->attachIterator(new ArrayIterator($newuser),'user');
$newArray = array();
foreach($mi as $details) {
$newArray[] = $details;
}
print_r($newArray);
Results
Array
(
[0] => Array
(
[1] => Array
(
[id] => 1
[title] => Title 1
[content] => Content 1
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[1] => Array
(
[1] => Array
(
[id] => 2
[title] => Title 2
[content] => Content 2
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[2] => Array
(
[1] => Array
(
[id] => 3
[title] => Title 3
[content] => Content 3
)
[user] => Array
(
[id] => 2
[name] => Selena
)
)
)
Related
Here is an array. Once a new element comes in with parent_uuid, I need to add that to the corresponding position, that is to the children of the item which has uuid value as parent_uuid value. The children then can have other children and if that is specified, I need to insert it to the particular parent. I think for this I need to search to the multidimensional array with the parent_uuid value. How can I do this and insert in PHP?
Array
(
[0] => Array
(
[id] => 1
[uuid] => ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9
[parent_uuid] =>
[name] => First Parent
[children] => Array
(
)
)
[1] => Array
(
[id] => 2
[uuid] => 74bd4b37-6a20-4579-99a3-ce56b0bc28a7
[parent_uuid] =>
[name] => Second Parent
[children] => Array
(
[0] => Array
(
[id] => 3
[uuid] => f87c6d5c-93ec-40bf-a04d-c925dd1e0aca
[parent_uuid] => 74bd4b37-6a20-4579-99a3-ce56b0bc28a7
[name] => First Child
[children] => Array
(
)
)
[1] => Array
(
[id] => 4
[uuid] => cb2b3d9d-867c-40a0-9254-05b466859db1
[parent_uuid] => 74bd4b37-6a20-4579-99a3-ce56b0bc28a7
[name] => Second Child
[children] => Array
(
)
)
)
)
)
I think you need some kind of recursive function, here is my messy example.
<?php
header('Content-type: text/plain');
$data = array (
0 =>
array (
'id' => 1,
'uuid' => 'ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9',
'parent_uuid' => '',
'name' => 'First Parent',
'children' =>
array (
),
),
1 =>
array (
'id' => 2,
'uuid' => '74bd4b37-6a20-4579-99a3-ce56b0bc28a7',
'parent_uuid' => '',
'name' => 'Second Parent',
'children' =>
array (
0 =>
array (
'id' => 3,
'uuid' => 'f87c6d5c-93ec-40bf-a04d-c925dd1e0aca',
'parent_uuid' => '74bd4b37-6a20-4579-99a3-ce56b0bc28a7',
'name' => 'First Child',
'children' =>
array (
),
),
1 =>
array (
'id' => 4,
'uuid' => 'cb2b3d9d-867c-40a0-9254-05b466859db1',
'parent_uuid' => '74bd4b37-6a20-4579-99a3-ce56b0bc28a7',
'name' => 'Second Child',
'children' =>
array (
),
),
),
),
);
function arrayAddChild(&$data, $child) {
if (!isset($data) || !is_array($data) || empty($data)) {
return false;
}
foreach ($data as $key => $value) {
if ($value['uuid'] === $child['parent_uuid']) {
$data[$key]['children'][] = $child;
return true;
}
if(arrayAddChild($data[$key]['children'], $child)) {
return true;
}
}
return false;
}
var_export(arrayAddChild($data, [
'id' => 31,
'uuid' => '31',
'parent_uuid' => 'cb2b3d9d-867c-40a0-9254-05b466859db1',
'name' => 'Second Child',
'children' => []
]
));
var_export(arrayAddChild($data, [
'id' => 32,
'uuid' => '32',
'parent_uuid' => '31',
'name' => 'Second Child',
'children' => []
]
));
var_export(arrayAddChild($data, [
'id' => 33,
'uuid' => '33',
'parent_uuid' => '32',
'name' => 'Second Child',
'children' => []
]
));
var_export(arrayAddChild($data, [
'id' => 34,
'uuid' => '34',
'parent_uuid' => '33',
'name' => 'Second Child',
'children' => []
]
));
var_export(arrayAddChild($data, [
'id' => 35,
'uuid' => '35',
'parent_uuid' => '34',
'name' => 'Second Child',
'children' => []
]
));
var_export(arrayAddChild($data, [
'id' => 36,
'uuid' => '36',
'parent_uuid' => '35',
'name' => 'Second Child',
'children' => []
]
));
var_export($data);
this is the structure you need
$Array["ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9"]['name'] = "First Parent";
$Array["ef4b72ae-012a-4b2c-88b2-d4bf8726fcb9"]['children'] = [];
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['name'] = "Second Parent";
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["f87c6d5c-93ec-40bf-a04d-c925dd1e0aca"]['name'] = "First Child";
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["f87c6d5c-93ec-40bf-a04d-c925dd1e0aca"]['children'] = [];
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["cb2b3d9d-867c-40a0-9254-05b466859db1"]['name'] = "Second Child";
$Array["74bd4b37-6a20-4579-99a3-ce56b0bc28a7"]['children']["cb2b3d9d-867c-40a0-9254-05b466859db1"]['children'] = [];
and this is if you really need 'name' or any complementary info you need to store with each item. If it is just about a tree structure of uid, get rid of 'name' and 'children' keys
have not found a standard php function to recursively search for a given key (anyone ?)
so here is the function you need
function insertItem($newItem,$uidParent,$array) {
foreach ($array as $uid => $content) {
if ($uid == $uidParent) { // parent found, item insert
$array[$uid]['children'][$newItem['uid']]['name'] = $newItem['name'];
$array[$uid]['children'][$newItem['uid']]['children'] = [];
} elseif (!empty($content['children'])) { // recursively search the tree
$array[$uid]['children'] = insertItem($newItem,$uidParent,$content['children']);
}
}
return $array;
}
$newItem['name'] = "new item";
$newItem['uid'] = "f87c6d5c-93ec-40bf-a04d-c925dd1e0aca";
$uidParent = "cb2b3d9d-867c-40a0-9254-05b466859db1";
$Array = insertItem($newItem,$uidParent,$Array);
sandbox here
I have an indexed array with nested categories like this:
array (
0 =>
array (
'name' => 'Furniture',
'id' => 'b3cdd1k',
'content' =>
array (
0 =>
array (
'name' => 'Tables',
'id' => 'nw24ga3',
'content' =>
array (
0 =>
array (
'name' => 'Wooden tables',
'id' => 'ba5lgaz',
),
1 =>
array (
'name' => 'Glass tables',
'id' => 'rqt91gz',
),
),
),
),
),
1 =>
array (
'name' => 'Lamps',
'id' => 'vb1a4nf',
),
2 =>
array (
'name' => 'Doors',
'id' => 'a5l4gal',
'content' =>
array (
0 =>
array (
'name' => 'Entrance doors',
'id' => 'qwg30fb',
),
),
),
)
Is there elegant way to convert it to associative array (where keys are id's) and keep nesting structure?
After conversion I excepting something like this:
array (
'b3cdd1k' =>
array (
'name' => 'Furniture',
'content' =>
array (
'nw24ga3' =>
array (
'name' => 'Tables',
'content' =>
array (
'ba5lgaz' =>
array (
'name' => 'Wooden tables',
),
'rqt91gz' =>
array (
'name' => 'Glass tables',
),
),
),
),
),
'vb1a4nf' =>
array (
'name' => 'Lamps',
),
'a5l4gal' =>
array (
'name' => 'Doors',
'content' =>
array (
'qwg30fb' =>
array (
'name' => 'Entrance doors',
),
),
),
)
You could try this - not the most elegant, but seems to work:
function convert(&$a)
{
$result = Array();
foreach($a as $k=>&$v)
{
$result[$v['id']]['name'] = $v['name'];
if(is_array($v['content'])) $result[$v['id']]['content'] = convert($v['content']);
}
return $result;
}
if(count($array) != 0) $result = convert($array);
This is my array:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
I've to convert this array into multi-dimentional array as below:
$arr = array(
'jan-2015' => array(
0 => array(
'title' => 'test1',
'count' => 4,
),
1 => array(
'title' => 'test2',
'count' => 10,
),
),
'jun-2015' => array(
0 => array(
'title' => 'test3',
'count' => 14,
),
),
'july-2015' => array(
0 => array(
'title' => 'test4',
'count' => 45,
),
),
);
I've tried to make it as expected but unfortunately i can't make this.
Is any other solutions for this?
According to your data structure :
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
try this:
$newArray = array();
foreach($arr as $key => $val) {
$newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
[month] => jan-2015
)
[1] => Array
(
[title] => test2
[count] => 10
[month] => jan-2015
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
[month] => jun-2015
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
[month] => july-2015
)
)
)
You could use this function:
function transform($input) {
// Extract months, and use them as keys, with value set to empty array
// The array_fill_keys also removes duilicates
$output = array_fill_keys(array_column($input, 'month'), array());
foreach ($input as $element) {
$copy = $element;
// remove the month key
unset($copy["month"]);
// assign this to the month key in the output
$output[$element["month"]][] = $copy;
}
return $output;
}
Call it like this:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
print_r (transform($arr));
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
)
[1] => Array
(
[title] => test2
[count] => 10
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
)
)
)
By using answer of #Girish Patidar, You can achieve this by:
$outputArr = array();
$to_skip = array();
foreach($arr as $row){
$to_skip = $row;
unset($to_skip['month']);
$outputArr[$row['month']][] = $to_skip;
}
echo "<pre>";
print_r($outputArr);
die;
There could many way to do this. Please try this one if it works for you
<?php
$newArr=NULL;
foreach($arr as $array)
{
$temp=NULL;
$temp['title']=$array['title'];
$temp['count']=$array['count'];
$newArr[$array['month']][]=$temp;
}
var_dump($newArr);
?>
Hello guys I am stuck for this kind of merging of arrays:
Sample Array
Array(
[0] => Array(
'id' => '1',
'task' => 'Task 1.0'
),
[1] => Array(
'id' => '1',
'task' => 'Task 1.1'
),
[2] => Array(
'id' => '2',
'task' => 'Task 2.0'
),
[3] => Array(
'id' => '2',
'task' => 'Task 2.1'
)
)
Expected Result
Array(
[0] => Array(
'id' => '1',
'task' => array(
[0] => 'Task 1.0',
[1] => 'Task 1.1'
)
),
[1] => Array(
'id' => '2',
'task' => array(
[0] => 'Task 2.0',
[1] => 'Task 2.1'
)
)
)
How can I do this kind of merging?
Thanks in advance.
this might not be the best solution, but i would consider it as an aproach:
$oldArray = array (
0 => array(
'id' => '1',
'task' => 'Task 1.0'
),
1 => array(
'id' => '1',
'task' => 'Task 1.1'
),
2 => array(
'id' => '2',
'task' => 'Task 2.0'
),
3 => array(
'id' => '2',
'task' => 'Task 2.1'
)
);
$newArray = array();
foreach( $oldArray as $array ) {
if( !isset( $newArray[$array["id"]] ) ) {
$newArray[$array["id"]] = array( "id" => $array["id"] );
}
$newArray[$array["id"]]["task"][] = $array["task"];
}
// reset the temp keys
$newArray = array_values( $newArray );
Edited: forgot "tasks" in $newArray[$array["id"]]["task"][] = $array["task"];, made an edit again
I have an array of variable size structured like this (categories is only one of the keys inside data):
print_r($json[123]["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
)
print_r($json[456]["data"]["categories"]);
array(
array(
'id' => '21',
'description' => 'Downloadable Content'
),
array(
'id' => '1',
'description' => 'Multi-player'
)
)
Now, I want to merge these sub-arrays (they can be in variable number) and have all keys added and replaced. I've tried array_merge but it replaces the keys without adding new ones.
In this case I need to obtain this array:
print_r($merged["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
),
array(
'id' => '21',
'description' => 'Downloadable Content'
)
)
Any help?
Edit:
I think I didn't expressed myself well enough. $json[$id]["data"] has multiple keys I want to merge (categories is just an example). Also the number of $json[$id] keys is variable
Edit2:
The arrays can have duplicate values, and the depth of the keys can be variable. I need to get something like array_merge_recursive() but with same values replaced.
Edit3:
This is the current array. http://pastebin.com/7x7KaAVM I need to merge all keys that have sub-arrays
Try below code:
$json = array(
'123' => array('data' => array('categories' => array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
))
),
'456' => array('data' => array('categories' => array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
))
),
);
//print_r($json);
$merged = array();
foreach($json as $j1)
{
foreach($j1 as $j2)
{
foreach($j2 as $key => $j3)
{
foreach($j3 as $j4)
{
$merged[$key][] = $j4;
}
}
}
}
print_r($merged);
Result:
Array
(
[categories] => Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
)
)
Demo:
http://3v4l.org/X61bE#v430
Try this . To generalize I have added some more arrays.
<?php
$merged_array = array();
$final_array = array();
$json[123]["data"]["categories"] = array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
);
$json[456]["data"]["categories"] = array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
);
$json[786]["data"]["categories"] = array(
array(
'id' => '31',
'description' => 'Downloadable Content'
)
);
$json[058]["data"]["categories"] = array(
array(
'id' => '41',
'description' => 'Downloadable Content'
)
);
foreach($json as $key=>$value){
array_push($merged_array,$json[$key]["data"]["categories"]);
}
foreach($merged_array as $value){
foreach($value as $val){
array_push($final_array,$val);
}
}
print_r($final_array);
?>
RESULT
Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
[6] => Array
(
[id] => 31
[description] => Downloadable Content
)
[7] => Array
(
[id] => 41
[description] => Downloadable Content
)
)