开发者

How to filter this php array

开发者 https://www.devze.com 2023-03-07 08:49 出处:网络
I have an array: Array( [0] => Array( [\'title\'] => \'Apple\', [\'type\'] => \'1\' ), [1] => Array(

I have an array:

Array(
    [0] => Array(
        ['title'] => 'Apple',
        ['type'] => '1'
    ),
    [1] => Array(
        ['title'] => 'Oranage',
        ['type'] => '2'
    ),
    [2] => Array(
        ['title'] => 'Tomato',
        ['type'] => '1'
    ),
    //when compares with [0], 'type' is equal and both contains 'Apple', remove.
    [3] => Array(
        ['title'] => 'Red Apple',
        ['type'] => '1'
    ),
    [4] => Array(
        ['title'] =&开发者_StackOverflowgt; 'Big Tomato',
        ['type'] => '3'
    ),
    //when compares with [1], 'type' is equal and both contains 'Oranage', remove.
    [5] => Array(
        ['title'] => 'Sweet Oranage',   
        ['type'] => '2'
    )
);

If the 'type' element is equal, while 'title' elment contains the same string ([0] and [3] contains 'Apple'), then only one element will be used. The result will be:

Array(
    [0] => Array(
        ['title'] => 'Apple',
        ['type'] => '1'
    ),
    [1] => Array(
        ['title'] => 'Oranage',
        ['type'] => '2'
    ),
    [2] => Array(
        ['title'] => 'Tomato',
        ['type'] => '1'
    ),
    [4] => Array(
        ['title'] => 'Big Tomato',
        ['type'] => '3'
    )
);

How to filter this, I am so confused. Thanks.


If you consider your conditions as alternative, then if passing one of the following criteria, only one of all the values meeting specific criteria can be used:

  • the 'type' element is equal,
  • the 'title' element in each second level array are the same ([2] and [5]), or
  • one 'title' element contains another([0] and [3]),

If so, you can just use some indexing arrays for storing already found 'type' and 'title' variables and:

  1. add one variable,
  2. check whether the next variable:
    • has 'type' already in the index table,
    • has 'title' already in the index,
    • is contained in 'title' already defined in index or contains 'title' already defined in index,
  3. if 2. shows that at least one of the criteria is met, do not add it to the result, otherwise add it,
  4. go to point 2.

In the return you should receive what you described. Just write it in PHP.


Something like this should do:

$array = array(/* your array */);
$filteredArray = array();

foreach ($array as $key => $elem) {
    foreach ($filteredArray as $comp) {
        if ($elem['type'] == $comp['type'] &&
            (strpos($elem['title'], $comp['title']) !== false || strpos($comp['title'], $elem['title']) !== false)
        ) {
            continue 2;  // skip to the next $elem
        }
    }
    $filteredArray[$key] = $elem;
}


If the array is held in variable $ar, then:

$filtered = Array();

for(int $i = 0; $i < count($ar); $i++) {
  for(int $j = $i; $j < count($ar); $j++) {
    if($ar[$i][$type] == $ar[$i][$type]) {
      if(strlen($ar[$i][$title]) < strlen($ar[$j][$title])) {
        $larger = $j;
        $smaller = $i;
      }
      else {
        $larger = $i;
        $smaller = $j;
      }
      if(preg_match(strtolower($smaller), strtolower($larger)) {
        $filtered = array_merge($filtered, array_slice($ar, 0, $larger - 1), array_slice($ar, $larger, count($ar) - $larger));
      }
    }
  }
}

Finds two elements with equal types. Then, take the title of the element with the shortest title, and search through the other element for a preg_match. If found, merge the previous filtered array with all of the elements before and after the duplicate.

0

精彩评论

暂无评论...
验证码 换一张
取 消