Jump to content

Merge arrays


lindm

Recommended Posts

Stuck on merging two arrays: Got the following to arrays:

 

ARRAY1

Array

(

[uB] => Array

([0] => Array

(

[1070] => 750000.00

[1079] => -750000.00

)

)

)

 

ARRAY2

Array

(

[uB] => Array

([-1] => Array

(

[1079] => -450000.00

)

)

)

 

Want them to merge to the following array (important as you can see that if a key is missing in one array but exists in the other it is created with the value 0)

 

CREATED ARRAY

Array

(

[uB] => Array

(

[1070] => Array

(

[0] => 750000.00

[-1] => 0.00

)

[1079] => Array

(

[0] => -750000.00

[-1] => -450000.00

)

)

)

Link to comment
Share on other sites

I'm sure there is probably a more efficient method, but this will work. I would give the varaibles more descriptive names as to what the values represent. But since I have no idea what the values are I used $key1, $key2, etc.

 

<?php
    
function custom_merge($inputArrays)
{
    $outputAry = array();
    $key2List = array();
    
    //Reverse keys 2 and 3
    foreach($inputArrays as $inputAry)
    {
        foreach($inputAry as $key1 => $data1) //key1 = UB
        {
            foreach($data1 as $key2 => $data2) //key2 = 0, -1
            {
                if(!in_array($key2, $key2List))
                {
                    $key2List[] = $key2;
                }
                foreach($data2 as $key3 => $value) //key3 = 1070, 1079
                {
                    $outputAry[$key1][$key3][$key2] = $value;
                }
            }
        }
    }
    
    //Insert 0 values where needed
    foreach($outputAry as $key1 => $data1)
    {
        foreach($data1 as $key3 => $data2)
        {
            foreach($key2List as $key2)
            {
                if(!isset($outputAry[$key1][$key3][$key2]))
                {
                    $outputAry[$key1][$key3][$key2] = 0;
                }
            }
        }
    }

    return $outputAry;
}
    
$ary1 = array(
    'UB' => array(
        0 => array(
            1070 => 750000.00,
            1079 => -750000.00
        )
    )
);
    
$ary2 = array(
    'UB' => array(
        -1 => array(
            1079 => -450000.00
        )
    )
);
    
echo "<pre>";
print_r(custom_merge(array($ary1, $ary2)));
echo "</pre>";
?>

 

Output:

Array
(
    [uB] => Array
        (
            [1070] => Array
                (
                    [0] => 750000
                    [-1] => 0
                )

            [1079] => Array
                (
                    [0] => -750000
                    [-1] => -450000
                )

        )

)

Link to comment
Share on other sites

Works perfect. Minor point. I missed that the two arrays to be merged are actually part of the same array. Could this be handled easily?

 

ARRAY

Array

(

[uB] => Array

  ([0] => Array

      (

      [1070] => 750000.00

      [1079] => -750000.00

      )

  )

  ([-1] => Array

      (

      [1079] => -450000.00

      )

  )

)

 

Should become

Array

(

    [uB] => Array

        (

            [1070] => Array

                (

                    [0] => 750000

                    [-1] => 0

                )

 

            [1079] => Array

                (

                    [0] => -750000

                    [-1] => -450000

                )

 

        )

 

)

Link to comment
Share on other sites

If you had taken the time to identify that in your first post instead of specifically stating you had two arrays you would have samed me a lot of time.

Stuck on merging two arrays: Got the following to [sic] arrays:

 

I love to help, but when I provide a solution to what someone asks and then the requestor changes the requirements it tells me the person does not appreciate the the time and effort we take to provide help. I'll make the assumption that it was an honest oversight - this time.

 

<?php

function custom_merge($inputAry)
{
    $outputAry = array();
    $key2List = array();
    
    foreach($inputAry as $key1 => $data1) //key1 = UB
    {
        $key2List = array_keys($data1);
        foreach($data1 as $key2 => $valuesAry) //key2 = 0, -1
        {
            foreach($valuesAry as $key3 => $value) //key3 = 1070, 1079
            {
                if(!isset($outputAry[$key1][$key3]))
                {
                    //Fill the $key3 subarray will all 0 values for all $key2 instances
                    $outputAry[$key1][$key3] = array_fill_keys($key2List, 0);
                }
                $outputAry[$key1][$key3][$key2] = $value;
            }
        }
    }
    return $outputAry;
}
    
$ary = array(
    'UB' => array(
        0 => array(
            1070 => 750000.00,
            1079 => -750000.00
        ),
        -1 => array(
            1079 => -450000.00
        )
    )
);
    
echo "<pre>";
print_r(custom_merge($ary));
echo "</pre>";
?>

Link to comment
Share on other sites

Understand your point and appreciate your time and suggested solution. Thought by only needing the answer for merging two arrays, I would handle the problem of the one array. Tried your solution and it seems to work well.

 

Many thanks!

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.