Dizideki diğer değişken verildiğinde PHP bir dizi anlamak için

2 Cevap php

Biz nihai çözüm oldukça yakın Cha cevabını bakınız. Biz bu verilerle kod hata ayıklama

  1. database in PostgreSQL
  2. test sql-queries

-

Problem related to the Unsolved problem in the code

Şu dizi How can you refer to the php-eleman?

Ben aşağıdaki veri

Array
(
    [1] => Array
        (
            [tag] => Array
                (
                    [0] => php
                    [1] => scripts
                )

        )

    [2] => Array
        (
            [tag] => Array
                (
                    [0] => ssh
                    [1] => network
                )

        )

)

where the arrays stand for tags_and_id, question_id and tag respectively. In other words, the variable $tags_and_id has the column question_id and that still has a subcolumn tag and that still each single tag.

Ben istiyorum sen etiketi php bakın nasıl biliyorum.

Ben başarısız bir çıkış olarak hiçbir şey elde çalıştırmak

echo $tags_and_id[1]['tag'][0];

2 Cevap

If you want to add more information about each question, you can simply add to $end_array[1]. This is the array my first foreach statement creates:

// The end result turns out to be something like this:
    array(
          1 => array(
                       'tags' => array(
                                      0 => "php",
                                      1 => "sql"),
                    )
         );


<?php 
$dbconn = pg_connect("host=localhost port=5432 dbname=noa user=noa password=123");

if( empty($_GET) ) {
    // to get titles and question_ids
    $result_titles_tags = pg_prepare( $dbconn, "query777",

        "SELECT question_id, title
        FROM questions
        WHERE question_id IN
        (    
            SELECT question_id
            FROM questions
            ORDER BY was_sent_at_time
            DESC LIMIT 50
        )
        ORDER BY was_sent_at_time
        DESC LIMIT 50;"
    );
    $result_titles = pg_execute( $dbconn, "query777", array());

    // TAGS
    $result_tags = pg_prepare( $dbconn, "query9",
        "SELECT question_id, tag
        FROM tags
        WHERE question_id IN
            ( SELECT question_id
            FROM questions
            ORDER BY was_sent_at_time
            DESC LIMIT 50
            );"
    );
    $result_tags = pg_execute( $dbconn, "query9", array());

ve ilk olarak söz konusu kod

    // Go through each Tag
    while( $tags_and_Qid = pg_fetch_array( $result_tags )) {
        // Add the Tag to an array of tags for that question
        $end_array [ $tags_and_Qid['question_id'] ] ['tag'] [] = $tags_and_Qid['tag'];
    }

    // First compile the Data
    // Go through each question

    while( $titles_and_Qid = pg_fetch_array( $result_titles ) ) {
        echo ("<div class='question_summary'>"
                . $titles_and_Qid['title']
                  // Problem here!
                  // How can you print the titles such that 
                  // they are assigned to each tag -set?
            );

        $i = 0;
        // Then Loop Through each question
        foreach( $end_array as $tags_and_Qid['question_id'] => $tags_and_Qid['tag'] )
        {

        echo ("\n\nITERATION NUMBER IS " . $i);      // The code is buggy here
                    // For instance, we get 9 iterations for 3 questions

        // Create the starting HTML
        echo ("<div class='tags'>");
            // Go through each tag

        // not sure about this
                foreach( $end_array[$tags_and_Qid['question_id']] ['tag'] as $tag )

                {
                    echo ( "<a class='post_tag' href='?tag="
                    . $tag
                    . "'>"
                        . $tag
                    . "</a>"
                    );
                }
        // end the html
            echo '</div>';

        $i++; 
        }
        echo ("</div>");
    }

    // to end the list of questions
    echo ("</div>"
    );
}
?>

print_r başarısız olursa, o zaman bir şey daha önce yanlış. Sizin veri muhtemelen yapılandırılmıştır ediliyor düşünüyorum şekilde yapılandırılmıştır varlık değildir.

Ve ben sadece sadece etiketi ve soru adı varsa, sadece aşağıdaki gibi görünen bir tek seviyeli dizi olarak saklayabilirsiniz tavsiye edebilirsiniz:

array(
'php' => '7',
'sql' => '7',
'python' => '3'
)

Eğer evet sonra saklamak için gidiyoruz başka şeyler varsa, ne yaptığınızı yapmak gerekecek. Eğer parantez içinde tuşları ile dizi erişemiyorsanız bu durumda, o zaman sen tuşları olduklarını düşünüyorum muhtemelen ne değildir, ya da bir yere kodu ile yanlış bir şey var.

Ben senin koduna baktım, ve neden $row[0] yerine $row['question_id'] in gibi sorgu sonuçlarını erişiyor?

Bir sorgudan veri aslında PHP ile saklanmaz çünkü Ayrıca, print_r($result_tags), sadece başvurulan olamaz. Bu nedenle Eğer komut bir seferde sonuç bir satır arayacak gibi, onu geçmesi için bir süre döngü kullanmak zorunda.

Diyorsun ki: $result_tags hiçbir değer için bir defada sonuç bir satır aramak için kullanılan sorgu için bir referans dışında olduğundan foreach( $result_tags as $question_id => $data ) Bu, mantıklı değil . Bunun yerine yapmak istiyorum:

while( $row2 = pg_fetch_array( $result_tags )) {
    $question_id = $row2['questions_question_id'];
    $data = $row2['data'];
    $end_array[$question_id]['tags'][] = $data;  
      // you can't have your $data['tag'] as that refers to the 
      // value of an array that doesn't exist
}