If you know the position in the array can use the unset(); directly
unset($input[0]);
To be clearer:
$input = array("item 2", "item 4");
var_dump($input);
unset($input[0]);
var_dump($input);
gives:
array(2) {
[0]=>
string(6) "item 2"
[1]=>
string(6) "item 4"
}
array(1) {
[1]=>
string(6) "item 4"
}
If you don’t know the position in the array you have to go through the array or use array_search() first and then the unset()
$key = array_search('item 2', $input);
if($key!==false){
unset($input[$key]);
}
Example:
$input = array("item 2", "item 4");
$key = array_search('item 2', $input);
if($key!==false){
unset($input[$key]);
}
var_dump($input);
that gives
array(1) {
[1]=>
string(6) "item 4"
}
Your example using array_search worked for me. But when I was used with the array generated by Wordpress (get_the_category( $post->ID ), it continues printing the two categories.
– marcelo2605
@marcelo2605
get_the_category( $post->ID )
is the function that gives the value you want to remove? or is the $post that is your array?– Sergio
is the function that generates my array: $input = get_the_category( $post->ID );
– marcelo2605
@marcelo2605 what you get when you do
var_dump(get_the_category( $post->ID ))
?– Sergio
I found the answer here: http://wordpress.stackexchange.com/a/144617/38553 I added your if($key!== false){
– marcelo2605
@marcelo2605 this answer is basically the same as mine. I got it to work?
– Sergio
Yes, I did. Thank you.
– marcelo2605