我有这个数组:
array(3) {
[0] = > array(4) {
[0] = > int(11)[1] = > string(14)"28/07/14 13:05" [2] = > string(8)"a" [3] = > string(15)"b"
}[1] = > array(4) {
[0] = > int(12)[1] = > string(14)"28/07/14 15:15" [2] = > string(7)"c" [3] = > string(6)"d"
}[2] = > array(4) {
[0] = > int(13)[1] = > string(14)"28/07/14 15:30" [2] = > string(8)"e" [3] = > string(8)"f"
}
}
来自PHP foreach语句,现在......我的问题非常简单:有可能(注意:为每个循环使用另一个)为数组的每一行添加一个[4]键和一个新值?为了获得这个:
array(3) {
[0] = > array(5) {
[0] = > int(11)[1] = > string(14)"28/07/14 13:05" [2] = > string(1)"a" [3] = > string(1)"b" [4] = > string(8)"NEWVALUE"
}[1] = > array(5) {
[0] = > int(12)[1] = > string(14)"28/07/14 15:15" [2] = > string(1)"c" [3] = > string(1)"d" [4] = > string(8)"NEWVALUE"
}[2] = > array(5) {
[0] = > int(13)[1] = > string(14)"28/07/14 15:30" [2] = > string(1)"e" [3] = > string(1)"f" [4] = > string(8)"NEWVALUE"
}
}
我希望你能理解,提前谢谢
编辑:可能我不清楚..我会发布我的PHP代码:
...
$xpath = new DOMXPath($document);
$expression = '(//table[@cellpadding="3"])[1]/tr[position() > 1]';
$rows = $xpath->query($expression);
$results = array();
$result = array();
foreach ($rows as $row) {
$td = $row->childNodes;
$data = $td->item(2)->nodeValue;
$immagine = $td->item(4)->firstChild->attributes->getNamedItem("src")->nodeValue;
$team1 = $td->item(10)->nodeValue;
$team2 = $td->item(14)->nodeValue;
$unixTime = time()*1000;
$result[0] = $unixTime;
$result[1] = $data;
$result[2] = $team1;
$result[3] = $team2;
$results[] = $result;
}
$expression1 = '(//table[@cellpadding="3"])[1]/tr[position() > 1]/td[19]/a[1]/@href';
$rows1 = $xpath->query($expression1);
$result = array();
foreach ($rows1 as $row) {
$result[4] = $row->value;
$results[] = $result;
}
var_dump($results);
感谢您的回答
编辑:我放了$ result = array();在foreach之前,但现在我有这个奇怪的结果:它打印了一个“130”项目数组,其中包含“65”元素,其中包含4个键和其他“65”项目,其中显示所有5个键!但前4个键没有循环!!
示例
第0到第64个元素:
[0]=> array(4) { [0]=> int(11) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c"
...
[64]=> array(4) { [0]=> int(74) [1]=> string(2) "a1" [2]=> string(2) "b1" [3]=> string(2) "c3"
第65至130个元素:
[65]=> array(5) { [0]=> int(11) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c" [4]=> string(36) "popup.asp?tp=2100&lang=en&idm=556296" }
...
[130]=> array(5) { [0]=> int(11) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c" [4]=> string(36) "popup.asp?tp=2100&lang=en&idm=452452" }
编辑:现在它显示一个65行数组,正确编号,但前4个没有循环!示例:
表示第0到第64个元素:
[0]=> array(4) { [0]=> int(11) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c" [4]=> string(36) "popup.asp?tp=2100&lang=en&idm=452452"
[1]=> array(4) { [0]=> int(11) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c" [4]=> string(36) "popup.asp?tp=2100&lang=en&idm=333352"
...
[65]=> array(4) { [0]=> int(11) [1]=> string(1) "a" [2]=> string(1) "b" [3]=> string(1) "c" [4]=> string(36) "popup.asp?tp=2100&lang=en&idm=999999"
答案 0 :(得分:2)
您应该使用&
,因为它只是引用,并且会在$array
中更改。
foreach($array as &$bit) {
$bit[] = "New value";
}
输出与输入$array
相同。
答案 1 :(得分:2)
您可以使用$array
中的原始foreach
,如下所示:
//$array our array
foreach($array as $key => $value){
$array[$key][] = 'NEW VAL';
//equivalent to use $value = 'NEW VAL' but it should be referenced
}
答案 2 :(得分:1)
是的,可以使用[]运算符。
您可以在括号中输入任意键,或者如果键是数字的,就像您的情况一样:simple将其留空以将数字计数增加1并添加1个元素。
foreach($array as $value) {
$value[] = "NEWVALUE";
}