Jump to content

I need help Merging 2 different multidimensional associative arrays.


Recommended Posts

The first array is a result of database model query. The structure is a database model structure. 

 $array1 = Array
                (
                    [0] => Array
                        (
                            [name] => ID
                            [table] => DatabaseModelTable
                        )
                    [1] => Array
                        (
                            [name] => GuestListID
                            [table] => DatabaseModelTable
                        )
                    [2] => Array
                        (
                            [name] => Name
                            [table] => DatabaseModelTable
                        )
                    [3] => Array
                        (
                            [name] => UpdatedAt
                            [table] => DatabaseModelTable
                        )
                    [4] => Array
                        (
                            [name] => UpdaterPeopleID
                            [table] => DatabaseModelTable
                        )
                    [5] => Array
                        (
                            [name] => ConcRV
                            [table] => DatabaseModelTable
                        )
                )

The second array is a result of another select query. The structure of this array is like following

$array2 =  Array
		(
   			 [0] => Array
       					 (
           					[ID] => 1
            					[NumberOfTickets] => 21
        				)
    			[1] => Array
      					  (
        					[ID] => 2
            					[NumberOfTickets] => 12
					)
		)

The relation between two arrays s ID .  I want to merge these two array and make it one single array . The resulting array actually will add an extra column in the first array and look like the following

$result = Array
                (
                    [0] => Array
                        (
                            [name] => ID
                            [table] => DatabaseModelTable
                        )
                    [1] => Array
                        (
                            [name] => GuestListID
                            [table] => DatabaseModelTable
                        )
                    [2] => Array
                        (
                            [name] => Name
                            [table] => DatabaseModelTable
                        )
                    [3] => Array
                        (
                            [name] => UpdatedAt
                            [table] => DatabaseModelTable
                        )
                    [4] => Array
                        (
                            [name] => UpdaterPeopleID
                            [table] => DatabaseModelTable
                        )
                    [5] => Array
                        (
                            [name] => ConcRV
                            [table] => DatabaseModelTable
                        )
                    [6] => Array
                        (
                            [name] => NumOfTickets
                            [table] => DatabaseModelTable
                        )
                )

How can I do that. Please help me with code. I am really very bad with array and loops

Obvious question: why isn't the first query returning what you want, instead of you faking the data yourself?

Second question: How did you, as a human being, decide what to do with $array2 to get that merged version? Because I can't see how [ID=1,NumberOfTickets=21] [ID=2,NumberOfTickets=12] translates into [name=NumOfTickets,table=DatabaseModelTable].

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.