Я не знаю, как это сделать. Смотрите мой массив ниже. Я запускаю этот массив в цикле while и должен найти первый [attach_id]
для каждого [topic_id]
и может использовать $topic_id
, которые установлены в цикле …
Правильный вывод:
Первый цикл:
[attach_id] => 17989
(потому что это первый attach_id
для topic_id 20890
)
а потом
Второй цикл:
[attach_id] => 17896
(потому что это первый attach_id
для topic_id 20887
)
Но я не могу заставить его работать …
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 );
ВЫВОД:
// 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) }
<?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); } }
$topicIdArray = []; $foundAttachId = []; foreach ($arrayToSearch as $value) { if (array_key_exists('topic_id', $value) { if (!in_array($value['topic_id'], $topicIdArray)) { if (array_key_exists('attach_id', $value) { $foundAttachId[] = $value['attach_id']; $topicIdArray[] = $value['topic_id']; } } } } print_r($foundAttachId);
Это должно работать