19

i have two array

/**
 * Menu Navigation
 * @var array
 */
public $nav_top = array(
    100 => 'Dashboard',
    200 => 'Sell',
    300 => 'Products',
    400 => 'History',
    500 => 'Customers',
    600 => 'Setup'
);

/**
 * Menu Navigation
 * @var array
 */
public $nav_sub = array(
    201 => 'Current Sale',
    202 => 'Retrieve Sale',
    203 => 'Close Register',

    301 => 'Product',
    302 => 'Stock Control',
    303 => 'Price Books',
    304 => 'Types',
    305 => 'Suppliers',
    306 => 'Brands',
    307 => 'Tags',

    501 => 'Customer',
    502 => 'Group'
);

How to combine this two array without losing it's key index?

if i do it with array_merge() the index will restart from zero

$nav = array_merge($Class->nav_top, $Class->nav_sub);
var_dump($nav);

# Output:
array(
    0 => 'Current Sale',
    1 => 'Retrieve Sale',
    2 => 'Close Register',
    .......
);

expected result the array key still same

# Expected Output
array(
    100 => 'Dashboard',
    200 => 'Sell',
    300 => 'Products',
    ........
);

2 Answers 2

66

The easiest I can think of:

$combined = $nav_top + $nav_sub;

An example.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 i never knew + can work on array .. are you sure its not a bug ... lol
@Baba, not a bug at all and has been documented for around a decade.
Note that the order is important. with array_merge($a,$b), keys in $b will override matching keys in $a. Where with $a+$b, keys in $b will NOT override matching keys in $a, but they will be ignored. It's explained in this example: php.net/manual/en/function.array-merge.php#example-5418
Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC BY-SA 4.0 license). By SE policy, any vandalism will be reverted.
1

MY advice

Use the solution from jeroen's answer

Hard way out

$combined  = merge($nav_top,$nav_sub);

Function

function merge($arr1,$arr2)
{
    if(!is_array($arr1))
        $arr1 = array();
    if(!is_array($arr2))
        $arr2 = array();
    $keys1 = array_keys($arr1);
    $keys2 = array_keys($arr2);
    $keys  = array_merge($keys1,$keys2);
    $vals1 = array_values($arr1);
    $vals2 = array_values($arr2);
    $vals  = array_merge($vals1,$vals2);
    $ret    = array();

    foreach($keys as $key)
    {
        list($unused,$val) = each($vals);
        $ret[$key] = $val;
    }

    return $ret;
}

Or

function merge($a1, $a2) {
                           
    $aRes = $a1;
    foreach ( array_slice ( func_get_args (), 1 ) as $aRay ) {
        foreach ( array_intersect_key ( $aRay, $aRes ) as $key => $val )
            $aRes [$key] += $val;
        $aRes += $aRay;
    }
    return $aRes;
}

Demo : http://codepad.org/9B8V96Gf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.