代码:
if ( $_GET['tab'] == 'newest' ) {
// Go through each question
foreach( array_reverse( $end_array, true ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
{
// Grab the title for the first array
$title = $titles [ $tags_and_Qid['question_id'] ] ['title'];
// Grab the tags for the question from the second array
$tags = $end_array [ $tags_and_Qid['question_id'] ] ['tag'];
// Grab the username for the question from the second array
$username = $usernames [ $tags_and_Qid['question_id'] ] ['username'];
--- cut ----
}
}
我需要经常使用这段代码。唯一的区别是array_reverse (..., true)
在第一个示例中。
我试图通过制作一个函数organize_question
来解决这个问题来解决这个问题。我没有成功:
function organize_questions ( $tab ) {
if ( $_GET['tab'] == 'newest' ) {
echo ( "array_reverse ( $end_array , true )" );
// Problem here!
}
if ( $_GET['tab'] == 'oldest' ) {
echo ( "$end_array" );
// this does not work
} else {
echo ( "array_reverse ( $end_array , true )" );
// Problem here!
}
}
然后我将代码中的相关行更改为:
foreach( organize_question( $tab ) as $tags_and_Qid['question_id'] => $titles_and_Qid['title'] )
问题在于将变量从一个函数转移到另一个函数。
我试图将所有必要的变量放在函数的参数中,但是一切都被破坏了,因为这个函数有很多依赖项。
我是 PHP 新手,所以必须有比我正在尝试的更简单的方法来做到这一点。