我正在构建一个自动电子邮件系统,作为我设计的会员系统的扩展。
采取以下数组:
array (size=5)
'002858' => string '<p>These members have expired:</p><p>Adults: None</p><p>Children: Florence Carline</p><p>Disabled Members: None</p>' (length=115)
'001974' => string '<p>These members have expired:</p><p>Adults: Kelly Hughes, Andrew Hughes</p><p>Children: Alfie Hughes</p><p>Disabled Members: None</p>' (length=134)
'000033' => string '<p>These members have expired:</p><p>Adults: Kate Shirley</p><p>Children: Rolsin Shirley</p><p>Disabled Members: None</p>' (length=121)
'000259' => string '<p>These members have expired:</p><p>Adults: Charmaine Walsh, Michael Baxter</p><p>Children: Liam Baxter, George Baxter</p><p>Disabled Members: None</p>' (length=152)
'000000' => string '<p>These members are expiring in a month:</p><p>Adults: Edward Selby</p><p>Children: None</p><p>Disabled Members: None</p>' (length=122)
当我尝试在foreach循环中读取数组时(每封电子邮件一次迭代),我收到以下错误:
Notice: Uninitialized string offset: 1974 in C:\wamp\www\subdomains\[host]\controllers\email.class.php on line 145
请注意,这是我的数组中的第二个,但没有'00'。
我看过这个:php array behaving strangely with key value 07 & 08
所以我检查了(使用is_int()) - 键是一个非int的字符串。
有人能为我解释一下吗?
编辑:
创作:
foreach($email_list_array['month'] as $key => $ticket) {
$header[$key] = "<p>Our records indicate that your pass (ticket number: $key) is expiring or has already expired.</p>";
$header[$key] .= "<p>Please see the details below</p>";
if(!isset($message[$key])) {
$message[$key] = "<p>These members are expiring in a month:</p>";
} else {
$message[$key] .= "<p>These members are expiring in a month:</p>";
}
$message[$key] .= "<p>Adults: $adults</p>";
$message[$key] .= "<p>Children: $children</p>";
$message[$key] .= "<p>Disabled Members: $disabled</p>";
}
这是我进行阵列转储的地方
var_dump($message);
测试输出:
foreach($message as $key => $value) {
$message_header = $header[$key];
$message = $message[$key];
echo $message_header;
echo $message;
echo "<br>";
}
答案 0 :(得分:2)
在foreach中更改变量$ message的名称:
$messageXXX = $message[$key];
echo $messageXXX;
答案 1 :(得分:1)
我猜这个数组是作为整数访问而不是字符串。所以你应该将整数转换为字符串。
请注意,整数访问和字符串访问之间存在差异!
$arrayX = array( "A", "X" );
$arrayY = array( "0012"=> "A", "0001"=>"b" );
print_r( $arrayX );
print_r( $arrayY);
echo "array X ". $arrayX[1]; // returns "X"
echo "array Y".$arrayY["0012"]; // returns "A"
echo "array Y ".$arrayY[12]; // should also return "A"
输出如下
Array
(
[0] => A
[1] => X
)
Array
(
[0012] => A
[0001] => b
)
XA
Notice: Undefined offset: 12 in /test.php on line 14
在您的情况下,您还需要添加类似
的前导零$value = 12; // integer
$str = sprintf("%04d",$value); // now $str = "0012"
echo "array Y ".$arrayY[ $str ]; // now returns "A"
答案 2 :(得分:1)
您的部分问题是您在循环中为$message
分配了值,但$message
是您的输入变量。这使得PHP正在做它正在做的事情。您似乎也试图从两个不同的阵列中获取相同的密钥。这是对的吗?
无论如何看看以下内容:
foreach($message as $key => $value) {
$message_header = $header[$key];
$messageText = $message[$key];
echo $message_header;
echo $messageText;
echo "<br>";
}
这有望显示您在测试中需要进行哪些更改以阻止错误发生。