D
D
damarkuzz2021-09-01 17:26:13
PHP
damarkuzz, 2021-09-01 17:26:13

How to group a multidimensional array by overlapping values ​​of another PHP array?

Tell me how to split the First array into arrays according to the match condition in the string of the value post_titlefrom the Second array ?

First array

[0] => Array
        (
            [ID] => 80433
            [post_title] => Консоль из нержавеющей 1
        )

[1] => Array
     (
         [ID] => 80428
         [post_title] => Комод из неражавеющей 2
     )


Second array
[0] => Консоль
[1] => Комод

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-09-01
@damarkuzz

Nonsense, sure, but still:

<?php
$items = [
  ['ID' => 80433, 'post_title' => 'Консоль из нержавеющей 1'],
  ['ID' => 80428, 'post_title' => 'Комод из неражавеющей 2']
];

$categories = ['Консоль', 'Комод'];

$result = [];

foreach($categories as $category) {
  $results[$category] = [];
  foreach($items as $item) {		
    if (mb_strpos(mb_strtolower($item['post_title']), mb_strtolower($category)) !== false) {
      $result[$category][] = $item;
    }
  }
}

print_r($result);

PHP code online

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question