Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 716513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:18:28+00:00 2026-05-14T05:18:28+00:00

Excuse me if this has indeed been asked before, I couldn’t see anything that

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:18:28+00:00Added an answer on May 14, 2026 at 5:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Excuse me if this is a question that has been asked before, if so
I feel like this question has already been asked and answered, yet I couldn't
Excuse me if this has already been asked, but searching for coalesce brings up
This has been asked before, and will be asked again! I am currently building
I could imagine this question has already been asked, but I actually could not
This question has been brought up before, and I have searched many of the
I have been trying to figure out how to do this and it seems
I'm looking for an excuse to learn Django for a new project that has
Excuse me if this is the wrong place to ask. I have been using
I am a patterns newbie so please excuse this question if it sounds too

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.