1

I have an array like the following:

 5-9-21, 
 5-10-22,
 5-10-22,
 5-11-23,
 3-17-29,
 3-19-31,
 3-19-31,
 1-25-31,
 7-30-31

I wil get a value dynamically. Then I have to compare that value with the middle part of array.

9,
10,
10,
11,
17,
19,
19,
25,
30

If it's matching then I have to remove the whole part from array. For example. If I am getting a value dynamically is 19, then I wil match with that array. And 3-19-31 is there two times. So it will remove all 3-19-31. After exploding with "-".

How can I do this?

4

6 回答 6

2

You could use array_filter to get a new array.

$new_arr = array_filter($old_arr, function($var) use ($input) {
  $ret = explode('-', $var);
  return !(isset($ret[1]) && $ret[1] === $input);
});

Or use a normal loop and then use unset to remove the values.

for ($arr as $key => $value) {
  $ret = explode('-', $value);
  if (isset($ret[1]) && $ret[1] === $input) {
    unset($arr[$key]);
  }
}
于 2012-09-03T06:50:21.477 回答
2
foreach($array as $key=>$value){
    $parts = explode('-', $value);
    if($parts[1] == $search) {
        unset($array[$key]);
    }
}

Or if your search is an array

foreach($array as $key=>$value){
    $parts = explode('-', $value);
    if(in_array($parts[1], $search)) {
        unset($array[$key]);
    }
}
于 2012-09-03T06:56:25.067 回答
0

You can try this...

$arr; // your array
$value = 19;

foreach ($arr as $key=>$a)
{
    if(strpos($a, "-".$value."-") !== false)
        unset($arr[$key]);
}
于 2012-09-03T06:52:34.137 回答
0

There are few ways you can do this. If you are going to have only one digit always in the first eliment of your triplet, the following code should work;

$triplet_array = array(5-9-21, 5-10-22, 5-10-22, 5-11-23, 3-17-29, 3-19-31, 3-19-31, 1-25-31, 7-30-31);
$i = 0;
foreach($triplet_array as triplet){
  $middle = substring($triplet,2,0);
  if($middle == $my_dynamic_value) unset($triplet_array[$i]);
  $i++
}

but, if the first part is not going to contain only one digit always;

foreach($triplet_array as triplet){
  $this_triplet = explode('-',$triplet);
  if($this_triplet[1] == $my_dynamic_value) unset($triplet_array[$i]);
  $i++
}

hope this helps :-)

于 2012-09-03T06:54:23.553 回答
0

use this function, this will give you all the keys which are matched:

function custom_array_search($keyword,$array){
  if(!is_array($array)){
    return false;
  }
  $ret_keys = array();
  foreach($array as $key=>$value){
    if(strpos("-{$keyword}-",$value)!==false){
      $ret_keys[] = $key;
    }
  }
  return $ret_keys;
}

This function will give you all keys in an array.

Now you can delete those i.e. unset all keys from that array. :)

于 2012-09-03T06:55:10.720 回答
0
<?php
$arr = array('5-9-21', '5-10-22', '5-10-22', '5-11-23', '3-17-29', '3-19-31', '3-19-31', '1-25-31', '7-30-31');
$k = '10';
#print_r($arr);
foreach ($arr as $key => $value)
{
    $t = explode('-',$value);
    if($t[1] == $k)
    {
        unset($arr[$key]);
        #echo "deleted<br>";
    }
}
#print_r($arr);
?>
于 2012-09-03T06:55:52.417 回答