Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
Is this supposed to be a 2D array or is 5-9-21 an array element? –  Havelock Sep 3 '12 at 6:47
 
5-9-21 and others are just one one element of array. –  user1638279 Sep 3 '12 at 6:51
 
thankx everyone for ur answers... –  user1638279 Sep 3 '12 at 7:21
 
You can now accept an answer if it worked for you. –  Havelock Sep 3 '12 at 7:32
add comment

6 Answers

up vote 1 down vote accepted
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]);
    }
}
share|improve this answer
add comment

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]);
  }
}
share|improve this answer
add comment

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. :)

share|improve this answer
add comment
<?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);
?>
share|improve this answer
add comment

You can try this...

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

foreach ($arr as $key=>$a)
{
    if(strpos($a, "-".$value."-") !== false)
        unset($arr[$key]);
}
share|improve this answer
 
is the 19 after $value a typo? –  tigrang Sep 3 '12 at 6:53
add comment

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 :-)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.