keevitaja Posted November 9, 2009 Share Posted November 9, 2009 $test = array('yks', 'kaks', 'kolm'); $key = array_shift((array_keys($test, 'kolm'))); echo '<br>'; unset($test[$key]); print_r($test); is it possible to write this shorter? i need to eliminate the 'kolm' from $test array! actualy i have an array which contains objects Array ( [0] => Page Object ( [id] => 22 [language_id] => 1 [level] => 0 [order_number] => 1 [name] => 1 [body] => ) [1] => Page Object ( [id] => 23 [language_id] => 1 [level] => 1 [order_number] => 2 [name] => 2 [body] => ) ) i need to find the key where object Page->id has a value 23. can i do it? Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/ Share on other sites More sharing options...
ram4nd Posted November 9, 2009 Share Posted November 9, 2009 unset($test[2]); for searching use http://php.net/manual/en/function.in-array.php Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954080 Share on other sites More sharing options...
Adam Posted November 9, 2009 Share Posted November 9, 2009 You could use.. $test = array('yks', 'kolm', 'kaks'); $key = array_search('kolm', $test); unset($test[$key]); You'll need to check that $key !== false, as if it does currently it'll remove the element at key '0' (remember 0 == false, but 0 !== false). ram4nd, your suggestion would only work assuming he knows the key value of 'kolm'. Also in_array() only returns a bool value, so he wouldn't be able to use it to identify the key if he didn't. Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954082 Share on other sites More sharing options...
keevitaja Posted November 9, 2009 Author Share Posted November 9, 2009 i don't know the key of value 'kolm' Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954086 Share on other sites More sharing options...
Adam Posted November 9, 2009 Share Posted November 9, 2009 You could use.. $test = array('yks', 'kolm', 'kaks'); $key = array_search('kolm', $test); unset($test[$key]); Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954089 Share on other sites More sharing options...
keevitaja Posted November 9, 2009 Author Share Posted November 9, 2009 whatabout this object thing? Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954091 Share on other sites More sharing options...
keevitaja Posted November 9, 2009 Author Share Posted November 9, 2009 this how i do it now: foreach($pages_array as $page_object) { if($page_object->id != $page->id) { $pages[] = $page_object; } } where $pages_array is the array displayd in the first post and $page-id is the one i want to delete from array! Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954097 Share on other sites More sharing options...
Adam Posted November 9, 2009 Share Posted November 9, 2009 Give this a try.. foreach ($pages_array as $key => $page_object) { if ($page_object->id == $page->id) { unset($pages_array[$key]); } } Link to comment https://forums.phpfreaks.com/topic/180850-finding-and-unseting-an-element-in-array/#findComment-954100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.