代码之家  ›  专栏  ›  技术社区  ›  Sam Adah

从php foreach语句中的json数组获取数据

  •  0
  • Sam Adah  · 技术社区  · 6 年前

    我正试图从JSON数组中获取数据——这个数组的结构非常复杂。

    这是我的阵列:

    Array
    (
    [query] => Plagiarism is the "wrongful appropriation" and "stealing and publication"
    [data] => Array
        (
            [error] => 0
            [webs] => Array
                (
                    [0] => Array
                        (
                            [title] => Plagiarism - Wikipedia
                            [url] => https://en.wikipedia.org/wiki/Plagiarism
                            [des] => 
                        )
    
                    [1] => Array
                        (
                            [title] => Plagiarism - Wikipedia
                            [url] => https://en.wikipedia.org/wiki/Plagiarism
                            [des] => Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's "language, thoughts, ideas, or expressions" and the representation of them as one's own original work. Plagiarism is considered academic dishonesty and a breach of journalistic ethics.
                        )
    
                    [2] => Array
                        (
                            [title] => What is the concept of plagiarism? - ResearchGate
                            [url] => https://www.researchgate.net/post/What_is_the_concept_of_plagiarism
                            [des] => “Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's "language, thoughts, ideas, or expressions" and the representation of them as one's own original work.
                        )
    
                    [3] => Array
                        (
                            [title] => essay1 | Plagiarism | Crime & Justice - Scribd
                            [url] => https://www.scribd.com/document/252964635/essay1
                            [des] => Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's"language, thoughts, ide...
                        )
    
                    [4] => Array
                        (
                            [title] => ARSSS
                            [url] => http://www.arsss.org/plagiarism.php
                            [des] => Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's "language, thoughts, ideas, or expressions" and the representation of them as one's own original work. Plagiarism is considered academic dishonesty and a breach of journalistic ethics.
                        )
    
                    [5] => Array
                        (
                            [title] => Plagiarism - Plagiarism - wattpad.com
                            [url] => https://www.wattpad.com/199706948-plagiarism
                            [des] => Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's "language, thoughts, ideas, or expressions" and the representation of them as one's own original work. The idea remains problematic …
                        )
    
                    [6] => Array
                        (
                            [title] => A DEFINITION OF PLAGIARISM - cmplt.ovh WordPress
                            [url] => https://www.compilatio.net/en/plagiarism-definition/
                            [des] => a definition of plagiarism “Plagiarism is the “wrongful appropriation” and “stealing and publication” of another author‘s “language, thoughts, ideas, or expressions,” and the representation of them as one’s own original work.
                        )
    
                    [7] => Array
                        (
                            [title] => What is plagiarism? | Yahoo Answers
                            [url] => https://answers.yahoo.com/question/index?qid=20160301142448AALJsfV
                            [des] => Mar 01, 2016 · Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's "language, thoughts, ideas, or expressions" and the representation of them as one's own original work.[1][2] The idea remains problematic with unclear definitions and unclear rules.[3][4][5] The modern concept of plagiarism as immoral …
                        )
    
                    [8] => Array
                        (
                            [title] => Reading: Plagiarism | ITE 115 Introduction to Computer ...
                            [url] => https://courses.lumenlearning.com/vccs-ite115-17sp/chapter/reading-plagiarism/
                            [des] => Introduction. Plagiarism is the “wrongful appropriation” and “stealing and publication” of another author’s “language, thoughts, ideas, or expressions” and the representation of them as one’s own original work.
                        )
    
                    [9] => Array
                        (
                            [title] => Adis-Group - DEFENITION OF PLAGIARISM Plagiarism is the ...
                            [url] => https://www.coursehero.com/file/15841247/Adis-Group/
                            [des] => View Adis-Group from COMMERCE 1101 at University of Santo Tomas. DEFENITION OF PLAGIARISM Plagiarism is the "wrongful appropriation" and "stealing and publication" of another author's "language,
                        )
    
                    [10] => Array
                        (
                            [title] => PLAGIARISM - Substantial Paper Guide - LibGuides at ...
                            [url] => http://libguides.library.arizona.edu/c.php?g=608443&p=4386531
                            [des] => There are many different definitions of plagiarism. Wikipedia defines it "as the wrongful appropriation and stealing and publication of another author's language, thoughts, ideas, or expressions and the representation of them as one's own original work."
                        )
    
                )
    
            [unique] => false
        )
    

    )

    我正在尝试获取标题键结果。这是我的问题:

    $data = $someArray["data"];
    foreach ($data as $result) {
        foreach ($result as $finalres) {
            echo $finalres["title"]."<br/>";
        }
      }
    }
    

    这是我得到的结果。该列表正在更新,但上面和下面显示了一个PHP错误。

    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\naris.com.ng\check_plagia.php on line 69
     1. Plagiarism - Wikipedia
    2. Plagiarism - Wikipedia
    3. What is the concept of plagiarism? - ResearchGate essay1 | Plagiarism |Crime & Justice - Scribd ARSSS
    4. Plagiarism - Plagiarism - wattpad.com
    5. A DEFINITION OF PLAGIARISM - cmplt.ovh WordPress
    6. What is plagiarism? | Yahoo Answers
    7. Reading: Plagiarism | ITE 115 Introduction to Computer ...
    8. Adis-Group - DEFENITION OF PLAGIARISM Plagiarism is the ...
    9. PLAGIARISM - Substantial Paper Guide - LibGuides at ...
    
    Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\naris.com.ng\check_plagia.php on line 69
    

    我不明白为什么即使结果显示了警告也会一直显示? 如何正确显示列表而不出现PHP错误?

    3 回复  |  直到 6 年前
        1
  •  0
  •   Pradeep    6 年前

    https://eval.in/1017825

    $data = $someArray["data"]['webs'];
    
    foreach ($data as $finalres) 
    {
        echo $finalres["title"].PHP_EOL;
    }
    
        2
  •  0
  •   Nigel Ren    6 年前

    foreach ($data['webs'] as $result) {
    

    [query] => Plagiarism is the "wrongful appropriation" and "stealing and publication"
    

        3
  •  0
  •   Rajamohan S    6 年前

    foreach ($data as $key => $result) {}

    http://php.net/manual/en/control-structures.foreach.php