基于antoher ID阵列中的多阵列搜索结果排序只有第一项(PHP) [英] Sort out only first item in multi array based on antoher id in array (PHP)

查看:249
本文介绍了基于antoher ID阵列中的多阵列搜索结果排序只有第一项(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该怎么办了这一点。
请参阅下面我的数组。
我跑这个数组中while循环,需要先找到 [attach_id] 每个 [topic_id] ,并且可以使用 $ topic_id 这是在循环设定...

I dont know how do to this. See my array below. I run this array in a while loop and need to find first [attach_id] for each [topic_id] and can use $topic_id that are set in the loop...

正确的输出将是:

第一环:

[attach_id] => 17989 (因为这是第一次 attach_id topic_id 20890

Correct output would be:
First loop:
[attach_id] => 17989 (because this is the first attach_id for topic_id 20890)

然后
第二环:

[attach_id] => 17896 (因为这是第一次 attach_id topic_id 20887

and then
Second loop:
[attach_id] => 17896 (because this is the first attach_id for topic_id 20887)

但我不能让它工作....

But I cant get it to work ....

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 
)

推荐答案

由于我不知道你期望的结果正好就是,这里有两种可能性:

Because I didn't know what your expected result exactly is, here are two possibilities:

$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 );

OUTPUT:

OUTPUT:

// first version
array(2) {
  [0]=>
  array(2) {
    ["attach_id"]=>
    int(17989)
    ["topic_id"]=>
    int(20890)
  }
  [1]=>
  array(2) {
    ["attach_id"]=>
    int(17896)
    ["topic_id"]=>
    int(20887)
  }
}


//second version
array(2) {
  [20890]=>
  int(17989)
  [20887]=>
  int(17896)
}

这篇关于基于antoher ID阵列中的多阵列搜索结果排序只有第一项(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆