从PHP foreach语句中获取JSON数组中的数据

时间:2018-06-08 17:36:41

标签: php arrays loops

我正在尝试从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/>";
    }
  }
}

这是我得到的结果。该列表是diaplaying,但上面和下面显示了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 个答案:

答案 0 :(得分:0)

尝试使用foreach中的键,如

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

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

答案 1 :(得分:0)

要输出结果,您只需要选择'网络'元素......

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

你得到的错误是它第一次试图使用......

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

并迭代这个。

答案 2 :(得分:0)

尝试使用此代码:

工作演示:https://eval.in/1017825

$data = $someArray["data"]['webs'];

foreach ($data as $finalres) 
{
    echo $finalres["title"].PHP_EOL;
}