Excuse me if this has indeed been asked before, I couldn't see anything that fitted my needs out of the dozens of similar titled posts out there ;)
I'm trying to merge 2 php arrays which aren't of the same length, and merge them on a value that exists from identical key => values within both arrays.
My first query produces an array from a nested set:
array
(
1 => array
(
'node_id' => 1,
'lft' => 1,
'rgt' => 4,
'moved' => 0,
'label' => 'Home',
'entry_id' => 1,
'template_path' => '',
'custom_url' => '/',
'extra' => '',
'childs' => 1,
'level' => 0,
'lower' => 0,
'upper' => 0
),
2 => array
(
'node_id' => 2,
'lft' => 2,
'rgt' => 3,
'moved' => 0,
'label' => 'Home',
'entry_id' => NULL,
'template_path' => '',
'custom_url' => 'http://google.com/',
'extra' => '',
'childs' => 0,
'level' => 1,
'lower' => 0,
'upper' => 0
)
);
My second array returns some additional key/values I'd like to insert to the above array:
array
(
'entry_id' => 1,
'entry_title' => 'This is my title',
);
I want to merge both of the arrays inserting the additional information into those that match on the key 'entry_id', as well as keeping the sub arrays which don't match.
So, by combining the two arrays, I'd end up with
array
(
1 => array
(
'node_id' => 1,
'lft' => 1,
'rgt' => 4,
'moved' => 0,
'label' => 'Home',
'entry_id' => 1,
'template_path' => '',
'custom_url' => '/',
'extra' => '',
'childs' => 1,
'level' => 0,
'lower' => 0,
'upper' => 0,
'entry_title' => 'This is my title'
),
2 => array
(
'node_id' => 2,
'lft' => 2,
'rgt' => 3,
'moved' => 0,
'label' => 'Home',
'entry_id' => NULL,
'template_path' => '',
'custom_url' => 'http://google.com/',
'extra' => '',
'childs' => 0,
'level' => 1,
'lower' => 0,
'upper' => 0,
'entry_title' => NULL
)
);
Actually, writing this out makes me think I should do it via sql...
UPDATE: I'm trying to update the query, but getting errors that I can't figure out how to fix on the join :(
SELECT `n`.*, round((`n`.`rgt` - `n`.`lft` - 1) / 2, 0) AS childs,
count(*) - 1 + (`n`.`lft` > 1) + 1 AS level,
((min(`p`.`rgt`) - `n`.`rgt` - (`n`.`lft` > 1)) / 2) > 0 AS lower,
(((`n`.`lft` - max(`p`.`lft`) > 1))) AS upper
FROM `exp_node_tree_6` `n`, `exp_node_tree_6` `p`
LEFT JOIN `exp_channel_titles` ON (`exp_node_tree_6.entry_id`=`exp_channel_titles.entry_id`)
WHERE `n`.`lft`
BETWEEN `p`.`lft`
AND `p`.`rgt`
AND ( `p`.`node_id` != `n`.`node_id` OR `n`.`lft` = 1 )
GROUP BY `n`.`node_id`
ORDER BY `n`.`lft`
Sorry, I'm quite new to sql... and even if I do get this right, will this not only return those that have entry_id matches on both tables?
I need to get all the data from exp_node_tree_6 table, and be able to dip into the other if the entry_id exists...