dongle2627 2017-07-22 15:14
浏览 36

php数组深层次[关闭]

Word-Array
     Words1-array
     Words1 meaning
        -Words1 Synonyms-
             -Synonym1,Synonym2.etc(Array)
        -Words1 Antonyms
             -Antonym1,Antonym2.etc(Array)
    Words2-array
     Words2 meaning
        -Words2 Synonyms-
             -Synonym1,Synonym2.etc(Array)
        -Words2 Antonyms
             -Antonym1,Antonym2.etc(Array)

$ar1 = array(array("Words"=>array("word","meaning","synonyms"=>array(),"antonyms"=array()))));

I was trying to create Php Array of Arrays for Generating JSON Result but always failed to achieve this

  • 写回答

1条回答 默认 最新

  • dongshi6844 2017-07-22 15:24
    关注

    Something like this?

    $words = array(
        'word1' => array(
            'meaning' => 'word1 meaning',
            'synonyms' => array(
                'word1 synonym1',
                'word1 synonym2'
            ),
            'antonyms' => array(
                'word1 antonym1',
                'word1 antonym2'
            )
        ),
        'word2' => array(
            'meaning' => 'word2 meaning',
            'synonyms' => array(
                'word2 synonym1',
                'word2 synonym2'
            ),
            'antonyms' => array(
                'word2 antonym1',
                'word2 antonym2'
            )
        )
    );
    

    If this is what you're looking for, it is pretty straightforward to create such an array dynamically from whatever datasource you have available. Since you haven't provided that information in your question, let's assume you have a MySQL database that's structured like this:

                     +----------+
    +---------+      | synonyms |
    | words   |      +----------+
    +---------+      | id       |
    | id      | -+-> | word_id  |
    | word    |  |   | synonym  |
    | meaning |  |   +----------+
    +---------+  |
                 |   +----------+
                 |   | antonyms |
                 |   +----------+
                 |   | id       |
                 \-> | word_id  |
                     | antonym  |
                     +----------+
    

    This is of course very simplistic, you'll have a lot of duplicates in the synonyms and antonyms table with this configuration.

    You can use standard PDO or mysqli functionality to retrieve the information from your database. Since it's not clear whether your datasource is in fact a MySQL-database, I'm going to assume mysqli and use very basic queries in this example. After all, the purpose here is not to teach you how to communicate with your database securely, but how to build a multidimensional array you can pass through json_encode(). Here's one way you can do that:

    $words = array();
    
    $mysqli = new mysqli(...);
    
    // retrieve the list of words from the `words` table
    $words_res = $mysqli->query('SELECT * FROM `words`');
    while ($word = $words_res->fetch_assoc()) {
        // build the initial sub-array for this word, we'll be adding
        // it to $words after we fill it
        $word_array = array(
            'meaning' => $word['meaning'],
            'synonyms' => array(),
            'antonyms' => array()
        );
    
        // retrieve all synonyms for this word
        $synonyms_res = $mysqli->query('SELECT * FROM `synonyms` WHERE `word_id` = ' . $word['id']);
        while ($synonym = $synonyms_res->fetch_assoc()) {
            // add this synonym to our $word_array
            $word_array['synonyms'][] = $synonym['synonym'];
        }
    
        // retrieve all antonyms for this word
        $antonyms_res = $mysqli->query('SELECT * FROM `antonyms` WHERE `word_id` = ' . $word['id']);
        while ($antonym = $antonyms_res->fetch_assoc()) {
            // add this antonym to our $word_array
            $word_array['antonyms'][] = $antonym['antonym'];
        }
    
        // now that $word_array is filled, we can add it to $words
        $words[$word['word']] = $word_array;
    }
    
    // since you say you want a JSON result, convert the array to
    // JSON and output it
    echo json_encode($words);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题