doxzrb8721 2015-06-03 01:47
浏览 39
已采纳

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

i have an array like this

array 1

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

i have plan to giving array with name, the array name is number

array 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" } 
}

I want to inserting array 1 into array 2 which same array key

I want to insert array 1 data into array 2 accordance with the key array . so i I was expecting to be joined both of arrays and became joined array like this

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"} 
} 

is there any way to create or manipulate into an array like that ? my array is dynamically so number of array can be changed anytime.

I would greatly appreciate it if you could help me

  • 写回答

1条回答 默认 最新

  • doushupu2521 2015-06-03 02:24
    关注

    Read up on the basic language control structures and foreach in particular.

    foreach ($array2 as $index => $object) {
        if (isset($array1[$index])) {
            $object->number = $array1[$index];
        }
    }
    

    Outcome:

    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"
      }
    }
    

    Here is a Codepad demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成