到阵列中给出的其他变量时明白在PHP阵列 [英] To understand an array in PHP when given the other variable in the array

查看:117
本文介绍了到阵列中给出的其他变量时明白在PHP阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请,请查的回答,我们是pretty关闭最终解决方案。我们与这些数据调试code


  1. 数据库PostgreSQL中

  2. 测试SQL查询

-

问题在code相关的尚未解决的问题

你怎么能参考的 PHP 的-element以下数组中?

我有以下数据

 阵列

    [1] =>排列
        (
            [标签] =>排列
                (
                    [0] => PHP
                    [1] =>脚本
                )        )    [2] =>排列
        (
            [标签] =>排列
                (
                    [0] => SSH
                    [1] =>网络
                )        ))

其中阵列代表 tags_and_id question_id 标记分别。
换句话说,变量 $ tags_and_id 有柱 question_id ,并且仍然有子列标签并仍然每个单独的标签。

我想知道你怎么能参考标记的 PHP

我运行不成功得到什么作为输出

 回声$ tags_and_id [1] ['标签'] [0];


解决方案

如果您想添加有关每个问题的更多信息,你可以简单地添加到 $ end_array [1]
这是我第的foreach 语句创建数组:

  //最后的结果原来是这样的:
    阵列(
          1 =>阵列(
                       '标签'= GT;阵列(
                                      0 => PHP
                                      1 => SQL),
                    )
         );


 < PHP
$ dbconn = pg_connect(主机=本地主机端口= 5432数据库名= NOA用户= NOA密码= 123);如果(空($ _ GET)){
    //获得冠军和question_ids
    $ result_titles_tags = PG_ prepare($ dbconn,query777        选择question_id,标题
        FROM问题
        WHERE question_id IN
        (
            SELECT question_id
            FROM问题
            ORDER BY was_sent_at_time
            DESC LIMIT 50
        )
        ORDER BY was_sent_at_time
        DESC LIMIT 50;
    );
    $ result_titles = pg_execute($ dbconn,query777,阵列());    //标签
    $ result_tags = PG_ prepare($ dbconn,query9
        SELECT question_id,标签
        从标签
        WHERE question_id IN
            (SELECT question_id
            FROM问题
            ORDER BY was_sent_at_time
            DESC LIMIT 50
            );
    );
    $ result_tags = pg_execute($ dbconn,query9,阵列());

和code。在最初的问题。

  //通过每个标签去
    而($ tags_and_Qid = pg_fetch_array($ result_tags)){
        //标签添加到标签的数组,这个问题
        $ end_array [$ tags_and_Qid ['question_id'] ['标签'] [] = $ tags_and_Qid ['标签'];
    }    //首先编译数据
    //通过每一个问题去    而($ titles_and_Qid = pg_fetch_array($ result_titles)){
        回声(< D​​IV CLASS ='question_summary'>中
                。 $ titles_and_Qid ['标题']
                  //问题就在这里!
                  //你怎么能打印的标题,这样
                  //他们被分配到每个标签-set?
            );        $ I = 0;
        //然后循环通过每个问题
        的foreach($ end_array为$ tags_and_Qid ['question_id'] => $ tags_and_Qid ['标签'])
        {        回声(\\ n \\ nITERATION号为$ I); //将code是越野车在这里
                    //例如,我们获得9次迭代的3个问题        //创建HTML开始
        回声(< D​​IV CLASS ='标签'>中);
            //通过每个标签去        //不知道这
                的foreach($ end_array [$ tags_and_Qid ['question_id'] ['标签']为$标记)                {
                    回声(<一类='post_tagHREF =标记=
                    。 $标记
                    。 '>中
                        。 $标记
                    。 &所述; / A>中
                    );
                }
        //结束HTML
            呼应'< / DIV>';        $ I ++;
        }
        回声(< / DIV>中);
    }    //结束的问题清单
    回声(< / DIV>中
    );
}
?>

Please, see Cha's answer where we are pretty close the final solution. We are debugging the code with these data

  1. database in PostgreSQL
  2. test sql-queries

--

Problem related to the Unsolved problem in the code

How can you refer to the php -element in the following array?

I have the following data

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.

I would like know how you can refer to the tag php.

I run unsuccessfully getting nothing as an output

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

解决方案

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());

and the code in the question initially

    // 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>"
    );
}
?>

这篇关于到阵列中给出的其他变量时明白在PHP阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆