这是我的代码的一部分
$try = "<a href='{$linkURL}'>{$className}</a> w/ {$staffName} {$startDateTime} - {$endDateTime}";
echo $try;
负责打印这样的数据(它只是数据的一部分,原始数据包含类似输出的长列表):
Trial Class w/ Class Cancelled 2015-03-17 19:30:00 - 2015-03-17 20:15:00
Private Class w/ Cheyenne 2015-03-17 18:30:00 - 2015-03-17 19:30:00
Trial Class w/ Speed 2015-03-17 19:30:00 - 2015-03-17 20:15:00
我想把这个数据放在一个数组中,所以我试过这样的事情:
$arr4 = array_merge(array($try));
echo '<pre>';
print_r($arr4);
echo '</pre>';
这开始以这种形式打印数据:
Array
(
[0] => Trial Class w/ Class Cancelled 2015-03-17 19:30:00 - 2015-03-17 20:15:00
)
Array
(
[0] => Private Class w/ Cheyenne 2015-03-24 18:30:00 - 2015-03-24 19:30:00
)
Array
(
[0] => Trial Class w/ Speed 2015-03-17 19:30:00 - 2015-03-17 20:15:00
)
它正在形成不同的数组,任何人都可以告诉我如何将数据存储在一个数组中,并得到这样的东西(可能接近它):
Array
(
[0] => Trial Class w/ Class Cancelled 2015-03-17 19:30:00 - 2015-03-17 20:15:00
[1] => Private Class w/ Cheyenne 2015-03-24 18:30:00 - 2015-03-24 19:30:00
[2] => Private Class w/ Speed 2015-03-24 18:30:00 - 2015-03-24 19:30:00
)
答案 0 :(得分:1)
我不明白这样做的原因:
$arr4 = array_merge(array($try));
当您希望将具有该字符串的单个数组作为值时,您将每个$ try字符串视为一个数组。
尝试使用array_push()函数并查看输出,这可能就是您所需要的。
//Initializing the array
$arr4 = array();
//and now for each $try string you want to store:
array_push($arr4, $try);
此致
答案 1 :(得分:1)
最简单的是:
$array = array();
// Your line logic might be
foreach ($lines as $line) {
// Add '$array[] =' before each line
$array[] = $try = "<a href='{$linkURL}'>{$className}</a> w/ {$staffName} {$startDateTime} - {$endDateTime}";
echo $try;
}
// when all lines are created you will have a single array with an entry per line
print_r($array);
$array[]
也会将该行添加为数组的新条目。
$array = array();
// Your line logic might be
foreach ($lines as $line) {
$try = "<a href='{$linkURL}'>{$className}</a> w/ {$staffName} {$startDateTime} - {$endDateTime}";
echo $try;
// Create multidimensional record
$lineForArray = array(
'linkUrl' => $linkURL,
'className' => $className,
'staffName' => $staffName,
'startDateTime' => $startDateTime,
'endDateTime' => $endDateTime
);
$array[] = $lineForArray;
}
// when all lines are created you will have a single array with an entry per line
print_r($array);