chomedey Posted April 30, 2010 Share Posted April 30, 2010 Does anyone know how I would reformat this array so I just have the numeric strings, without quotes, separated by commas? I know I need implode, but because the strings are arrays within an array the $formatted_friends=implode(",",$friends); isn't working. Cheers. J array(218) { [0]=> array(1) { ["id"]=> string(7) "5244442" } [1]=> array(1) { ["id"]=> string( "13600096" } [2]=> array(1) { ["id"]=> string( "13601927" } [3]=> array(1) { ["id"]=> string( "13602843" } [4]=> array(1) { ["id"]=> string( "13603150" } [5]=> array(1) { ["id"]=> string( "13604526" } [6]=> array(1) { ["id"]=> string( "13608213" } [7]=> array(1) { ["id"]=> string( "13610311" } [8]=> array(1) { ["id"]=> string( "13612969" } [9]=> array(1) { ["id"]=> string( "13618723" } [10]=> array(1) { ["id"]=> string( "13621124" } [11]=> array(1) { ["id"]=> string( "13621381" } [12]=> array(1) { ["id"]=> string( "13801292" } [13]=> array(1) { ["id"]=> string( "28107584" } [14]=> array(1) { ["id"]=> string( "28110007" } [15]=> array(1) { ["id"]=> string( "28125641" } [16]=> array(1) { ["id"]=> string( "28127538" } [17]=> array(1) { ["id"]=> string(9) "500809095" } [18]=> array(1) { ["id"]=> string(9) "500877775" } [19]=> array(1) { ["id"]=> string(9) "501384573" } } Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/ Share on other sites More sharing options...
andrewgauger Posted April 30, 2010 Share Posted April 30, 2010 function implodeIds($arrImplode, $index='id', $separator=",") { $string =""; foreach ($arrImplode as $key => $val) { $string .= $val[$index].$separator; } return $string; } For your example you only need to call it with your variable, and you can customize it for a different index ('id') or a different separator. Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1050862 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 Works great. Thanks very much. The query I'm putting it in still isn't working, though, but I think it's a different problem. SELECT *, DATE_FORMAT(date_entered, '%e %M, %Y') as newdate FROM data WHERE deleted='N' AND userID IN (5244442,13600096,13601927,13602843,13603150,13604526,13608213,13610311,13612969,13618723,13621124,13621381,13801292,28107584,28110007,28125641,28127538,500809095,500877775,) ORDER BY date_entered DESC LIMIT 0, 10 Does anything strike you as wrong about that? Cheers. Julian Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051022 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 Could it be the comma at the end of the list of userIDs? Or is there a limit to the number of comma-separated values in a WHERE IN clause (I have edited the query here - it is much longer in reality)? Cheers. Julian Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051024 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 Yes, it's the comma at the end of the array that's doing it. Any idea of how to get rid of it with your function? Cheers. Julian Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051026 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 $csv = ''; foreach ($friends as $key => $value) { if (array_key_exists('id', $value)) $csv .= sprintf('%d,', $value['id']); } $csv = rtrim(','); Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051033 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 Sorry - not sure how to implement this. Is this to replace the function as is, or in addition to? J Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051042 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 The implementation is up to you. But I suggest you try to understand what I did rather than copy and paste. If you understood the concept, you wouldn't have that question. Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051052 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 Exactly - I don't understand what you did, but would very much like to. What is the variable $csv? And I have no idea what this is? sprintf('%d,' ... The rtrim is, I guess, a reverse trim to get rid of the end comma? Any explanation much appreciated. Cheers. Julian Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051058 Share on other sites More sharing options...
Ken2k7 Posted April 30, 2010 Share Posted April 30, 2010 1. $csv is just a temp variable to store value. There's nothing special about $csv. 2. php.net is a great site. Please use it for any functions you don't understand. sprintf. It'll give you a much better explanation than what I can do. Plus, I don't like to repeat something that's already written somewhere. I'm a lazy typer. 3. Again, php.net - rtrim Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051060 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 Fair enough. Thanks. Julian Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051094 Share on other sites More sharing options...
chomedey Posted April 30, 2010 Author Share Posted April 30, 2010 All I needed was to add $string = rtrim($string,','); at the end of the function. Not sure what all the other stuff was for. Many thanks nonetheless. Julian Quote Link to comment https://forums.phpfreaks.com/topic/200246-reformatting-an-array/#findComment-1051101 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.