'PHP map array1 keys to array 2 indexes?

I'm trying to take the results of one array and replace them in a different array when the indexes don't match up. I've tried all the usual stuff and can't seem to figure it out.

$keysArray = array("Header 1", "Header 2", "Header 3", "Header 4", "Header 5", "Header 6", "Header 7", "Header 8");
$resultsArray = array("Result 1", "Result 2", "Result 3", "Result 4", "Result 5");
[Keys] => Array
        (
            [0] => 
            [1] => 0
            [2] => 
            [3] => 1
            [4] => 4
            [5] => 
            [6] => 
            [7] => 2
        )

[Results] => Array
        (
            [0] => Array
                (
                    [0] => Result 1 (Belongs in Keys Array [1])
                    [1] => Result 2 (Belongs in Keys Array [3])
                    [2] => Result 3 (Belongs in Keys Array [7])
                    [3] => Result 4 (Not in Keys)
                    [4] => Result 5 (Belongs in Keys Array [4])
                    [5] => Result 6 (Not in Keys)
                    [6] => Result 7 (Not in Keys)
                    [7] => Result 8 (Not in keys)
                 )
        )

Code to this point goes something like...

$result = array_intersect($keysArray, $resultsArray);
foreach($result as $k=>$r) {
    if($r == "Result 1") {
        $result1 = $r;
    }
    if($r == "Result 2") {
        $result2 = $r;
    }
        // etc., etc., etc.
}

"Keys" array is set in a specific order (to match DB table). What I'm trying to do is take data from other sources and map it to the correct order. The "Results" array is one example of the source data. Kind of like parsing tabular data and putting it into the correct field.



Solution 1:[1]

I figured it out. My [Keys] array is fixed in format and count limits and my [Results] array was not, so the [Results] wouldn't necessarily line up. For my above example, [Keys] => [1] is equiavlent to [Results] => [0] so I used bindParam to affix the result to the proper key depending on where it fell in my [Keys] array. Now, when I print/echo/etc., it displays in the correct location.

$stmt->bindParam( 1, $result1);
$stmt->bindParam( 2, $result2);
$stmt->bindParam( 3, $result3);
$stmt->bindParam( 4, $result4);
$stmt->bindParam( 5, $result5);

Thank you to those who responded. I know it wasn't easy to decipher what I was trying to do.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 B Barry