从几个创建一个PHP变量

时间:2011-09-26 12:34:22

标签: php

基本问题但是我已经搜索过并且不能完全固定 - 如何从其他几个创建1个变量?然后我想把它变成一个数组。

这样的事情(显然语法错了!):

$name;
$address;
$post_code;

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

我的php非常基本,所以请轻松搞定!

10 个答案:

答案 0 :(得分:8)

您可以采用以下几种方式:

// 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;

或者如果你想将它们作为一个数组:

// 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  

答案 1 :(得分:4)

由于没人提及,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"
}
*/

答案 2 :(得分:3)

你只是想简单地追加几个字符串吗?

如果是这样,正确的语法是:

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

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

...使用.(点)到concatentate他们。您可能希望在它们之间插入换行符,逗号等:

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

或者,您可以在一个新字符串中指定它们,如下所示:

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

...注意使用双打引号而不是单引号。 read this可能对你有好处。

或者,您可以将它们存储为array中的单独值,如下所示:

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

......或者这个:

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

...您可以将该数组转换为implode()的字符串:

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

答案 3 :(得分:2)

对于一个简单的数组:

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

对于关联数组:

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

答案 4 :(得分:2)

你想连接字符串吗?您应该使用字符串连接运算符“”。 例如:

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

答案 5 :(得分:1)

你的意思是这个基本或你希望如何完成:

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

答案 6 :(得分:1)

您尝试做的事情称为字符串连接,这是在PHP中使用. - 运算符

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

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

答案 7 :(得分:1)

你几乎就在那里。

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

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

答案 8 :(得分:1)

如果你想连接名称地址和邮政编码,你可以这样做:

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

如果你想要的是将每个字段添加到地址数组,你可以这样做:

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

答案 9 :(得分:0)

您可以查看标准对象:

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

或者自己创建一个Address类,它在你的应用程序中非常有用。

class Address {

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