jolyon_russ Posted August 12, 2007 Share Posted August 12, 2007 I should probably be posting this in the OOP section but my problem is only half OOP related. I have an array that contain objects, I want to be able to test against the id of that object and remove it from the array. In it's simplest for I want to go from this: $people = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo'); ...to this... $people = array('Tom', 'Dick', 'Brenda', 'Jo'); The fact that the items in the array are objects should not matter right? Plus I've already written my loops and conditionals to check which item i need to remove. function removeItem ( $item ) { $len = count ( $_SESSION['basket_arr'] ) ; if ( $len == 1 ) { emptyBasket ( ) ; } else { for ( $i = 0 ; $i < $len ; $i++ ) { if ( $item->id == $_SESSION['basket_arr'][$i]->id ) { //$_SESSION['basket_arr'][$i] must go } } } } Thanks in advance for your help. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/ Share on other sites More sharing options...
cooldude832 Posted August 12, 2007 Share Posted August 12, 2007 I think you can use unset to delete a single array node similar to how you do with a variable, and what you could do is simply find the key based on the value (Read up on array functions to get it exactly) and then unset($array[$deltekey]) or if it sa bunch of them say foreach($deletekey as $value) Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-321720 Share on other sites More sharing options...
Dragen Posted August 12, 2007 Share Posted August 12, 2007 can't you simply unset the array variable? unset($people[$id]) Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-321721 Share on other sites More sharing options...
cooldude832 Posted August 12, 2007 Share Posted August 12, 2007 yes, but the problem is in this example the deletion is not based off the key, but based off a value (the name of a person) there for you will need something that returns all the keys with that given value Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-321726 Share on other sites More sharing options...
Dragen Posted August 12, 2007 Share Posted August 12, 2007 $key = array_search($value); unset($people[$key]); Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-321731 Share on other sites More sharing options...
cooldude832 Posted August 12, 2007 Share Posted August 12, 2007 there you go that was the function i described. However be careful if you want to delete all entries or only 1 of hte given value Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-321735 Share on other sites More sharing options...
jolyon_russ Posted August 13, 2007 Author Share Posted August 13, 2007 Cheers guys, I did try unset already, but it screwed with my indexing: $people = array([0] => 'Tom', [1] => 'Dick', [2] => 'Harriet', [3] => 'Brenda', [4] => 'Jo'); became... $people = array([0] => 'Tom', [1] => 'Dick', [3] => 'Brenda', [4] => 'Jo'); If I do use unset is there a way that I can then, re-index the array to fill the gap? I know that array_splice does this automatically unless specified. Thanks again for the help. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-322167 Share on other sites More sharing options...
Barand Posted August 13, 2007 Share Posted August 13, 2007 <?php $people = array(0 => 'Tom', 1 => 'Dick', 2 => 'Harriet', 3 => 'Brenda', 4 => 'Jo'); unset ($people[2]); $people = array_values($people); echo '<pre>', print_r($people, true), '</pre>'; ?> --> Array ( [0] => Tom [1] => Dick [2] => Brenda [3] => Jo ) Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-322220 Share on other sites More sharing options...
jolyon_russ Posted August 13, 2007 Author Share Posted August 13, 2007 Thanks barand, from what I can gather the array_values function reorders the array to fill any gaps, wicked! You've all be a great help, thank again. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/64541-deleting-a-single-item-from-the-middle-of-an-array/#findComment-322331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.