Получение данных из массива JSON в выражении PHP foreach - PullRequest
0 голосов
/ 08 июня 2018

Я пытаюсь получить данные из массива 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
    )

)

IЯ пытаюсь получить ключевые результаты заголовка.Вот мой запрос:

$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 ]

0 голосов
/ 08 июня 2018

Чтобы вывести только результаты, вам просто нужно выбрать элементы 'webs' ...

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

Ошибки, которые вы получаете, заключаются в том, что в первый раз он пытается использовать ...

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

и итерируйте по этому.

0 голосов
/ 08 июня 2018

Попробуйте с этим кодом:

Рабочая демонстрация: https://eval.in/1017825

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

foreach ($data as $finalres) 
{
    echo $finalres["title"].PHP_EOL;
}
0 голосов
/ 08 июня 2018

попробуйте использовать ключ в foreach, как

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

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...