Array Value into a Variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Array Value into a Variable

Post by theoph »

I have an array called $select. The following script works just fine, however, I wanting to put the result into a variable instead of displaying it on a web page. How can i do this?

Code: Select all

<?php 
	$numbercount =count($select);
	for ($i=0; $i<$numbercount; $i++) {echo $select[$i]. ", ";}
?>
-Newbie Dave
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Code: Select all

<?php

	foreach($select as $key => $val) {
		$$key = $val;
	}
?>
... will turn every element of an associative array named "select" into a variable based on its key name.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Oh, I see. I didn't really read your post, did I?

Okay:

Code: Select all

<?

   $numbercount =count($select); 
   for ($i=0; $i<$numbercount; $i++) {
$array_data .= $select[$i]. ", ";
?>
That will append whatever the value of $select[$i] is to the end of everything else in $array_data so far, and then loop back and do it again. It will leave an extra (and possibly unwanted) comma at the very end, but you could fix this with a simple if ($i == $numbercount) inside the loop.
theoph
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:26 pm
Location: Lexington, KY USA

Post by theoph »

:D , I see that I need to have a concatenation tag after the variable.

Thanks much!
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

Code: Select all

<?
foreach($select as $value){
  $array_data .= $value. ", ";
}
$array_data = substr($array_data,0,-1);
?>
Unipus : this variation of what you said does it all automatically, and seems to me to be more efficient, though i'm not positive there's an difference
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Oh, yeah... that's a nice way to do it.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could simplify the code to:

Code: Select all

$csv_string = implode(',', $select);
then you don't need to loop through the array or remove a comma at the end.

Mac
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

i've never used implode, but that made me think, is it anything like join?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Exactly the same thing, just different name (from C).
Post Reply