dongpin1059 2017-03-13 04:04
浏览 58

php - 将复杂的多维数据从API保存到数据库中

I am using oxford dictionary API to develop a dictionary function on my website. I have successfully implemented the function as I can search the word and pull the results from the API. However, I have a problem with saving these data into MySQL database.

This is what I get when I print_r($dic_array):

Array
(
[metadata] => Array
    (
        [provider] => Oxford University Press
    )

[results] => Array
    (
        [0] => Array
            (
                [id] => love
                [language] => en
                [lexicalEntries] => Array
                    (
                        [0] => Array
                            (
                                [entries] => Array
                                    (
                                        [0] => Array
                                            (
                                                [etymologies] => Array
                                                    (
                                                        [0] => Old English lufu, of Germanic origin; from an Indo-European root shared by Sanskrit lubhyati ‘desires’, Latin libet ‘it is pleasing’, libido ‘desire’, also by leave and lief
                                                    )

                                                [grammaticalFeatures] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [text] => Mass
                                                                [type] => Countability
                                                            )

                                                    )

                                                [homographNumber] => 000
                                                [senses] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [definitions] => Array
                                                                    (
                                                                        [0] => a strong feeling of affection
                                                                    )

                                                                [examples] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [text] => their love for their country
                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [text] => babies fill parents with intense feelings of love
                                                                            )

                                                                    )

                                                                [id] => m_en_gbus0596690.007
                                                                [subsenses] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => a strong feeling of affection and sexual attraction for someone
                                                                                    )

                                                                                [examples] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => they were both in love with her
                                                                                            )

                                                                                        [1] => Array
                                                                                            (
                                                                                                [text] => we were slowly falling in love
                                                                                            )

                                                                                    )

                                                                                [id] => m_en_gbus0596690.009
                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => affectionate greetings conveyed to someone on one's behalf
                                                                                    )

                                                                                [examples] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => give her my love
                                                                                            )

                                                                                    )

                                                                                [id] => m_en_gbus0596690.010
                                                                            )

                                                                        [2] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => a formula for ending an affectionate letter
                                                                                    )

                                                                                [examples] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => take care, lots of love, Judy
                                                                                            )

                                                                                    )

                                                                                [id] => m_en_gbus0596690.011
                                                                            )

                                                                        [3] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => a personified figure of love, often represented as Cupid.
                                                                                    )

                                                                                [domains] => Array
                                                                                    (
                                                                                        [0] => Roman History
                                                                                    )

                                                                                [id] => m_en_gbus0596690.012
                                                                                [variantForms] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => Love
                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [1] => Array
                                                            (
                                                                [definitions] => Array
                                                                    (
                                                                        [0] => a great interest and pleasure in something
                                                                    )

                                                                [examples] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [text] => his love for football
                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [text] => we share a love of music
                                                                            )

                                                                    )

                                                                [id] => m_en_gbus0596690.016
                                                            )

                                                        [2] => Array
                                                            (
                                                                [definitions] => Array
                                                                    (
                                                                        [0] => a person or thing that one loves
                                                                    )

                                                                [examples] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [text] => she was the love of his life
                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [text] => their two great loves are tobacco and whisky
                                                                            )

                                                                    )

                                                                [id] => m_en_gbus0596690.018
                                                                [notes] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [text] => count noun
                                                                                [type] => grammaticalNote
                                                                            )

                                                                    )

                                                                [subsenses] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => a friendly form of address
                                                                                    )

                                                                                [examples] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => it's all right, love
                                                                                            )

                                                                                    )

                                                                                [id] => m_en_gbus0596690.021
                                                                                [regions] => Array
                                                                                    (
                                                                                        [0] => British
                                                                                    )

                                                                                [registers] => Array
                                                                                    (
                                                                                        [0] => informal
                                                                                    )

                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => used in affectionate requests
                                                                                    )

                                                                                [examples] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => don't fret, there's a love
                                                                                            )

                                                                                    )

                                                                                [id] => m_en_gbus0596690.022
                                                                                [notes] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => "a love"
                                                                                                [type] => wordFormNote
                                                                                            )

                                                                                    )

                                                                                [registers] => Array
                                                                                    (
                                                                                        [0] => informal
                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                        [3] => Array
                                                            (
                                                                [definitions] => Array
                                                                    (
                                                                        [0] => (in tennis, squash, and some other sports) a score of zero; nil
                                                                    )

                                                                [domains] => Array
                                                                    (
                                                                        [0] => Tennis
                                                                    )

                                                                [examples] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [text] => love fifteen
                                                                            )

                                                                    )

                                                                [id] => m_en_gbus0596690.024
                                                            )

                                                    )

                                            )

                                    )

                                [language] => en
                                [lexicalCategory] => Noun
                                [pronunciations] => Array
                                    (
                                        [0] => Array
                                            (
                                                [audioFile] => http://audio.oxforddictionaries.com/en/mp3/love_gb_1.mp3
                                                [dialects] => Array
                                                    (
                                                        [0] => British English
                                                    )

                                                [phoneticNotation] => IPA
                                                [phoneticSpelling] => lʌv
                                            )

                                    )

                                [text] => love
                            )

                        [1] => Array
                            (
                                [entries] => Array
                                    (
                                        [0] => Array
                                            (
                                                [grammaticalFeatures] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [text] => Transitive
                                                                [type] => Subcategorization
                                                            )

                                                    )

                                                [homographNumber] => 001
                                                [senses] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [definitions] => Array
                                                                    (
                                                                        [0] => feel deep affection or sexual love for (someone)
                                                                    )

                                                                [examples] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [text] => do you love me?
                                                                            )

                                                                    )

                                                                [id] => m_en_gbus0596690.026
                                                                [subsenses] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [definitions] => Array
                                                                                    (
                                                                                        [0] => like or enjoy very much
                                                                                    )

                                                                                [examples] => Array
                                                                                    (
                                                                                        [0] => Array
                                                                                            (
                                                                                                [text] => I just love dancing
                                                                                            )

                                                                                        [1] => Array
                                                                                            (
                                                                                                [text] => I'd love a cup of tea
                                                                                            )

                                                                                    )

                                                                                [id] => m_en_gbus0596690.032
                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                                [language] => en
                                [lexicalCategory] => Verb
                                [pronunciations] => Array
                                    (
                                        [0] => Array
                                            (
                                                [audioFile] => http://audio.oxforddictionaries.com/en/mp3/love_gb_1.mp3
                                                [dialects] => Array
                                                    (
                                                        [0] => British English
                                                    )

                                                [phoneticNotation] => IPA
                                                [phoneticSpelling] => lʌv
                                            )

                                    )

                                [text] => love
                            )

                    )

                [type] => headword
                [word] => love
            )

    )

)

To echo out the result on my website:

foreach($dic_array['results'][0]['lexicalEntries'] as $word) {
        echo '<hr><div class="partOfSpeech"><p><b>'.$word['lexicalCategory'].'</b></p></div>';
            foreach($word['entries'][0]['senses'] as $definition) {
                echo '<p class="definition">'.$definition['definitions'][0].'</p>';

                if (!empty($definition['subsenses'])) {
                    foreach($definition['subsenses'] as $subsenses) {
                        echo '<p class="subDefinition" style="padding-left: 10px; "> -'.$subsenses['definitions'][0].'</p>'; 
                    }
                }
            }
    }    
  • 写回答

1条回答 默认 最新

  • doujiao3072 2017-03-13 04:36
    关注

    This is not an answer. I am the author who asks this question. Because I cannot write over 30000 characters, I use some extra content here.

    This is what I tried:

     foreach($dic_array['results'][0]['lexicalEntries'] as $word) {
                $lexicalCategory = serialize($word['lexicalCategory']);
    
                foreach($word['entries'][0]['senses'] as $definition) {
                    $wordDefinition = serialize($definition['definitions'][0]);
    
                    if (!empty($definition['subsenses'])) {
                        foreach($definition['subsenses'] as $subsenses) {
                            $wordSubdefinition = serialize($subsenses['definitions'][0]);
    
                            if(!empty($_POST['save'])) {
                                $sth = $this->db->prepare('INSERT INTO dictionary 
                                            (`word`, `partOfSpeech`, `definition`, `subDefinition`) 
                                            VALUES (:word, :partOfSpeech, :definition, :subDefinition)
                                            ');
    
                                $sth->execute(array(
                                    ':word' => $data['word'],
                                    ':partOfSpeech' => $lexicalCategory,
                                    ':definition' => $wordDefinition,
                                    ':subDefinition' => $wordSubdefinition
                                ));             
                            }
    
                        }
                    }
                }
    
            }
    

    I know it is too nested but once I can get this done, I will separate this into several functions.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度