3

I need some help with the foreach loop. Say I have this method:

public function showPaymentDetails() {

    $data = array("name"             => $this->name,
                  "lastname"         => $this->lastname,
                  "email"            => $this->email, 
                  "company"          => $this->company,
                  "event_name"       => $this->event_name,
                  "event_price"      => $this->event_price,
                  "uniqid"           => $this->uniqid,
                  "mobile"           => $this->mobile,
                  "travel"           => $this->travel,
                  "job"              => $this->job,
                  "how_hear"         => $this->how_hear,
                  "geusts"           => $this->guests,
                  "slider_network"   => $this->slider_network,
                  "slider_invest"    => $this->slider_invest,
                  "slider_investors" => $this->slider_investors,
                  "gender"           => $this->gender
                  );

    return $data;

}

Now i want to assign each value of an array to a variable. This is what I do:

$i = 0;
foreach($data as $row[$i]) {
    $row[$i];
    $i++;
}

echo $row[0];
echo $row[1];
echo $row[2];

As you can see I am assigning values to $row[]. Which is really bad. How can I assign those values to such variable names as $name, $lastname etc.

Thanks!

4
  • Perhaps you should describe what you want to do ... sounds incorrect anyway. Commented Mar 5, 2014 at 20:29
  • 1
    extract($data);? php.net/extract Why do you want to do this? Commented Mar 5, 2014 at 20:29
  • 4
    Why assign them to variables? You already have an array with named keys. That's more efficient than separate vars. Commented Mar 5, 2014 at 20:29
  • What is $data as $row[$i] supposed to do? Maybe you just want $row = array_values($data);? Commented Mar 5, 2014 at 20:30

4 Answers 4

3

I think you don't need to loop in the array, you can just assign variables using your array and scopes with keys

$data =  showPaymentDetails();
$name = $data['name'];
$lastname = $data['lastname'];
//and so on
Sign up to request clarification or add additional context in comments.

Comments

2

You could do

foreach($data as $key => $value) {
    $$key = $value;
}

that would then turn $this->name into $name. However this is very bad practice. You are better of just accessing the variables like

$data['name'];
$data['lastname'];
//Etc

1 Comment

By the way you are missing a paranthesy in your foreach
1

Variable variables is what you described, however I would just use the $data array and not assign to individual variables:

foreach($data as $key => $value) {
    $$key = $value;
}

Also, extract($data); is the same result.

A closer look at your code has me wondering, why in the world you are building an array in a class function that is comprised of already existing object vars just to transform them into standalone vars? Bad design so far.

Comments

0

You can extract them within the current scope:

extract(showPaymentDetails());

echo $lastname;

See also: extract()

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.