0

私の質問は、私は配列を SQL クエリから移入しており、その配列をクエリの ID が重複していない新しい配列にマージしたいと考えています。また、配列を別の配列に連結して同じ結果を得ようとしましたが、配列を結合して正しく表示するにはどうすればよいですか。

  $rows[0] = array("181", "a","g");  //Results from previous  query
 $rows[1] = array("181","j","L")
 $rows[2] = array("181","p");
 $rows[3] = array("182","k");
 $rows[4] = array("183","l");
 $rows[5] = array("183","p");
 $id =0;
 $commentsH = ""; 

  while( $row=mysql_fetch_array($query_comments) ){

           If($id == $image){  //image id is the first element in array.

           $comments[] =$row;

         $commentsH = $comments.",".$commentsH[$i];

            }

            else{

           $id = $image;
           $i = $i +1;

           }

      }  


  $result = array();
  $result["result"] = 500;
  $result["message"] = "Database Read Successfully";
  $result["comments"] =$commentsH;
  echo json_encode($result);
  exit;


 EXPECTED OUTPUT

 $commentsH[0] = array("181", "a","g","j","L","p");
 $commentsH[1] = array("182","k");
 $commentsH[2] = array("183","l","p");
4

2 に答える 2

0

このコードは間違っています

while( $row=mysql_fetch_array($query_comments) ){

       If($id == $image){  //image id is the first element in array.

       $comments[] =$row;

     $commentsH = $comments.",".$commentsH[$i];

        }

        else{

       $id = $image;
       $i = $i +1;

       }

  }  

間違った変数と比較しようとしています。$image は配列の最初の要素を持つことはありません。これを試して

while( $row=mysql_fetch_array($query_comments) ){
       $image = $row[0];
       If($id == $image){  //image id is the first element in array.

    // put your further logic here
于 2013-04-28T10:03:31.837 に答える
0

上で簡単に述べたように、一度にすべてのデータを取得するためにクエリを再作成することもできますが、このアプリが何を行うべきか、アプリの構造やページが正確にわからないので、あなたが求めていることに基づいて例を挙げます。

配列をマージするための比較は、ここにあるものよりも動的である必要がありますが、配列を取得する方法が正確にわからないため、追加する必要があることに注意してください。以下は、比較およびマージする多くの方法の 1 つにすぎません。

$commentsH = array();
$rows = array();
$rows[0] = array("181", "a","g");  //Results from previous  query
$rows[1] = array("181","j","L", "z", "a");
$rows[2] = array("183", "d", "W", "t");
$rows[3] = array("183", "h", "W", "e");


// check 2 arrays
if(  $rows[0][0] == $rows[1][0]  ){
    for( $j=1; $j<count($rows[1]); $j++ ){
        $found = false;
        $val = "";
        for( $i=1; $i<count($rows[0]); $i++ ){
            if( $rows[1][$j] == $rows[0][$i] ){
                $found = true;
                break;
            }
        }

        if( $found == false )  array_push( $rows[0], $rows[1][$j] );
    }
}
$commentsH[] = $rows[0];

// check two more ...
if(  $rows[2][0] == $rows[3][0]  ){
    for( $j=1; $j<count($rows[3]); $j++ ){
        $found = false;
        $val = "";
        for( $i=1; $i<count($rows[2]); $i++ ){
            if( $rows[3][$j] == $rows[2][$i] ){
                $found = true;
                break;
            }
        }

        if( $found == false )  array_push( $rows[2], $rows[3][$j] );
    }
}
$commentsH[] = $rows[2];



// this block simple iterates through the commentsH array and prints out the keys and values cleanly for plain viewing
for( $i=0; $i<count($commentsH); $i++ ){
    foreach( $commentsH[$i] as $key=>$val ){
        echo $key . " => " . $val . "<br />";
    }

    echo "<br /><br />";
}

また、値を比較してそのようにマージする必要がある場合は、一意のキーが必要なため、連想配列を使用する方が簡単で論理的であることがよくあります。これを書き直して、array_key_exists()で配列キーが既に存在するかどうかをテストし、存在する場合は、配列を一意の値とマージしてみてください。

お役に立てれば ...

于 2013-04-28T11:10:38.023 に答える