Reputation: 183
I have this array:
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"
}
}
Coming from a PHP foreach statement, now.. my question is very simple: it is possible (Notice: using another for each loop) to add for each rows of the array a [4] key and a new value? In order to obtain this:
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"
}
}
I hope you understand, thanks in advance
EDIT: Probably i'm not clear.. i will post my php code:
...
$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);
Thanks for these answers
EDIT: I have put $result = array(); before foreach, but now i have this strange result: it is printed a "130" items array which consists on "65" elements that contained the 4 keys and after other "65" items in which are displayed all the 5 keys! But the first 4 keys are not looped!!
Example
for 0th to 64th element:
[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"
for 65th to 130th element:
[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" }
EDIT: Now it is displayed a 65 rows array, correctly number, but the first 4 are not looped! Example:
for 0th to 64th elements:
[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"
Upvotes: 2
Views: 93
Reputation: 3552
You can use the original $array
in the foreach
like this:
//$array our array
foreach($array as $key => $value){
$array[$key][] = 'NEW VAL';
//equivalent to use $value = 'NEW VAL' but it should be referenced
}
Upvotes: 2
Reputation: 2783
Yes this is possible using the [] operator.
You can enter any key into the brackets, or if the keys are numeric like in your case: simple leave it empty to increment the numeric count by 1 and add 1 element.
foreach($array as $value) {
$value[] = "NEWVALUE";
}
Upvotes: 1
Reputation: 2364
You should use &
because it is only refference and it will be changed in $array
so.
foreach($array as &$bit) {
$bit[] = "New value";
}
The output is same variable as input $array
.
Upvotes: 2