1

配列に値が存在するかどうかを確認しようとしています。存在する場合は、データベースからその値に関する情報をエコーする必要があります。

私が今得ているのは「null」の結果ですが、配列とデータベースの両方に存在するリストに少なくとも 3 つの結果が得られると予想しています。

これは配列の var_dump の外観です。配列全体ではありませんが、引き続き同じように見えます。$friends = array($friends['data']);

array(1) {
[0]=>
array(680) {
[0]=>
array(2) {
  ["name"]=>
  string(17) "One friends name"
  ["id"]=>
  string(8) "FRIEND_ID"
}
[1]=>
array(2) {
  ["name"]=>
  string(13) "Another friends name"
  ["id"]=>
  string(9) "FRIEND_ID"
}
[2]=>
array(2) {
  ["name"]=>
  string(22) "Another friends name"
  ["id"]=>
  string(9) "FRIEND_ID"
}

PHP コード:

<?php

$query_top_list_friends = mysql_query("
SELECT * FROM ".$DBprefix."users 
WHERE center_id='" . $personal['center_id'] . "' 
ORDER BY workouts DESC LIMIT 10");

$i = 0;
$friends = array($friends['data']);

while ($top_list_friends = mysql_fetch_array($query_top_list_friends)) 
{
//Below it should only echo if the "fid" in the database and the "id" in the array is equal, then it should echo information based on that id from the database
if($friends[$top_list_friends['fid']])
{
    $i++;
    echo "<div class='user'>";
    echo "<span class='number'>" . $i . "</span>";
    echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
    echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
    echo "</div>";
} 
}  

これを修正する方法はありますか?

4

2 に答える 2

2

$friends = array($friends['data']);友人の配列をインデックス 0 に保持しているように見えるので、呼び出すときif($friends[$top_list_friends['fid']])に、友人を保持する配列の中ではなく、友人を保持する配列の配列を見ているのです。

に変更$friends = array($friends['data']);してみてください$friends = $friends['data'];。結果が得られるはずですが、fid はどのように機能するのでしょうか。配列のインデックスまたは配列のキーを指していますか? 上に投稿した配列から、アクセス可能な値は次のとおりです。

$friends[0][0] // One friend
$friends[0][1] // Another friend
$friends[0][2] // Another friend

$friends[0][$fid]そのため、データを返すには呼び出しが整数 0 から 2 である必要があるため、fid が整数であることを確認してください。

ご不明な点がございましたら、喜んでお答えいたします。

于 2013-09-23T19:21:13.543 に答える
1

を使用してこれを達成できるはずSQLですIN

$array = array(
    array(
        'id' => 5,
        'name' => 'Harry'
    ),
    array(
        'id' => 8,
        'name' => 'Josh'
    )
);

// create an array with just your fid's
$sql_in_values = array_map(function($ele){
    return $ele['id'];
}, $array);

// select all entries where the fid's are specified
$query_top_list_friends = mysql_query("
    SELECT * FROM ".$DBprefix."users 
    WHERE center_id='" . $personal['center_id'] . "'
    AND WHERE fid IN ('" . implode(',', $sql_in_values) . "')
    ORDER BY workouts DESC LIMIT 10");

$i = 0;
while($top_list_friends = mysql_fetch_array($query_top_list_friends)){
    echo "<div class='user'>";
    echo "<span class='number'>" . ++$i . "</span>";
    echo "<span class='name'>" . $top_list_friends['name'] . "</span>";
    echo "<span class='workouts'>" . $top_list_friends['workouts'] . "</span>";
    echo "</div>";
} 
于 2013-09-23T19:34:07.723 に答える