在两个php数组中找到匹配的元素,然后将两个数组元素一起列出

时间:2012-08-29 21:47:59

标签: php arrays

Array
(
    [0] => Array
        (
            [0] => "Product Category"
            [1] => Product Name
            [2] => Product ID
            [3] => Average Item Price
            [4] => Item Sales
            [5] => Items Sold
            [6] => Product Conversion (Sold/Views)
            [7] => Item Abandonment Rate
        )
    [1] => Array
        (
            [0] => OVERALL
            [1] => -
            [2] => -
            [3] => $51.17
            [4] => $1335974.77
            [5] => 26111
            [6] => 16.25%
            [7] => 42.06%
        )



ARRAY B:
Array
(
    [0] => Array
        (
            [0] => "Content Category"
            [1] => Page Name
            [2] => Page Views
            [3] => Sessions
            [4] => Orders / Session
            [5] => Sales
            [6] => Bounce Rate
            [7] => Page Views / Session
        )

    [1] => Array
        (
            [0] => OVERALL
            [1] => -
            [2] => 1017924
            [3] => 135154
            [4] => 16.57%
            [5] => $1354866.20
            [6] => 23.81%
            [7] => 7.53
        )



我想将两个数组合并为一个...结果如下:

Array
(
    [0] => Array
        (
            [0] => "Product Category"
            [1] => Product Name
            [2] => Product ID
            [3] => Average Item Price
            [4] => Item Sales
            [5] => Items Sold
            [6] => Product Conversion (Sold/Views)
            [7] => Item Abandonment Rate
            [8] => "Content Category"
            [9] => Page Name
            [10] => Page Views
            [11] => Sessions
            [12] => Orders / Session
            [13] => Sales
            [14] => Bounce Rate
            [15] => Page Views / Session

        )
     [1] => Array
        (
            [0] => OVERALL
            [1] => -
            [2] => -
            [3] => $51.17
            [4] => $1335974.77
            [5] => 26111
            [6] => 16.25%
            [7] => 42.06%
            [8] => OVERALL
            [9] => -
            [10] => 1017924
            [11] => 135154
            [12] => 16.57%
            [13] => $1354866.20
            [14] => 23.81%
            [15] => 7.53

        )

我只想输出匹配的数组,其中“Product Category”==“Content Category”,并结合两个数组元素......

我不太确定是否有预定义函数,但我假设我必须做某种array_intersect()或array_merge()...主要的一点是抓住匹配元素和将两个数组中的两个元素合并为一个,这样我就可以进行一些后处理计算...

谢谢!

2 个答案:

答案 0 :(得分:1)

这会生成您要求的数组:

$result = array(
    array_merge($A[0], $B[0]), 
    array_merge($A[1], $B[1])
);

答案 1 :(得分:0)

我想出了我的问题的答案..

// loop through ARRAY A
for ($i = 0; $i < count($arrA); $i++) {

    //loop through ARRAYB

    for ($j = 0; $j < count($arrB); $j++) {

        // if arrA value 0 matches arrB value 0
        if ($arrA[$i][0] == $arrB[$j][0]) {
            //append arrB to arrA
            $arrA[$i] = array_merge($arrA[$i], $arrB[$j]);
            break;
        }
    }
}

//clean out items

for ($i = (count($arrA)-1); $i >= 0 ; $i--) {

    if (count($arrA[$i]) < 16) {
        unset($arrA[$i]);
    }
}