1

I have an array which looks like

        $cclArray[0]['url']='test.html';
        $cclArray[0]['entity_id']=9;
        $cclArray[1]['url']='test1.html';
        $cclArray[1]['entity_id']=10;
        $cclArray[2]['url']='test2.html';
        $cclArray[2]['entity_id']=11;

if i would like to remove a specific array, for example if i want to delete the array element with 'url'='test1.html' .. how do i do it?

Thanks a lot for helping.

6 Answers 6

2
foreach ( $cclArray as $k => $array ) {
    if ( $array['url'] == 'test1.html' ) {
        unset($cclArray[$k]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you are using PHP >5.3 you can do it simply like this:

$cclArray = array_filter($cclArray, function ($var) {
    return $var['url'] != 'test1.html';
});

1 Comment

And with PHP < 5.3 you could do the same using create_function().
1

if you make your array this way

    $cclArray['test.html'] = 9;
    $cclArray['test1.html'] = 10;
    $cclArray['test2.html'] = 11;

it will be possible to delete with just

$url = 'test.html';
unset($cclArray[$url]);

Comments

0
for ($i=0;$i<count($cclArray);$i++)
  if($cclArray[$i]['url']=='test.html') unset($cclArray[$i]['url'])

3 Comments

Please don't give examples with count() in a for condition.
why not? you think, right part of this condition is being calculated on each iteration?
Correct me if I'm wrong, I'm always happy to refresh my knowledge. codeforest.net/php-mythbusters-count-inside-for-loop-is-slow
0
foreach($cclArray as $key=>$val){
  if($val['url'] == 'test1.html'){
   unset($cclArray[$key]);
  }
}

I think this is it.

1 Comment

unset($val[$key]) - key is a numeric value of keys in $cclArray (0, 1, 2). You are unseting right now $val[0] - not $cclArray[0]
0
for($i=0; $i<count($cclArray); $i++){
    if('test1.html' === $cclArray[$i]['url']){
        array_splice($cclArray, $i, 1);
    }
}

This is better but remember that unset() does not remove actual element it just sets it to null. If you want it to be complete gone, then after the unset() you should also do this: $cclArray = array_filter($cclArray);

Or you can do this in order to complete remove the array element instead of just unsetting it

for($i=0; $i<count($cclArray); $i++){
if('test1.html' === $cclArray[$i]['url']){
array_splice($cclArray, $i, 1);
}

}

3 Comments

Not sure you you mean. Inside the foreach look the $k is the value of $key of main array and $array is value of your nested array.
Have you tried it? I did, and my solution with array_splice worked as expected. Try it youself - it does completely remove the actual element fro array not from a copy.
Please don't give examples with count() in a for condition.

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.