0

I have a foreach loop which prints out the usernames of all entries in a database table like so:

foreach($results as $result){

echo $result->username;
echo '<br>';

}

As more and more users are added to the table, the list will grow. I would like to take the complete list and assign each entry to a separate PHP variable for use later on the PHP script. What is the best way to do this?

5
  • You mean like $results[0], $results[1], $results[2] and so on? Commented Apr 10, 2014 at 21:10
  • 1
    you have a list and it will grow. the best way to do this is with an array like you already have. why would you want to have separate vars for each? Commented Apr 10, 2014 at 21:10
  • 1
    no. you don't want a separate variable. Consider when you reach 50,000 users. Do you really want 50,000 separate variables? Hell no. You've already got them in a nice neat and clean array. Keep them there. Commented Apr 10, 2014 at 21:11
  • You are correct that I have them in an array, so I guess my mis-understanding has to do with how I can use those array values later in the script. I am trying to apply each username as a javascript variable. I was thinking that I would need to pull them out individual, but now with these comments I will reconsider this based upon the pre-existing array. Thanks. Commented Apr 10, 2014 at 21:20
  • Will your javascript need access to every username in the table, or just a few? If just a few, you can consider using ajax to get a username. For example if you're checking to see if a username exists, you can write a script like "checkUsername.php" which queries the DB for a username (provided as a POST variable), and outputs "true" or "false". Then use ajax to post the username to checkUsername.php and check the output. Commented Apr 10, 2014 at 21:25

6 Answers 6

2

You don't want individual variables, try an array:

foreach($results as $result){
    $names[] = $result->username;
}

In PHP 7 it's even easier:

$names = array_column($results, null, 'username');
Sign up to request clarification or add additional context in comments.

Comments

0

Based on your comments I am trying to apply each username as a javascript variable, all you need seems to be something like:

<script type="text/javascript">
  var usernames = <?php echo json_encode($results); ?>;
</script>

Of course if you only need one element from the $results array, you should first generate a reduced list using one of the other answers; if you use the whole $results array, all information in there will be sent to the browser / visible to the visitor.

Comments

0

Put them in an array:

$names = array();
foreach ($results as $result) {
    $names[] = $result->username;
}

Comments

0

foreach loops are soooo out of fashion :p

$names = array_map(function($r) {return $r->username;},$results);

Comments

0

I don't know why you want too.... but you could do ...

foreach($results as $result)
{
    $$result->username = $result;
 }

That answers your question. But why anyone would do that is beyond me

Comments

-1

I use below code

<?php

$string = 'a|b|c|d|e|f';

$tags = explode('|' , $string);


foreach($tags as $i =>$key) {
$i >0;
     $a[$i]=$key .'</br>';

}

echo $a[3];

good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.