codeigniter:从数组1插入值到数组2?

时间:2015-06-03 01:47:36

标签: php arrays codeigniter

我有一个像这样的数组

数组1

array(3) { 
   [0]=> string(2) "47" 
   [1]=> string(2) "48" 
   [2]=> string(2) "49" 
} 

我打算给出带名字的数组,数组名称是数字

阵列2

array(3) { 
[0]=> object(stdClass)#18 (2) { 
      ["address"]=> string(9) "Address 1" 
      ["price"]=> string(16) "120000" } 
[1]=> object(stdClass)#21 (2) { 
      ["address"]=> string(9) "Address 2"      
      ["price"]=> string(16) "150000" } 
[2]=> object(stdClass)#20 (2) { 
      ["address"]=> string(9) "Address 3" 
      ["price"]=> string(16) "180000" } 
}

我想将数组1插入到阵列2中,该数组2具有相同的数组键

我想根据密钥数组将数组1数据插入到数组2中。所以我希望加入这两个阵列并成为像这样的连接阵列

array(3) { 
[0]=> object(stdClass)#18 (2) { 
      ["address"]=> string(9) "Address 1" 
      ["price"]=> string(16) "120000" 
      ["number"]=> string(2) "47" } 
[1]=> object(stdClass)#21 (2) { 
      ["address"]=> string(9) "Address 2"      
      ["price"]=> string(16) "150000" 
      ["number"]=> string(2) "48"} 
[2]=> object(stdClass)#20 (2) { 
      ["address"]=> string(9) "Address 3" 
      ["price"]=> string(16) "180000" 
      ["number"]=> string(2) "49"} 
} 

有没有办法像这样创建或操作数组?我的数组动态,因此可以随时更改数组的数量。

如果你能帮助我,我将不胜感激

1 个答案:

答案 0 :(得分:2)

特别阅读基本语言control structuresforeach

ibdata1

结果:

array(3) {
  [0]=>
  object(stdClass)#1 (3) {
    ["address"]=>
    string(9) "Address 1"
    ["price"]=>
    string(6) "120000"
    ["number"]=>
    string(2) "47"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["address"]=>
    string(9) "Address 2"
    ["price"]=>
    string(6) "150000"
    ["number"]=>
    string(2) "48"
  }
  [2]=>
  object(stdClass)#3 (3) {
    ["address"]=>
    string(9) "Address 3"
    ["price"]=>
    string(6) "180000"
    ["number"]=>
    string(2) "49"
  }
}

这是 Codepad 演示