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?

3
  • Is this supposed to be a 2D array or is 5-9-21 an array element? Commented Sep 3, 2012 at 6:47
  • 5-9-21 and others are just one one element of array. Commented Sep 3, 2012 at 6:51
  • thankx everyone for ur answers... Commented Sep 3, 2012 at 7:21

6 Answers 6

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]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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]);
  }
}

Comments

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

Comments

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);
?>

Comments

0

You can try this...

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

foreach ($arr as $key=>$a)
{
    if(strpos($a, "-".$value."-") !== false)
        unset($arr[$key]);
}

1 Comment

is the 19 after $value a typo?
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 :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.