根据条件合并多维数组

问题描述 投票:0回答:1

我有以下数组:

array (size=6)
  'ID' => int 786
  'userID' => int 97291
  'tip_index' => string 'CARS' (length=11)
  'tip_apa' => string 'calda' (length=5)
  'citire' => string '2' (length=1)
  'data' => string '2020-04-21 15:32:49' (length=19)
C:\wamp64\www\test2.php:19:
array (size=6)
  'ID' => int 785
  'userID' => int 97291
  'tip_index' => string 'TRUCKS' (length=9)
  'tip_apa' => string 'calda' (length=5)
  'citire' => string '1' (length=1)
  'data' => string '2020-04-21 15:32:49' (length=19)
C:\wamp64\www\test2.php:19:
array (size=6)
  'ID' => int 784
  'userID' => int 97291
  'tip_index' => string 'MOTORCYCLES' (length=4)
  'tip_apa' => string 'calda' (length=5)
  'citire' => string '1' (length=1)
  'data' => string '2020-04-21 15:32:49' (length=19)
C:\wamp64\www\test2.php:19:
array (size=6)
  'ID' => int 783
  'userID' => int 97291
  'tip_index' => string 'CARS' (length=11)
  'tip_apa' => string 'calda' (length=5)
  'citire' => string '1' (length=1)
  'data' => string '2020-03-21 15:32:44' (length=19)
C:\wamp64\www\test2.php:19:
array (size=6)
  'ID' => int 782
  'userID' => int 97291
  'tip_index' => string 'TRUCKS' (length=9)
  'tip_apa' => string 'calda' (length=5)
  'citire' => string '0' (length=1)
  'data' => string '2020-03-21 15:32:41' (length=19)
C:\wamp64\www\test2.php:19:
array (size=6)
  'ID' => int 781
  'userID' => int 97291
  'tip_index' => string 'MOTORCYCLES' (length=4)
  'tip_apa' => string 'calda' (length=5)
  'citire' => string '0' (length=1)
  'data' => string '2020-03-21 15:32:39' (length=19)

[我希望输出3个这样的多维数组,按tip_indextip_apa分组,在其中我为每个tip_apa添加一些自定义键(例如:对于tip_apa = rece,我想添加数组data_iv_recenumber_iv_recedata_in_recenumber_in_rece)。每个tip_apa都具有上个月的日期和值以及本月的日期和值,如下所示:

array (size=3)
  'CARS' => 
    array (size=2)
      'rece' => 
        array (size=4)
          'data_iv_rece' => string '22.03.2020' (length=10)
          'number_iv_rece' => string '1' (length=1)
          'data_in_rece' => string '22.04.2020' (length=10)
          'number_in_rece' => string '0' (length=1)
      'calda' => 
        array (size=4)
          'data_iv_calda' => string '22.03.2020' (length=10)
          'number_iv_calda' => string '1' (length=1)
          'data_in_calda' => string '22.04.2020' (length=10)
          'number_in_calda' => string '0' (length=1)
  'TRUCKS' => 
    array (size=2)
      'rece' => 
        array (size=4)
          'data_iv_rece' => string '22.03.2020' (length=10)
          'number_iv_rece' => string '1' (length=1)
          'data_in_rece' => string '22.04.2020' (length=10)
          'number_in_rece' => string '0' (length=1)
      'calda' => 
        array (size=4)
          'data_iv_calda' => string '22.03.2020' (length=10)
          'number_iv_calda' => string '1' (length=1)
          'data_in_calda' => string '22.04.2020' (length=10)
          'number_in_calda' => string '0' (length=1)
  'MOTORCYCLES' => 
    array (size=2)
      'rece' => 
        array (size=4)
          'data_iv_rece' => string '22.03.2020' (length=10)
          'number_iv_rece' => string '1' (length=1)
          'data_in_rece' => string '22.04.2020' (length=10)
          'number_in_rece' => string '0' (length=1)
      'calda' => 
        array (size=4)
          'data_iv_calda' => string '22.03.2020' (length=10)
          'number_iv_calda' => string '1' (length=1)
          'data_in_calda' => string '22.04.2020' (length=10)
          'number_in_calda' => string '0' (length=1)

这是最近两天一直在尝试的方法,没有获得任何预期的输出:

 $userID   = $_SESSION['ID'];
 $info  = $db->query("SELECT * FROM `indecsi` WHERE `userID` = '$userID'")->fetchAll();
 $arrayInfos = array();
 foreach ($info as $key => $val) 
    {
        $month= date('m',strtotime($val['data']));

        if(date($month < date('m')) && $val['tip_apa'] == 'rece')
        {
            $data_iv_rece = $val['data'];
            $number_iv_rece = $val['citire'];
            $arrayInfos += [ 'data_iv_rece' => $data_iv_rece ];
            $arrayInfos += [ 'number_iv_rece' => $number_iv_rece ];
        }

        if(date($month == date('m')) && $val['tip_apa'] == 'rece')
        {
            $data_in_rece = $val['data'];
            $number_in_rece = $val['citire'];
            $arrayInfos += [ "data_in_rece" => $data_in_rece ];
            $arrayInfos += [ "number_in_rece" => $number_in_rece ];
        }

        if(date($month < date('m')) && $val['tip_apa'] == 'calda')
        {
            $data_iv_calda = $val['data'];
            $number_iv_calda = $val['citire'];
            $arrayInfos += [ 'data_iv_calda' => $data_iv_calda ];
            $arrayInfos += [ 'number_iv_calda' => $number_iv_calda ];
        }

        if(date($month == date('m')) && $val['tip_apa'] == 'calda')
        {
            $data_in_calda = $val['data'];
            $number_in_calda = $val['citire'];
            $arrayInfos += [ "data_in_calda" => $data_in_calda ];
            $arrayInfos += [ "number_in_calda" => $number_in_calda ];
        }
    }

最后尝试获取表中的3个数组:

 $consum_rece = $number_in_rece - $number_iv_rece;
 $consum_calda = $number_in_calda - $number_iv_calda;
 $table.= "<tr class='text-center'>";
 $table.= "<td>" . str_replace('_', ' ' , $val['tip_index']) . "</td>";
 $table.= "<td>" . $arrayInfos['data_in_rece'] . "</td>";
 $table.= "<td>" . $arrayInfos['number_in_rece'] . "</td>";
 $table.= "<td>" . $arrayInfos['data_iv_rece'] . "</td>";
 $table.= "<td>" . $arrayInfos['number_iv_rece'] . "</td>";
 $table.= "<td>" . $consum_rece . "</td>";
 $table.= "<td>" . $arrayInfos['data_in_calda'] . "</td>";
 $table.= "<td>" . $arrayInfos['number_in_calda'] . "</td>";
 $table.= "<td>" . $arrayInfos['data_iv_calda'] . "</td>";
 $table.= "<td>" . $arrayInfos['number_iv_calda'] . "</td>";
 $table.= "<td>" . $consum_calda . "</td>";
 $table.= "<td>-</td>";
 $table.= "</tr>";

enter image description here

[尝试了多项操作,例如array_pusharray_mergearray_diff以达到预期的输出,但没有运气。我觉得我已经走到一半了,无法解决问题。有什么建议吗?谢谢!

php arrays multidimensional-array foreach associative-array
1个回答
1
投票

起初,您的解释让我有些困惑,但是我想我已经知道会发生什么。

假设您已经有此php数组(从MySQL或其他地方收集):

$all_vehicles = array(
  array(
    'id' => 786,
    'userID' => 9791,
    'tip_index' => 'CARS',
    'tip_apa' => 'calda',
    'citire' => 2,
    'data' => '2020-04-21 15:32:49'
  ),  
  array(
    'id' => 785,
    'userID' => 9791,
    'tip_index' => 'TRUCKS',
    'tip_apa' => 'calda',
    'citire' => 1,
    'data' => '2020-04-21 15:32:49'
  ), 
  array(
    'id' => 784,
    'userID' => 97291,
    'tip_index' => 'MOTORCYCLES',
    'tip_apa' => 'calda',
    'citire' => 1,
    'data' => '2020-04-21 15:32:49'
  ), 
  array(
    'id' => 783,
    'userID' => 97291,
    'tip_index' => 'CARS',
    'tip_apa' => 'calda',
    'citire' => 1,
    'data' => '2020-03-21 15:32:44'
  ), 
  array(
    'id' => 782,
    'userID' => 97291,
    'tip_index' => 'TRUCKS',
    'tip_apa' => 'calda',
    'citire' => 0,
    'data' => '2020-03-21 15:32:41'
  ), 
  array(
    'id' => 781,
    'userID' => 97291,
    'tip_index' => 'MOTORCYCLES',
    'tip_apa' => 'calda',
    'citire' => 0,
    'data' => '2020-03-21 15:32:39'
  ),
);

我的建议是滚动数组并更新所需的值。我已通过添加“ data_in _”-值来自动消除了if和else。

然后将它们添加到多维数组以在汽车,卡车和摩托车之间分配。

$ordered_vehicles = array();
foreach($all_vehicles as $val)
{
    $month= date('m',strtotime($val['data']));

    if($month <= date('m') && in_array($val['tip_apa'],array('rece','calda')))
    {
        $data = date('d.m.Y',strtotime($val['data']));
        // Convert the data in the required format DD.MM.YYYY
        $number = $val['citire'];

        $val['data_in_'.$val['tip_apa']] =  $data;
        $val['number_in_'.$val['tip_apa']] =  $number;
    }

    $ordered_vehicles[$val['tip_index']][$val['tip_apa']][] = $val;
}

[预期结果将是具有3个子数组的多维数组-汽车,卡车和摩托车。它们每个内部都有多个数组,其中包含所需的数据。

在我的情况下看起来像这样:

Array
(
    [CARS] => Array
        (
            [calda] => Array
                (
                    [0] => Array
                        (
                            [id] => 786
                            [userID] => 9791
                            [tip_index] => CARS
                            [tip_apa] => calda
                            [citire] => 2
                            [data] => 2020-04-21 15:32:49
                            [data_in_calda] => 21.04.2020
                            [number_in_calda] => 2
                        )

                    [1] => Array
                        (
                            [id] => 783
                            [userID] => 97291
                            [tip_index] => CARS
                            [tip_apa] => calda
                            [citire] => 1
                            [data] => 2020-03-21 15:32:44
                            [data_in_calda] => 21.03.2020
                            [number_in_calda] => 1
                        )

                )

        )

    [TRUCKS] => Array
        (
            [calda] => Array
                (
                    [0] => Array
                        (
                            [id] => 785
                            [userID] => 9791
                            [tip_index] => TRUCKS
                            [tip_apa] => calda
                            [citire] => 1
                            [data] => 2020-04-21 15:32:49
                            [data_in_calda] => 21.04.2020
                            [number_in_calda] => 1
                        )

                    [1] => Array
                        (
                            [id] => 782
                            [userID] => 97291
                            [tip_index] => TRUCKS
                            [tip_apa] => calda
                            [citire] => 0
                            [data] => 2020-03-21 15:32:41
                            [data_in_calda] => 21.03.2020
                            [number_in_calda] => 0
                        )

                )

        )

    [MOTORCYCLES] => Array
        (
            [calda] => Array
                (
                    [0] => Array
                        (
                            [id] => 784
                            [userID] => 97291
                            [tip_index] => MOTORCYCLES
                            [tip_apa] => calda
                            [citire] => 1
                            [data] => 2020-04-21 15:32:49
                            [data_in_calda] => 21.04.2020
                            [number_in_calda] => 1
                        )

                    [1] => Array
                        (
                            [id] => 781
                            [userID] => 97291
                            [tip_index] => MOTORCYCLES
                            [tip_apa] => calda
                            [citire] => 0
                            [data] => 2020-03-21 15:32:39
                            [data_in_calda] => 21.03.2020
                            [number_in_calda] => 0
                        )

                )

        )

)

您可以在那之后打印并根据需要更改数据。如果我在此过程中遗漏了一些东西,请在下面提及。干杯。

© www.soinside.com 2019 - 2024. All rights reserved.