0

I have array

[Company] => Demo Company 1
[First Name] => Test
[Last Name] => Lead 1
[Designation] => This is testing title 1
[Email] => [email protected]
[Phone] => 242377

I used extract() function so all the index values will become variable names, I also used {} as there are spaces in variable names. But i dont know why its not working :( This ${'First Name'} returns blank...below is my code

foreach($vals as $value){
extract($value);
echo '<tr><td><a href="edit.php?id='.$LEADID.'">'.${'First Name'}.' '.${"Last Name"}.'</a></td><td>'.$Company.'</td><td>'.$Phone.'</td><td>'.$Email.'</td></tr>';
}

3 Answers 3

6

Variable names cannot contain spaces. For reference, read the manual on variables:

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'

If I were you, I'd just go with a shorter name on the array, so instead of $value use $v or similar. You can also use printf to make the code more readable:

foreach($vals as $v) {
  printf('<tr><td><a href="edit.php?id=%d">%s %s</a></td><td>%s</td><td>%s</td><t\
d>%s</td></tr>',
         $LEADID,
         $v['First Name'],
         $v['Last Name'],
         $v['Company'],
         $v['Phone'],
         $v['Email']);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Because of this extract won't make variables with invalid names.
Yes u r right, but PHP also allows spaces in variable name and we can access it using {} around it blog.riff.org/2008_05_11_spaces_php_variable_names....but dont know why its not working here in my code
Amit, that's an undocumented feature so it may just as well be a bug. It's never good to rely on undocumented behavior. I usually avoid spaces even in array keys just because it may bite me in the future (spaces is not allowed as element names in HTML, not in MySQL column names, and so on...)
@AmitPatil: I would suggest that even if you have found a trick to use variable names with spaces that this is extremely bad practice. Obviously it is not enforced everywhere. If you really need a visual separator use the underscore character instead.
I would not rely on extract in the first place. Sometimes you will have to import key value pairs from external data sources like csv files and this will break your code if you use extract.
1

I think you should remove the spaces before using extract:

$keys = str_replace( ' ', '', array_keys($vals));
$values = array_values($vals);
$vals = array_combine($keys, $values);

Then, after extract you'd have variables like $FirstName.

Comments

0

In this case you do not really need extract. You can use array indexes in interpolated strings.

$sample = array('name' => 'frank, 'age' => 42);
echo "{$sample['name']} is {$sample['age']} years old.";

If you can avoid using ${'strange names'} you should do it. If yo REALLY need to do it this way: Please update your question and tell us HOW $value is defined. Is vals The array from your first code block?

1 Comment

Its coming from xml file, m accessing some API and converting it to array.

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.