【问题标题】:php array of arrays deep level [closed]php数组深层次的数组[关闭]
【发布时间】:2017-07-22 15:14:12
【问题描述】:
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()))));

我试图创建用于生成 JSON 结果的 Php 数组数组,但始终未能实现这一点

【问题讨论】:

    标签: php arrays associative-array


    【解决方案1】:

    这样的?

    $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'
            )
        )
    );
    

    如果这是您正在寻找的,那么从您可用的任何数据源动态创建这样一个数组非常简单。由于您没有在问题中提供该信息,假设您有一个结构如下的 MySQL 数据库:

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

    这当然非常简单,使用这种配置,synonymsantonyms 表中会有很多重复项。

    您可以使用标准的PDOmysqli 功能从数据库中检索信息。由于不清楚您的数据源是否实际上是 MySQL 数据库,我将假设 mysqli 并在此示例中使用非常基本的查询。毕竟,这里的目的不是教你如何安全地与你的数据库通信,而是如何构建一个你可以通过json_encode() 传递的多维数组。这是您可以做到这一点的一种方法:

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

    【讨论】:

    • 感谢您的回答,但是如何使用 PHP 添加或更新这些字段?
    • @MithunSudheendran 我添加了一个示例
    • 哇,您必须至少获得一个赞,才能写出出色的答案,超出 OP 的要求。可以添加一些改进(例如 - 使用准备好的语句),但这是 OP 应该学习的东西。干得好!
    • @AlonEitan 谢谢!
    猜你喜欢
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多