开发者

Create one php variable from several

Basic question but I've searched and can't quite get this nai开发者_开发技巧led - how can I create 1 variable from several others? I then want to get that into an array.

Something like this (obviously the syntax is wrong!):

$name;
$address;
$post_code;

$full_address = $name, $address, $post_code;
$address[] = $full_address;

My php is very basic so go easy on the abuse!


You can do it several ways:

// using sprintf
$address[] = sprintf('%s, %s, %s', $name, $address, $post_code);

// using "Traditional" concatenation
$address[] = "$name, $address, $post_code";

// other version of concatenation
$address[] = $name . ", " . $address . ", " . $post_code;

Or if you want them as an array:

// regular array
$address[] = array($name, $address, $post_code);
// $address[n][0] = name
// $address[n][1] = address
// $address[n][2] = post_code

// keyed array
$address[] = array(
  'name' => $name,
  'address' => $address,
  'post_code' => $post_code
);
// $address[n]['name'] = name
// $address[n]['address'] = address
// $address[n]['post_code'] = post_code  


Since noone else mentioned it, compact()

$name = 'Mike';
$address = '123 Main St';
$post_code = '55555';

$full_address = compact('name', 'address', 'post_code');
var_dump($full_address);

/* Output:
array(3) {
  ["name"]=>
  string(4) "Mike"
  ["address"]=>
  string(11) "123 Main St"
  ["post_code"]=>
  string(5) "55555"
}
*/


Are you simply wanting to append several strings to each other?

If so the correct syntax is this:

$name = 'Dave Random';
$address = 'Some place, some other place';
$post_code = 'AB123CD';

$merged = $name.$address.$post_code;

...using . (dot) to concatentate them. You will probably want to insert a line break, comma etc between them:

$merged = $name.', '.$address.', '.$post_code;

Alternatively, you can specify them all in one new string like this:

$merged = "$name, $address, $post_code";

...note the use of doubles quotes instead of single. It would probably do you well to read this.

Alternatively, you can store them as separate values in an array like this:

$myArray = array();
$myArray['name'] = 'Dave Random';
$myArray['address'] = 'Some place, some other place';
$myArray['post_code'] = 'AB123CD';

...or this:

$myArray = array(
  'name' => 'Dave Random',
  'address' => 'Some place, some other place',
  'post_code' => 'AB123CD'
);

...and your can convert that array to a string with implode():

$merged = implode(', ',$myArray);


For a simple array:

$full_address = array($name, $address, $post_code);

For an associative array:

$full_address = array('name'=>$name, 'address'=>$address, 'zip'=>$post_code);


Do you want to concatenate strings? You should use string concatenation operator ".". For example:

$s1 = "foo";
$s2 = "bar";
$s3 = "blabla";
$final_string = $s1 . " " . $s2 . $s3;
echo $final_string; // output: "foo barblabla"


Do you mean this basic or how you want it to be done:

$separator = " ";
$full_address = "{$name}{$separator}{$address}{$separator}{$postcode}";
$address[] = $full_address;


What you are trying to do is called string concatenation and this is done in PHP with the .-Operator

$name = "Foo";
$address = "Barstreet 1";
$post_code = "12345 Foobar";

$full_address = $name . ", " . $address . ", " . $post_code


You're pretty much almost there.

$name = 'Your name';
$address = 'Your address';
$post_code = 'Your post code';

$full_address = "$name, $address, $post_code";
$address[] = $full_address;


if you want to concatenate name adress and postcode you can do:

$address[] = $name.$address.$post_code;
or 
$address[] = "$name $address $post_code";

if what you want is to add each of the field to the adress array you can do this as :

$address = array($name,$address,$post_code);
or 
$address[] = $name;
$address[] = $address;
$address[] = $post_code;


You could take a look at standard object:

$address = new stdClass;
$address->postcode = '02115';
$address->name = "John Connor';

or create an Address class by itself, it will be quite useful in your app.

class Address {

     public function __construct ($name, $address) {...}
     public function getFullAddress() {...} // or other useful functions
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜