0

これは私の配列で、2番目の配列のようにするためにいくつかの要素を追加したい

1.)

array(7) {
  [0] => array(3) {
    ["name"] => string(9) "airG Chat"
    ["product_id"] => int(2469)
    ["price"] => string(4) "0.00"
  }

  [1] => array(3) {
    ["name"] => string(9) "PingChat!"
    ["product_id"] => int(7620)
    ["price"] => string(4) "0.00"
  }
  [2] => array(3) {
    ["name"] => string(25) "MobiEXPLORE Zagreb County"
    ["product_id"] => int(8455)
    ["price"] => string(4) "0.00"
  }
}

項目を追加するとこうなるはずで、機能ごとに使ってみたのですがうまくいきませんでした。2.)

array(7) {
  [0] => array(3) {
    ["name"] => string(9) "airG Chat"
    ["product_id"] => int(2469)
    ["price"] => string(4) "0.00"
    ["description"] => string(23) "this is the custom test"
    ["brief_description"] => string(23) "this is the custom test"
  }

  [1] => array(3) {
    ["name"] => string(9) "PingChat!"
    ["product_id"] => int(7620)
    ["price"] => string(4) "0.00"
    ["description"] => string(23) "this is the custom test"
    ["brief_description"] => string(23) "this is the custom test"
  }
  [2] => array(3) {
    ["name"] => string(25) "MobiEXPLORE Zagreb County"
    ["product_id"] => int(8455)
    ["price"] => string(4) "0.00"
    ["description"] => string(23) "this is the custom test"
    ["brief_description"] => string(23) "this is the custom test"
  }
}
4

2 に答える 2

3

参照とともに使用foreach( &):

foreach($data as &$datum) {
    $datum["description"] = "this is the custom test";
    $datum["brief_description"] = "this is the custom test";
}

&この問題を回避するために)使用したくない場合は、次のことができます

foreach($data as $i => $datum) {
    $data[$i]["description"] = "this is the custom test";
    $data[$i]["brief_description"] = "this is the custom test";
}
于 2012-06-25T07:15:15.770 に答える
2

foreach を使用していて元の配列を変更したい場合は、 を追加する必要があります&

foreach ($arr as &$item) {
  $item["description"] = "this is the custom test";
  $item["brief_description"] = "this is the custom test";
}
于 2012-06-25T07:14:44.970 に答える