如何遍历非结构化多维数组

时间:2019-04-04 08:08:00

标签: php soap

我对来自Web服务的SOAP响应有问题。响应是一个多维数组,每个员工的结构不同。

我尝试使用foreach循环来检索所有值并将它们存储在mysql数据库中。

Array
(
    [item] => Array
        (
            [0] => Array
                (
                    [department_id] => 1
                    [department_name] => Sales
                    [department_shortname] => SA
                    [hours] => Array
                        (
                            [item] => Array
                                (
                                    [activity_id] => 1
                                    [start_date] => 2019-03-12
                                    [end_date] => 2019-03-12
                                    [start_time] => 10:00
                                    [end_time] => 14:20
                                    [hour_code] => 0
                                    [foreign_hour_code] => 20
                                    [amount] => 3.83
                                    [type] => productive
                                    [labor_cost] => 25.23
                                )

                        )

                )

            [1] => Array
                (
                    [department_id] => 2
                    [department_name] => POS
                    [department_shortname] => POS
                    [hours] => Array
                        (
                            [item] => Array
                                (
                                    [activity_id] => 1
                                    [start_date] => 2019-03-12
                                    [end_date] => 2019-03-12
                                    [start_time] => 14:35
                                    [end_time] => 17:00
                                    [hour_code] => 0
                                    [foreign_hour_code] => 20
                                    [amount] => 2.42
                                    [type] => productive
                                    [labor_cost] => 24.67
                                )

                        )

                )

        )

)


Array
(
    [item] => Array 
        (
            [department_id] => 2
            [department_name] => POS
            [department_shortname] => POS
            [hours] => Array
                (
                    [item] => Array
                        (
                            [0] => Array
                                (
                                    [activity_id] => 1
                                    [start_date] => 2019-03-13
                                    [end_date] => 2019-03-13
                                    [start_time] => 07:30
                                    [end_time] => 12:00
                                    [hour_code] => 0
                                    [foreign_hour_code] => 20
                                    [amount] => 4.25
                                    [type] => productive
                                    [labor_cost] => 40.48
                                )

                            [1] => Array
                                (
                                    [activity_id] => 1
                                    [start_date] => 
                                    [end_date] => 
                                    [start_time] => 
                                    [end_time] => 
                                    [hour_code] => 
                                    [foreign_hour_code] => 63
                                    [amount] => 2
                                    [type] => none
                                    [labor_cost] => 0
                                )

                        )

                )

        )


我尝试了这个,但是我没有得到所有数据。

foreach($array as $employees)
{
  foreach($employees as $employee)
  {
    foreach($employee as $days)
    {
        foreach($days as $day)
        {
          foreach($day as $item)
          {
            foreach($item as $stores)
            {
              foreach($stores as $store)
              {
                foreach($store as $item1)
                {
                  foreach($item1 as $item2)
                  {
                    foreach($item2 as $hours)
                    {
                      foreach($hours as $hour)
                      {
                        foreach($hour as $booking)
                        {
                          /*
                          Store in DB per time registration.

                          $hours['department_id']
                          $hours['department_name']
                          $hours['department_shortname']

                          $booking[activity_id]
                          $booking[start_date]
                          $booking[end_date]
                          $booking[start_time]
                          $booking[end_time]
                          $booking[hour_code]
                          $booking[foreign_hour_code]
                          $booking[amount]
                          $booking[type]
                          $booking[labor_cost]
                          */
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
    }
  }
}

我想遍历所有元素,并在每次注册时将它们存储在数据库中。

department_id |部门名称| department_shortname | activity_id |开始日期|结束日期| start_time |结束时间|小时代码| foreign_hour_code |金额|类型人工费用

1 个答案:

答案 0 :(得分:0)

我将从查看“项目”开始回答。值得注意的是,当只有一个项目时,在“项目”中立即存在该项目,但是如果有多个项目,则使用项目的数字数组。因此,让我们为此做一些功能,只是为了纠正这种不一致。

function getItems($itemArray)
{
  // get the items in the item array
  $items = $itemArray['item'];
  // multiple items, in a numeric array, are returned as they are
  if (array_keys($items) === range(0, count($items) - 1) return $items;
  // whereas a single items is placed in a numeric array
  return [$items];
}

我们可以将其应用于任何“项目”。

现在我有一个问题,因为您没有给我足够的信息。我不知道您展示给我的两个数组是其中的一部分。因此,我必须假设您可以得到它们。它们是物品。让我们给他们起一个名字:“起点”。这是非常随意的。

function processStartpoint($startpoint)
{
    // first get the department items
    $departments = getItems($startpoint);
    // process each department
    foreach ($departments as $department) {
        // get department info
        $departmentId        = $department['department_id'];
        $departmentName      = $department['department_name'];
        $departmentShortName = $department['department_shortname'];  
        // walk through each hour
        foreach (getItems($department['hours']) as $hour) {
            // get hour info
            $ActivityId      = $hour['activity_id'];
            $StartDate       = $hour['start_date']; 
            $EndDate         = $hour['end_date']; 
            $StartTime       = $hour['start_time']; 
            $EndTime         = $hour['end_time'];
            $HourCode        = $hour['hour_code']; 
            $ForeignHourCode = $hour['foreign_hour_code']; 
            $Amount          = $hour['amount'];
            $Type            = $hour['type'];
            $LaborCost       = $hour['labor_cost']; 
            /*
              store this in your database
            */
        }
    }
}

现在,我没有真正的方法来检查此代码是否有效,但是类似这样的事情。真正的诀窍是在getItems()函数中,它对数组的结构做出反应。