コード:
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が初めてなので、私が試しているよりも簡単な方法があるはずです。