Jump to content

Reformatting an array


chomedey

Recommended Posts

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" } }

Link to comment
https://forums.phpfreaks.com/topic/200246-reformatting-an-array/
Share on other sites

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.

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

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

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. :P I'm a lazy typer.

 

3. Again, php.net - rtrim

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.