0

I have example those values:

$waiting['s9d8393838333'][] = array("client_id" => "2039833");
$waiting['s9d8393838333'][] = array("client_id" => "1039833");

How can i delete a client_id from the array? ex if i will delete client_id => 2039833 ?

foreach ( $waiting as $e => $array):
    if ( $array['client_id'] == $clientID):
         //Here i should delete that index ???
    endif;
endforeach;
4
  • Do you want to delete array("client_id" => 2039833") or "client_id" => 2039833 ? Commented Aug 21, 2013 at 18:03
  • can you show us the desired output of $waiting ? Commented Aug 21, 2013 at 18:06
  • codepad.viper-7.com/QcrzQh i want to delete the array where client_id is xxxxxx Commented Aug 21, 2013 at 18:15
  • @jesper codepad.viper-7.com/ut9UpS Commented Aug 21, 2013 at 18:18

2 Answers 2

2

That's what unset is for:

foreach ( $waiting['s9d8393838333'] as $key => $array ) {
    if ( $array['client_id'] == $clientID ) {
        unset( $waiting['s9d8393838333'][$key] );
        break;
    }
}

See it here in action: http://codepad.viper-7.com/6dmr3N

Sign up to request clarification or add additional context in comments.

7 Comments

Are you sure that would work? It doesn't make sence if it should.
@jesper - Check out the demo I posted.
As it is also the key of another array, you've to unset the key of the parent array to delete this field, and not simply unset it as you did in your demo, it's not the same case. I think the correct solution is the one given by Mic1780.
@C.Malet - The OP only said that they want to delete the client_id index from the $array array. They should clarify this.
@C.Malet Thanks for the reference. The reason I do it my way is because you are searching for the $clientID. the array is three deep so a double foreach loop + the key reference is needed to find the correct clientID.
|
1
foreach ($waiting as $key => $array) {
    foreach ($array as $key2 => $val) {
        if ($val['client_id'] == $clientID) {
            unset($waiting[$key][$key2]);
        }//END IF
    }//END FOREACH LOOP
}//END FOREACH LOOP

If you want to "keep the logic" as a complaint of someone, use this:

foreach ($waiting as $key => $array) {
    foreach ($array as $key2 => $val) {
        if ($val['client_id'] == $clientID) {
            unset($waiting[$key][$key2]);
        endif;
    endforeach;
endforeach;

With that you can change if you want more or less than just client_id

Working example: http://codepad.viper-7.com/ut9UpS

5 Comments

how does it change the logic? it does exactly as asked. the difference is the end braces.
You're checking if the name of the key is client_id, not if the value is $clientID...
Not working. Check codepad.viper-7.com/j3mVfa ... it doesnt delete the array.
@Mic1780 There's a difference between syntax and logic. I wasn't concerned about the difference in syntax (i.e. end braces), but you weren't comparing the correct things in the original answer.
@rink.attendant.6 I can understand that. The loop code in the question is wrong to start. so i fixed it and answered the question

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.