A
A
Andrey Angelovich Stock2018-04-03 16:00:33
PHP
Andrey Angelovich Stock, 2018-04-03 16:00:33

How to compare PHP arrays correctly?

Hi all. There are many arrays like

Array ( [0] => 12512650 [1] => https://www.makfsa.ru/ [2] => https://ru.wikipsedia.org/wiki/%D0%9C%D0%B0%D0%BA%D1%84%D0%B0 [3] => http://makfa.nichosst.ru/ [4] => http://ru.makfapasta.com/production/product/pasta/ [5] => http://ru.makfapasta.com/ [6] => https://www.utkaonos.ru/item/42/1096519 [7] => https://www.utkonos.ru/item/42/1096522 [8] => https://www.utkonos.ru/item/1381/3223485 [9] => https://www.uatkonos.ru/item/42/1096527 [10] => https://proactions.ru/actions/food/makfa/ )

there can be from 2 to 20 such arrays.
There is also an array like
Array ( [obj] => Array ( [0] => Array ( [url] => https://www.utkonos.ru/item/1381/3223485 [ton] => Не определена [type] => Не определен ) [1] => Array ( [url] => https://proactions.ru/actions/food/makfa/ [ton] => Не определена [type] => Не определен ) [2] => Array ( [url] => http://makfa.nichost.ru/ [ton] => Не определена [type] => Не определен ) [3] => Array ( [url] => https://ru.wikipedia.org/wikif/%D0%9C%D0%B0%D0%BA%D1%84%D0%B0 [ton] => Не определена [type] => Не определен ) [4] => Array ( [url] => http://ru.makfapasta.com/producftion/product/pasta/ [ton] => Не определена [type] => Не определен ) [5] => Array ( [url] => http://ru.mafkfapasta.com/ [ton] => Не определена [type] => Не определен ) ....

it is necessary to compare the url from the second array with the first arrays and form new arrays like the first ones, but with the addition of the corresponding ton and type values. How?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2018-04-03
@Immortal_pony

Use array_filter and leave in the second array only those values ​​that are in the first one.
More or less like this:

$someArray = []; // ваш первый массив со ссылками
$otherArray = ['obj'=>[]]; // ваш второй массив с расщиренными данными. 'obj' использовать необязательно, это я взял из примера данных.

$necessaryArray = array_values(array_filter($otherArray['obj'], function($element) use ($someArray) { // я использую array_values, чтобы ключи результирующего массива шли по порядку. Это необязательно, но часто бывает полезно
    return array_key_exists("url", $element) && in_array($element['url'], $someArray);
}));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question