-3

我有:

$title1 = "aaa1";
$body1 = "bbb1";
$author1 = "ccc1";

$title2 = "aaa2";
$body2 = "bbb2";
$author2 = "ccc2";

$title3 = "aaa3";
$body3 = "bbb3";
$author3 = "ccc3";

如何将这些变量分配给数组?接下来我想做:

foreach($array as $a){
   $a[title] . $a['body']. $a['author'] . "<br />";
}

这应该告诉我:

aaa1bbb1ccc1
aaa2bbb2ccc2
aaa3bbb3ccc3
4

5 回答 5

1
$array[] = array("title" => "aaa1", "body" => "bbbb1");
$array[] = array("title" => "aaa2", "body" => "bbbb2");
于 2012-05-16T11:06:59.137 回答
1

很简单,只需在数组中创建一个数组。

<?php
$array = array(
        array('title' => 'aaa1', 'body' => 'bbb1', 'author' => 'ccc1'),
        array('title' => 'aaa2', 'body' => 'bbb2', 'author' => 'ccc2'),
        array('title' => 'aaa3', 'body' => 'bbb3', 'author' => 'ccc3'),
    );

只需阅读有关数组的 PHP 文档:http: //php.net/manual/en/language.types.array.php

于 2012-05-16T11:07:53.447 回答
0

基本上

可以使用 array() 语言结构创建数组。它采用任意数量的逗号分隔键 => 值对作为参数。

array(
key  => value,
key2 => value2,
key3 => value3,
...

)

例子:

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

更多信息http://php.net/manual/en/language.types.array.php

于 2012-05-16T11:09:48.017 回答
0
$array = array(    
     0 => array('title' => 'aaa1', 'body => 'bbb1','author' => 'ccc1'),    
     1 => array('title' => 'aaa2', 'body => 'bbb2','author' => 'ccc2'),    
     2 => array('title' => 'aaa3', 'body => 'bbb3','author' => 'ccc3')
);

或使用list创建数组

于 2012-05-16T11:10:32.080 回答
0

你会这样做:

<?php
$array = array(
        array('title' => 'aaa1', 'body' => 'bbb1', 'author' => 'ccc1'),
        array('title' => 'aaa2', 'body' => 'bbb2', 'author' => 'ccc2'),
        array('title' => 'aaa3', 'body' => 'bbb3', 'author' => 'ccc3'),
    );
?>

然后你会像这样打印你想要的方式:

<?php
foreach($array as $item) {
    print($item['title'].$item['body'].$item['author']."<br>");
}
?>
于 2012-05-16T11:19:59.707 回答