-5

我有一个 sql 查询,它为我提供以下格式的数组。基本上,第零个元素是 event_id,第一个元素是与事件相关的 photo_id。以下是数组

Array
(
[0] => Array
    (
        [0] => 1
        [1] => 1
    )

[1] => Array
    (
        [0] => 1
        [1] => 2
    )

[2] => Array
    (
        [0] => 1
        [1] => 3
    )

[3] => Array
    (
        [0] => 0
        [1] => 4
    )

[4] => Array
    (
        [0] => 0
        [1] => 5
    )

[5] => Array
    (
        [0] => 1
        [1] => 6
    )

[6] => Array
    (
        [0] => 0
        [1] => 7
    )

[7] => Array
    (
        [0] => 1
        [1] => 8
    )

[8] => Array
    (
        [0] => 0
        [1] => 9
    )

[9] => Array
    (
        [0] => 1
        [1] => 10
    )

[10] => Array
    (
        [0] => 2
        [1] => 11
    )

[11] => Array
    (
        [0] => 2
        [1] => 12
    )

)

我需要将所有第零个元素转换为键,将第一个元素转换为值。即所有相同的事件ID 都有自己的一组照片。所以输出应该像:

array(
    [1]=>[1,2,3,6,8,10]
    [0]=>[4,5,7,9]
    [2]=>[11,12]

谢谢 !

4

2 回答 2

2
$newArray = array ();

foreach ($array as $key => $value)
{
    $newArray[$value[0]][] = $value[1];
}

print_r($newArray);
于 2012-10-24T10:25:02.523 回答
1

干得好:

$res = array();
foreach($arr as $k=>$v){
    $res[$v[0]][]=$v[1];
}
print_r($res);
于 2012-10-24T10:25:16.963 回答