Jump to content

[SOLVED] array merge problem


bpgillett

Recommended Posts

i'm trying to process 2 numerically indexed arrays, one of which is multidimensional, to result in an indexed array of associative arrays. the values from array #1 would ultimately be paired with any values contained in the nested array of corresponding numerical index from array #2.  nested arrays that are empty in array #2 would be ignored.

 

Array #1:

$array1 = Array ( [0] => Below Expectation [1] => Meets Expectation [2] => Above Expectation )

 

Array #2:

$array2 = Array ( [0] => Array ( [1] => pc [2] => p ) [1] => Array ( [1] => mk [2] => ics [3] => sbl ) [2] => Array ( )

 

Goal Array

$arrayFinal = Array ( [0] => Array ( ['pc'] => Below Expectation ['p'] => Below Expectation ) [1] => Array ( '[mk'] => Meets Expectation ['ics'] => Meets Expectation ['sbl'] => Meets Expectation ) [2] => Array ( )

 

i'm really pulling out my hair on this one as i can't even figure the conceptual approach; any help would be great.

 

thanks,

brian

Link to comment
Share on other sites

try

<?php
$array1 = array ( 
    0 => 'Below Expectation', 
    1 => 'Meets Expectation', 
    2 => 'Above Expectation' 
    );

$array2 = array ( 
    0 => array ( 1 => 'pc', 2 => 'p' ), 
    1 => array ( 1 => 'mk', 2 => 'ics', 3 => 'sbl' ), 
    2 => array ( )
    );
    
$result = array();    
foreach ($array2 as $k => $subarray) {
    foreach ($subarray as $item) {
        $result[$k][$item] = $array1[$k];
    }
}

echo '<pre>',  print_r($result,true), '</pre>';   
?>

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.