tags:

views:

157

answers:

2

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

A: 

It does like it would be easier to do at the query level. The SQL to do so would be something along the lines of:

SELECT * FROM my_main_table
    LEFT JOIN my_entry_table ON (my_main_table.entry_id = my_entry_table.entry_id)

Otherwise, if you really, really wanted to use PHP, then you'd want to do something along the lines of:

$modified_rows = array();
foreach ($first_rows as $row) {
    if (!is_null($row['entry_id']) && $row['entry_id'] == $second_array['entry_id']) {
        # merge/add colums from second array
        $modified_rows []= array_merge($row, $second_array);
    } else {
        # don't modify the original array
        $modified_rows []= $row;
    }
}

Where $first_rows is the unmodified first multidimensional array you mentioned, and $second_array is the second one.

amphetamachine
Thank you, looks like sql is the go, but unfortunately the query to get the first array is way over my head. When I add the join I get errors :(I'll persevere with the php method for now until I find a fix.Again, thank you.
Iain Urquhart
A: 

Maybe like this:

foreach($first_array as $first) {
  if($first['entry_id'] == $second_array['entry_id']) {
    array_merge($first, $second_array);
    break;
  }
}
gmunkhbaatarmn