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
https://forums.phpfreaks.com/topic/47476-solved-array-merge-problem/
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>';   
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.