1

I have a function that returns an array from the database from which I have to display some data into the view.

If I do

print "<pre>";
print_r($names);
exit();

on the variable that stores the data it returns this:

Array
(
[0] => Array
        (
            [nume] => Eugen
        )
[1] => Array
        (
            [nume] => Roxanescu
        )
[2] => Array
        (
            [nume] => Georgiana
        )
[3] => Array
        (
            [nume] => Andrei
        )
)

I can't change the function I talk about in any way, I need to store each name in a different variable. At the end it should look like this:

name1 = "Eugen"
name2 = "Roxanescu"
name3 = "Georgiana"
name4 = "Andrei"

Thank you very much!

6
  • 1
    Have you tried $name1 = $names[0]['nume']; and so on? Commented Aug 12, 2015 at 14:26
  • So this array is a result of a database query correct? how are you making that query? can you provide that code? also, can you provide the structure of your db table? Commented Aug 12, 2015 at 14:29
  • Why would you want to store the names in different variables? I think you are trying work harder then you should. consider iterating through your results array to access the names. Commented Aug 12, 2015 at 14:31
  • The query looks like this: "SELECT nume FROM rummy_users LEFT JOIN rummy_games ON rummy_users.id_user=rummy_games.id_user WHERE rummy_games.id_joc=".$id_joc.". Commented Aug 12, 2015 at 14:34
  • I only have to display 4 each time so it is a lot simpler for me as I am a beginner. Any tips are welcome but I'm not sure I'm able to make an iteration yet. Commented Aug 12, 2015 at 14:35

4 Answers 4

1

You could assign a variable the data of your array;

<?php
    $name1=$names[0];
    $name2=$names[1];
    $name3=$names[2];
    $name4=$names[3];

    echo $name1." ".$name2." ".$name3." ".$name4;
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Haha thanks! I have never thought of this way but now that I see it I realize I should've known it. Thanks!
1

Note entirely sure why you want to do this... But here's a function that should do what you're looking for.

This answer uses dynamic variable assignment, creating a new variable with a string ( $"name1", $"name2", etc ).

for($i = 0; $i < count($names); $i++) {
  $var_name = "name".$i;
  $$var_name = $names[$i]['nume'];
}

var_dump(get_defined_vars());

$name1, $name2, $name3 ... < count($names)

2 Comments

The purpose is an application to keep the score record while you play rummy (if you know the game, something like whist or rentz or something simmilar). You have to select the players from a dropdown list and in this application there are always 4 players so I have no reason to complicate the code. Still, thank you very much!
@VladEugenNitu, this is the right answer for what you've asked and the right way to code. Learn how to make code reusable and parametric as soon as possible, you won't regret it!
0
$result = array();
array_walk($names, function ($value, $key) use (&$result) {
    $result[ 'name' . $key ] = $value['name'];
});
var_dump($result);

1 Comment

Even this is a simple question, please add some contextual comments in order to help others that have beginner skills.
0

I suggest you use an array and reference to each element of it as a variable, if it's really necessary.

$result = array();
foreach ($names as $n) {
    array_push($result, $n['nume']);
    echo "name".array_search($n['nume'], $result)." = ".$n['nume'];
}

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.