dongshan7060 2014-07-29 11:33
浏览 169

在php中循环添加一个数组键

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"
  • 写回答

3条回答 默认 最新

  • doujing5846 2014-07-29 11:36
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况