Jump to content

combine two arrays


dsbpac

Recommended Posts

I have two arrays that are listed like this.

Array1
(
    [0] => Array
        (
            [32347] => 8
            [22188] => 3
            [37493] => 4
            [37201] => 7

        )

)
Array2
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 32347
                    [1] => 28470
                    [2] => 35319
                    [3] => 37493
                   
                )

            [1] => Array
                (
                    [0] => 5
                    [1] => 2
                    [2] => 
                    [3] => 4


                )

            [2] => Array
                (
                    [0] => 484
                    [1] => 383
                    [2] => 315
                    [3] => 320

                )

        )

)

I want to combine both arrays if value is the same.

 

Example

[32347] => 8 => 5 => 484

Thank you in advance.

Link to comment
Share on other sites

Have you looked at all of the array-related functions in the php manual? An excellent resource - you should bookmark it.

 

Of course you need to look at the structure of your arrays closer. One is a 2-deep array and the other is 3-deep. What exactly do you want to merge and what kind of an array do you want to end up. Plus - you don't have like values between the arrays. One has rather unique values as the keys and the other has them as values.

Edited by ginerjm
Link to comment
Share on other sites

Theoretically, yes, you can combine arrays in nested foreach() loops, but I'm not sure I understand what your end goal is, which makes it difficult to advise how one would possibly go about doing that. What's the correlation between the array values? Right now it seems utterly random.

Link to comment
Share on other sites

Yes! One seems to have real "value" oriented data as the values while the other seems to use those "values" as keys. Pluse your one array has arrays an extra layer deeper and neither of your two arrays identify the different child arrays separate from the others.

 

Why such a blind structure? And if this is the entire set of data, you could have manually created the desired result by now.

Edited by ginerjm
Link to comment
Share on other sites

Array1 as you call it is simply an element of a bigger array. What is the correlation between the named 'array1' element and the un-named element that is the sibling to it? I don't see it.

 

You do realize that you have a multiple level array with elements ( the [0] and the [1] ) that have no identifiers to help any program to recognize what it is dealing with. As for merging the latest post with the second(?) array you have in your original post, how is the script suppose to connect those completely separate arrays?

 

From your original post again - you have array1 that has one element. Period. Then you have array2 that has one element. In each of them you have arrays as the values of those single elements, except array1 has only one array under the child, while array2 has 3 arrays under its child. What is the purpose behind this? They don't have names so why are they are not simply all elements of the topmost array?

Edited by ginerjm
Link to comment
Share on other sites

What is the source of these arrays? Are you retrieving data from a database? If so, there will definitely be a better way to combine the data than combining arrays. Also, as others have said, the data has no context. In your example of:

 

[32347] => 8 => 5 => 484

 

What does the 8, 5 and 484 represent? Besides, your example seems to indicate that you want each value to be a sub array of the preceding value. Perhaps it should look more like this

 

[32347] => (8, 5, 484)

 

Or this:

 

[32347] => (

    'time' =>8,

    'amount' =>5,

    'foo' => 484

)

 

 

With your original data, here is a possible solution

<?php
$array1 = array(32347 => 8, 22188 => 3, 37493 => 4, 37201 => 7);
 
$array2 = array(
    array(
        array(32347, 28470, 35319, 37493),
        array(5, 2, '', 4),
        array(484, 383, 315, 320)
    )
);
 
$newArray = array();
foreach($array1 as $key => $value)
{
    $newArray[$key][] = $value;
    foreach($array2 as $subArray)
    {
        //extract the first array index (with the keys)
        $keyArray = array_shift($subArray);
        //Get the index for the key - if exists
        $keyIndex = array_search($key, $keyArray);
        //If the key exists, get the other values in the sub array
        if($keyIndex !== false)
        {
            foreach($subArray as $valuesArray)
            {
                if(isset($valuesArray[$keyIndex]))
                {
                    $newArray[$key][] = $valuesArray[$keyIndex];
                }
            }
        }
    }
}
 
echo "<pre>" . print_r($newArray, 1) . "</pre>";
?>

Output

 

Array
(
    [32347] => Array
        (
            [0] => 8
            [1] => 5
            [2] => 484
        )
 
    [22188] => Array
        (
            [0] => 3
        )
 
    [37493] => Array
        (
            [0] => 4
            [1] => 4
            [2] => 320
        )
 
    [37201] => Array
        (
            [0] => 7
        )
 
)
Edited by Psycho
Link to comment
Share on other sites

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.