dtpoius74857 2017-03-21 18:09
浏览 37

通过匹配PHP中多维数组中的子键来复制子数组

I have an array similar to this:

array(
  array(
    'id'         => 'a',
    'other'      => 'abc: ',
    'subarray' => array(
      'somekey' => 'abc',
    ),
    'subarray2' => array(
      'somekey' => 'abc',
    ),
  ),
  array(
    'id'         => 'b',
    'other'      => 'abc: ',
    'subarray' => array(
      'somekey' => 'abc',
    ),
  ),
  array(
    'id'         => 'c',
    'other'      => 'abc: ',
    'subarray' => array(
      'somekey' => 'abc',
    ),
  ),
  )

I need to be able to copy each sub array into a new array by matching the ID. So, let's say I need to match ID 'b'.

  • 写回答

1条回答 默认 最新

  • dourui7186 2017-03-21 18:09
    关注

    The code I ended up using was a foreach loop to loop the array and find my id, then using the key from the parent array to copy the values of the subarray:

      $value = 'b'
      foreach ($this->fields as $fields_array => $sub_array) {
        if ( $sub_array['id'] == $value ) {
          $new_array = $this->fields[$fields_array];
          //do something with new array
        }
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号