douhe4336 2015-12-22 13:50
浏览 28
已采纳

将数组元素移动到同一数组中的另一个元素中

This is my array:

array (size=5)
  0 => 
    object(stdClass)[50]
      public 'PkID' => string '608' (length=3)
      public 'ConstructionTypeFk' => string '1' (length=1)
      public 'Price' => string '65757' (length=5)
      public 'discount_id' => string '0' (length=1)
  3 => 
    object(stdClass)[53]
      public 'PkID' => null
      public 'ConstructionTypeFk' => string '2' (length=1)
      public 'Price' => null
      public 'discount_id' => null
  4 => 
    object(stdClass)[54]
      public 'PkID' => null
      public 'ConstructionTypeFk' => string '1' (length=1)
      public 'Price' => null
      public 'discount_id' => 2

I want to move every element that has discount_id assigned inside coresponding element that shares the same ConstructionTypeFk. So in this case the last element under the first element and array would look like so:

array (size=5)
  0 => 
    object(stdClass)[50]
      public 'PkID' => string '608' (length=3)
      public 'ConstructionTypeFk' => string '1' (length=1)
      public 'Price' => string '65757' (length=5)
      public 'discount_id' => string '0' (length=1)
      public 'discount' => 
         array (size=1)
           0 => 
              object(stdClass)[54]
                public 'PkID' => null
                public 'ConstructionTypeFk' => string '1' (length=1)
                public 'Price' => null
                public 'discount_id' => 2
           1 => 
              another one if needed etc... 
  3 => 
    object(stdClass)[53]
      public 'PkID' => null
      public 'ConstructionTypeFk' => string '2' (length=1)
      public 'Price' => null
      public 'discount_id' => null

So far I have this code ($query contains my original array):

    foreach ($query as $key => $value)
    {
        if ($value->discount_id !== '0' || $value->discount_id !== NULL)
        {

            //Move this element to another element here!
            $query['index of the element that has the same ConstructionTypeFk']->discount[] = $value;
        }
        else
        {
            continue;
        }
    }

    return $result;

How can I get "index of element the that has the same ConstructionTypeFk"? Or is there a better way?

  • 写回答

1条回答 默认 最新

  • dongtuan1594 2015-12-22 14:19
    关注
    $result = [];
    foreach ($array as $item) {
        if (!$item->discount_id) {
            $result[] = $item;
        } else {
            foreach ($result as $parent) {
                if ($item->ConstructionTypeFk == $parent->ConstructionTypeFk) {
                    $parent->discount[] = $item;
                }
            }
        }
    }
    

    Not the most performant solution, but pretty straight forward.

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

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python