Agum Posted July 5, 2007 Share Posted July 5, 2007 I have two questions regarding array. I would like to do the following in an efficient way. 1) I have two arrays. They contain ID numbers (unique). The first array has, say 100 elements. The second array has, say 20 elements. Every ID that appears in second array is also in the first array (guaranteed). What is the best way to search in the first array and remove every element that appears in the second array? 2) I have a multidimensional array/hash like this: arr[0]['id'] = 123; arr[0]['name'] = "John Doe"; arr[1]['id'] = 456; arr[1]['name'] = "Peter Smith"; ... I would like to "sort" this array based on the "name" part of the array. The id number needs to be kept with that particular name it comes with. What's the best way to do this? Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 5, 2007 Share Posted July 5, 2007 1 ) array_diff() 2 ) <?php function mysort($a, $b) { return strcmp($a['name'], $b['name']); } usort ($arr, 'mysort'); echo '<pre>', print_r($arr, true), '</pre>'; ?> Quote Link to comment Share on other sites More sharing options...
Agum Posted July 5, 2007 Author Share Posted July 5, 2007 Thanks Barand. One more quick question. What is the best way to concatenate an array into one string (comma separated values)? Just loop through it and do $str .= ... ? Or is there a function? Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted July 5, 2007 Share Posted July 5, 2007 $ar = array(1,2,3) echo join(',' , $ar); Quote Link to comment Share on other sites More sharing options...
Agum Posted July 5, 2007 Author Share Posted July 5, 2007 Thanks, always wanted to know those little things.. Quote Link to comment 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.