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. Link to comment https://forums.phpfreaks.com/topic/58618-solved-two-array-questions/ 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/58618-solved-two-array-questions/#findComment-290780 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 Link to comment https://forums.phpfreaks.com/topic/58618-solved-two-array-questions/#findComment-290802 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); Link to comment https://forums.phpfreaks.com/topic/58618-solved-two-array-questions/#findComment-290806 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.. Link to comment https://forums.phpfreaks.com/topic/58618-solved-two-array-questions/#findComment-290830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.