I
I
inkShio2018-07-24 16:37:32
PHP
inkShio, 2018-07-24 16:37:32

How to catch GET and compare with value in array?

Hello. I catch a utm tag like this ?utm_term=Toys in the city of Pushkino
Then I have such an array

Array
(
    [0] => Array
        (
            [Message_ID] => 1
            [Title] => Пушкино
            [Morphy] => Пушкино // игрушки в Пушкино
            [Term] => пушкино // слова которые могу встретиться в utm_term
        )

    [1] => Array
        (
            [Message_ID] => 2
            [Title] => Броницы
            [Morphy] => Броницах // игрушки в Броницах
            [Term] => броницы,броницах // слова которые могу встретиться в utm_term
        )
    [2] => Array
        (
            [Message_ID] => 3
            [Title] => Воскресенск
            [Morphy] => Воскресенске // игрушки в Воскресенске
            [Term] => воскресенск,воскресенске // слова которые могу встретиться в utm_term
        )
)

The essence of utm_term is to compare with what is in [Term] and if there are matches, display [Morphy].
$UtmTerm = htmlspecialchars($_GET["utm_term"]); - ловим utm_term
$UtmTerm = mb_strtolower($UtmTerm); - привидом все нижний регистр
$UtmTermArray = explode(" ", $UtmTerm); - разбираем на массив

I get an Array ( [0] => toys [1] => in [2] => city [2] => Pushkino )
foreach($var as $k => $v) {
   $res = array_intersect(explode(",", $v[Term]), $UtmTermArray); // сравниваем получившиеся массивы
   if (!empty($res)) {
       print_r($res);
   }
}

I get Array ( [1] => pushkino ) - this is what is in array [Term]
But how can I display not what is in [Term], but what is [Morphy], because in Morphy is a leaning city.
And in general, maybe I don’t have the right approach to the task, can you advise how, what is worth redoing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-07-24
@inkShio

$ar =  [
    [
            'Message_ID' => 1,
            'Title' => 'Пушкино',
            'Morphy' => 'Пушкино', // игрушки в Пушкино
            'Term' => 'пушкино', // слова которые могу встретиться в utm_term
   ],[
            'Message_ID' => 2,
            'Title' => 'Броницы',
            'Morphy' => 'Броницах', // игрушки в Броницах
            'Term' => 'броницы,броницах', // слова которые могу встретиться в utm_term
    ],[
            'Message_ID' => 3,
            'Title' => 'Воскресенск',
            'Morphy' => 'Воскресенске', // игрушки в Воскресенске
            'Term' => 'воскресенск,воскресенске', // слова которые могу встретиться в utm_term
     ]
];

$UtmTerm = 'Игрушки в городе Пушкино';
$UtmTerm = mb_strtolower($UtmTerm);
$UtmTermArray = explode(" ", $UtmTerm);

$keys = [];
foreach($ar as $k => $v) {
   $res = array_intersect(explode(",", $v['Term']), $UtmTermArray);
   if (!empty($res)) {
       $keys[] = $k;
   }
}
var_dump($keys); // ключи массивов в которых найдены совпадения в Term

Or get the Morphy array right away:
// ...
$results = [];
foreach($ar as $k => $v) {
   $res = array_intersect(explode(",", $v['Term']), $UtmTermArray);
   if (!empty($res)) {
       $results[] = $v['Morphy'];
   }
}
var_dump($results);

As for the approach, it is better to take out such a sample in the database. If not, then ensure the code is reused.
PS
Add code and data to the question at once in such a state that you can simply copy and run.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question