根据数组中的antoher id(PHP)只对多数组中的第一项进行排序

根据数组中的antoher id(PHP)只对多数组中的第一项进行排序,php,arrays,Php,Arrays,我不知道该怎么做。 请参见下面的我的数组。 我在while循环中运行此数组,需要首先为每个[topic\u id]查找[attach\u id],并且可以使用循环中设置的$topic\u id Array ( [0] => Array ( [attach_id] => 17989 [post_msg_id] => 298566 [topic_id] => 20890 [extension] => jpg [mime

我不知道该怎么做。 请参见下面的我的数组。 我在while循环中运行此数组,需要首先为每个
[topic\u id]
查找
[attach\u id]
,并且可以使用循环中设置的
$topic\u id

Array ( 
[0] => Array
( 
    [attach_id] => 17989 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 142437 
    [filetime] => 1442566541 
    [thumbnail] => 1
)
[1] => Array
( 
    [attach_id] => 17990 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 213432 
    [filetime] => 1442566541 
    [thumbnail] => 1
) 
[2] => Array 
(
    [attach_id] => 17991 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 63320 
    [filetime] => 1442566541 
    [thumbnail] => 1 
)
[3] => Array
( 
    [attach_id] => 17988 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 171560 
    [filetime] => 1442566540 
    [thumbnail] => 1
)
[4] => Array
(
    [attach_id] => 17896 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 304056 
    [filetime] => 1441372805 
    [thumbnail] => 1 
) 
[5] => Array
(
    [attach_id] => 17895 
    [post_msg_id] => 298546 
    [topic_id] => 20887  
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 125938 
    [filetime] => 1441372804 
    [thumbnail] => 1
)
[6] => Array 
(
    [attach_id] => 17894 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 328378 
    [filetime] => 1441372785 
    [thumbnail] => 1 
)
正确的输出应该是:
第一个循环:
[attach\u id]=>17989
(因为这是
主题\u id 20890
的第一个
attach\u id

Array ( 
[0] => Array
( 
    [attach_id] => 17989 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 142437 
    [filetime] => 1442566541 
    [thumbnail] => 1
)
[1] => Array
( 
    [attach_id] => 17990 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 213432 
    [filetime] => 1442566541 
    [thumbnail] => 1
) 
[2] => Array 
(
    [attach_id] => 17991 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 63320 
    [filetime] => 1442566541 
    [thumbnail] => 1 
)
[3] => Array
( 
    [attach_id] => 17988 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 171560 
    [filetime] => 1442566540 
    [thumbnail] => 1
)
[4] => Array
(
    [attach_id] => 17896 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 304056 
    [filetime] => 1441372805 
    [thumbnail] => 1 
) 
[5] => Array
(
    [attach_id] => 17895 
    [post_msg_id] => 298546 
    [topic_id] => 20887  
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 125938 
    [filetime] => 1441372804 
    [thumbnail] => 1
)
[6] => Array 
(
    [attach_id] => 17894 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 328378 
    [filetime] => 1441372785 
    [thumbnail] => 1 
)
然后
第二个循环:
[attach\u id]=>17896
(因为这是
主题\u id 20887
的第一个
attach\u id

Array ( 
[0] => Array
( 
    [attach_id] => 17989 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 142437 
    [filetime] => 1442566541 
    [thumbnail] => 1
)
[1] => Array
( 
    [attach_id] => 17990 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 213432 
    [filetime] => 1442566541 
    [thumbnail] => 1
) 
[2] => Array 
(
    [attach_id] => 17991 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 63320 
    [filetime] => 1442566541 
    [thumbnail] => 1 
)
[3] => Array
( 
    [attach_id] => 17988 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 171560 
    [filetime] => 1442566540 
    [thumbnail] => 1
)
[4] => Array
(
    [attach_id] => 17896 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 304056 
    [filetime] => 1441372805 
    [thumbnail] => 1 
) 
[5] => Array
(
    [attach_id] => 17895 
    [post_msg_id] => 298546 
    [topic_id] => 20887  
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 125938 
    [filetime] => 1441372804 
    [thumbnail] => 1
)
[6] => Array 
(
    [attach_id] => 17894 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 328378 
    [filetime] => 1441372785 
    [thumbnail] => 1 
)
但是我不能让它工作

Array ( 
[0] => Array
( 
    [attach_id] => 17989 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 142437 
    [filetime] => 1442566541 
    [thumbnail] => 1
)
[1] => Array
( 
    [attach_id] => 17990 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 213432 
    [filetime] => 1442566541 
    [thumbnail] => 1
) 
[2] => Array 
(
    [attach_id] => 17991 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 63320 
    [filetime] => 1442566541 
    [thumbnail] => 1 
)
[3] => Array
( 
    [attach_id] => 17988 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 171560 
    [filetime] => 1442566540 
    [thumbnail] => 1
)
[4] => Array
(
    [attach_id] => 17896 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 304056 
    [filetime] => 1441372805 
    [thumbnail] => 1 
) 
[5] => Array
(
    [attach_id] => 17895 
    [post_msg_id] => 298546 
    [topic_id] => 20887  
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 125938 
    [filetime] => 1441372804 
    [thumbnail] => 1
)
[6] => Array 
(
    [attach_id] => 17894 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 328378 
    [filetime] => 1441372785 
    [thumbnail] => 1 
)
)


Array ( 
[0] => Array
( 
    [attach_id] => 17989 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 142437 
    [filetime] => 1442566541 
    [thumbnail] => 1
)
[1] => Array
( 
    [attach_id] => 17990 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 213432 
    [filetime] => 1442566541 
    [thumbnail] => 1
) 
[2] => Array 
(
    [attach_id] => 17991 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 63320 
    [filetime] => 1442566541 
    [thumbnail] => 1 
)
[3] => Array
( 
    [attach_id] => 17988 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 171560 
    [filetime] => 1442566540 
    [thumbnail] => 1
)
[4] => Array
(
    [attach_id] => 17896 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 304056 
    [filetime] => 1441372805 
    [thumbnail] => 1 
) 
[5] => Array
(
    [attach_id] => 17895 
    [post_msg_id] => 298546 
    [topic_id] => 20887  
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 125938 
    [filetime] => 1441372804 
    [thumbnail] => 1
)
[6] => Array 
(
    [attach_id] => 17894 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 328378 
    [filetime] => 1441372785 
    [thumbnail] => 1 
)

这应该行得通

因为我不知道您的预期结果到底是什么,这里有两种可能性:

Array ( 
[0] => Array
( 
    [attach_id] => 17989 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 142437 
    [filetime] => 1442566541 
    [thumbnail] => 1
)
[1] => Array
( 
    [attach_id] => 17990 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 213432 
    [filetime] => 1442566541 
    [thumbnail] => 1
) 
[2] => Array 
(
    [attach_id] => 17991 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 63320 
    [filetime] => 1442566541 
    [thumbnail] => 1 
)
[3] => Array
( 
    [attach_id] => 17988 
    [post_msg_id] => 298566 
    [topic_id] => 20890 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 171560 
    [filetime] => 1442566540 
    [thumbnail] => 1
)
[4] => Array
(
    [attach_id] => 17896 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 304056 
    [filetime] => 1441372805 
    [thumbnail] => 1 
) 
[5] => Array
(
    [attach_id] => 17895 
    [post_msg_id] => 298546 
    [topic_id] => 20887  
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 125938 
    [filetime] => 1441372804 
    [thumbnail] => 1
)
[6] => Array 
(
    [attach_id] => 17894 
    [post_msg_id] => 298546 
    [topic_id] => 20887 
    [extension] => jpg 
    [mimetype] => image/jpeg 
    [filesize] => 328378 
    [filetime] => 1441372785 
    [thumbnail] => 1 
)
$myArray = array (
    array (
        "attach_id" => 17989,
        "post_msg_id" => 298566,
        "topic_id" => 20890 ,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 142437,
        "filetime" => 1442566541,
        "filetime" => 1,
    ),
    array(
        "attach_id" => 17990,
        "post_msg_id" => 298566,
        "topic_id" => 20890,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 213432,
        "filetime" => 1442566541,
        "filetime" => 1,
    ),
    array (
        "attach_id" => 17991,
        "post_msg_id" => 298566,
        "topic_id" => 20890,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 63320,
        "filetime" => 1442566541,
        "filetime" => 1,
    ),
    array(
        "attach_id" => 17988,
        "post_msg_id" => 298566,
        "topic_id" => 20890,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 171560,
        "filetime" => 1442566540,
        "filetime" => 1,
    ),
    array(
        "attach_id" => 17896,
        "post_msg_id" => 298546,
        "topic_id" => 20887,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 304056,
        "filetime" => 1441372805,
        "filetime" => 1,
    ),
    array (
        "attach_id" => 17895,
        "post_msg_id" => 298546,
        "topic_id" => 20887,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 125938,
        "filetime" => 1441372804,
        "filetime" => 1,
    ),
    array(
        "attach_id" => 17894,
        "post_msg_id" => 298546,
        "topic_id" => 20887,
        "extension" => "jpg",
        "mimetype" => "image/jpeg",
        "filesize" => 328378,
        "filetime" => 1441372785,
        "filetime" => 1,
    )
);

$firstResultArray = array();

// creates a new array with two keys => topic_id and attach_id
foreach( $myArray as $array ) {
    if( isset( $firstResultArray[ $array["topic_id"] ] ) ) continue;
    $firstResultArray[ $array["topic_id"] ] = array( "attach_id" => $array["attach_id"], "topic_id" => $array["topic_id"] );
}
// re-index array
$firstResultArray = array_values( $firstResultArray );

// create new array with topic_id as index and attach_id as value
$secondResultArray = array();
foreach( $myArray as $array ) {
    if( isset( $secondResultArray[ $array["topic_id"] ] ) ) continue;
    $secondResultArray[ $array["topic_id"] ] = $array["attach_id"];
}

echo "<pre>";
var_dump( $firstResultArray );

var_dump( $secondResultArray );

也许可以创建一个for循环,将
topic\u id
的所有唯一实例收集到一个数组中。然后使用另一个for循环检查是否是新创建数组中其中一个的第一个实例,如果是,则抓住它并从
topic\u id
的数组中删除该值。希望这是有道理的。什么东西你不能去上班?你能分享你尝试过的吗?