Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort out only first item in multi array based on antoher id in array (PHP)

Tags:

arrays

php

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...

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

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 
)

)

like image 504
stavasknall Avatar asked May 31 '26 08:05

stavasknall


1 Answers

<?php
$attachTopicId = array();
foreach($array as $subArray) {
    if (array_key_exists($subArray["topic_id"], $attachTopicId)) {
        if ($attachTopicId[$subArray["topic_id"]] < $subArray["attach_id"]) {
            $attachTopicId[$subArray["topic_id"]] = $subArray["attach_id"];
        }
    }
    else {
        $attachTopicId[$subArray["topic_id"]] = $subArray["attach_id"];
    }
}

// test output 
if (count($attachTopicId) > 0) {
    foreach($attachTopicId as $key => $value) {
        print sprintf("Topic ID: %s Attach ID: %s", $key, $value);
    }
}
like image 101
Halayem Anis Avatar answered Jun 02 '26 22:06

Halayem Anis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!