【问题标题】:unset values null array multidimensional php [duplicate]未设置值空数组多维php [重复]
【发布时间】:2022-01-08 19:30:15
【问题描述】:

这是我第一次使用 stackoverflow,如果我做错了什么,非常抱歉。我也很感激你的建议。

我有下一个数组:

$dataPayment= [
  "operationType" => null
  "terminal" => 12345
  "payment" => array:14 [
    "terminal" => 12345
    "order" => "1234519997"
    "amount" => 100
    "currency" => "EUR"
    "secure" => 0
    "idUser" => 123456789"
    "tokenUser" => "zidkeKeu68Kld"
    "urlOk" => null
    "urlKo" => null
    "originalIp" => "1.130.151.28"
    "methodId" => 1
    "trxType" => "N"
    "userInteraction" => 1
    "scaException" => "MIT"
  ]
  "subscription" => array:2 [
    "startDate" => null
    "endDate" => null
  ]
]

我想删除空值。使用 array_filter 也删除值 0,但我需要这些值 0。我尝试了以下方法:

private function arrayUnset( $dataPayment )
{
    foreach( $dataPayment as $key => $value ) 
    {
        if( is_array( $dataPayment[ $key ] ) )
        {
           $this->arrayUnset( $dataPayment[ $key ] );
        }
        if( $value === null || $value === "" ) 
        {
            unset( $dataPayment[ $key ] ); 
        } 
    }
    return $dataPayment;
}

但是,只删除第一个值。

$dataPayment = [
  "terminal" => 12345
  "payment" => array:14 [
    "terminal" => 12345
    "order" => "1234519997"
    "amount" => 100
    "currency" => "EUR"
    "secure" => 0
    "idUser" => 123456789"
    "tokenUser" => "zidkeKeu68Kld"
    "urlOk" => null
    "urlKo" => null
    "originalIp" => "1.130.151.28"
    "methodId" => 1
    "trxType" => "N"
    "userInteraction" => 1
    "scaException" => "MIT"
  ]
  "subscription" => array:2 [
    "startDate" => null
    "endDate" => null
  ]
]

我需要以下数组:

$dataPayment = [
      "terminal" => 12345
      "payment" => array:14 [
        "terminal" => 12345
        "order" => "1234519997"
        "amount" => 100
        "currency" => "EUR"
        "secure" => 0
        "idUser" => 123456789"
        "tokenUser" => "zidkeKeu68Kld"
        "originalIp" => "1.130.151.28"
        "methodId" => 1
        "trxType" => "N"
        "userInteraction" => 1
        "scaException" => "MIT"
      ]
    ]

你能帮帮我吗?谢谢。

【问题讨论】:

    标签: php arrays laravel null unset


    【解决方案1】:

    你应该通过引用传递参数。

    private function arrayUnset( &$dataPayment )
    {
        foreach( $dataPayment as $key => $value ) 
        {
            if( is_array( $dataPayment[ $key ] ) )
            {
               $dataPayment[ $key ] = $this->arrayUnset($value);
            }
            if( $value === null || $value === "" ) 
            {
                unset( $dataPayment[ $key ] ); 
            } 
        }
        return $dataPayment;
    }
    

    【讨论】:

      【解决方案2】:

      该代码似乎没有删除0 值条目,但如果您想查看调用过程中的更改,则需要通过引用传递参数

      $Payment = [
          "operationType" => null,
          "terminal" => 12345,
          "payment" => [
            "terminal" => 12345,
            "order" => "1234519997",
            "amount" => 100,
            "currency" => "EUR",
            "secure" => 0,
            "idUser" => 123456789,
            "tokenUser" => "zidkeKeu68Kld",
            "urlOk" => null,
            "urlKo" => null,
            "originalIp" => "1.130.151.28",
            "methodId" => 1,
            "trxType" => "N",
            "userInteraction" => 1,
            "scaException" => "MIT"
          ],
          "subscription" =>  [
              "startDate" => null,
              "endDate" => null
          ]
      ];
      class xxx
      {
          private function arrayUnset( &$dataPayment )
          {
              foreach( $dataPayment as $key => $value ) {
                  if( is_array( $dataPayment[ $key ] ) ) {
                     $this->arrayUnset( $dataPayment[ $key ] );
                  }
                  if( $value === null || $value === "" ) {
                      unset( $dataPayment[ $key ] ); 
                  } 
              }
              return $dataPayment;
          }
      
          public function zzz($data)
          {
              return $this->arrayUnset($data);
          }
      }
      
      $obj = new xxx;
      
      print_r($obj->zzz($Payment));
      
      

      结果

      Array
      (
          [terminal] => 12345
          [payment] => Array
              (
                  [terminal] => 12345
                  [order] => 1234519997
                  [amount] => 100
                  [currency] => EUR
                  [secure] => 0
                  [idUser] => 123456789
                  [tokenUser] => zidkeKeu68Kld
                  [originalIp] => 1.130.151.28
                  [methodId] => 1
                  [trxType] => N
                  [userInteraction] => 1
                  [scaException] => MIT
              )
      
          [subscription] => Array
              (
              )
      
      )
      

      【讨论】:

        【解决方案3】:

        数组过滤器移除空元素,所以使用 mapWithKeys 映射你的数组,如果每个属性都是一个数组,使用array_filter()。运行二级过滤器,移除空数组。

        $collection = collect($dataPayment);
        
        $result = $collection->mapWithKeys(function ($item, $key) {
        
            if (is_array($item)) {
                $item = array_filter($item);
            }
        
            return [$key => $item];
        })->filter()->all();
        

        这应该会产生预期的结果。如果代码有任何问题,请写。

        【讨论】:

          【解决方案4】:

          您没有存储递归调用的返回值。

          试试:

          <?php
          
          $Payment = [
              "operationType" => null,
              "terminal" => 12345,
              "payment" => [
                "terminal" => 12345,
                "order" => "1234519997",
                "amount" => 100,
                "currency" => "EUR",
                "secure" => 0,
                "idUser" => 123456789,
                "tokenUser" => "zidkeKeu68Kld",
                "urlOk" => null,
                "urlKo" => null,
                "originalIp" => "1.130.151.28",
                "methodId" => 1,
                "trxType" => "N",
                "userInteraction" => 1,
                "scaException" => "MIT"
              ],
              "subscription" =>  [
                  "startDate" => null,
                  "endDate" => null
              ]
          ];
          
          function arrayUnset($dataPayment) {
              foreach($dataPayment as $key => $value) 
                  if(is_array($dataPayment[$key]))
                      $dataPayment[$key]=arrayUnset($dataPayment[$key]);
                  else if ($value==null || $value=="") 
                      unset($dataPayment[$key]); 
              return $dataPayment;
          }
          
          print_r(arrayUnset($Payment));
          

          输出:

          大批 ( [终端] => 12345 [支付] => 数组 ( [终端] => 12345 [订购] => 1234519997 [数量] => 100 [货币] => 欧元 [安全] => 0 [idUser] => 123456789 [tokenUser] => zidkeKeu68Kld [原始IP] => 1.130.151.28 [方法ID] => 1 [trxType] => N [用户交互] => 1 [scaException] => 麻省理工学院 ) [订阅] => 数组 ( ) )

          Teh Playground!

          【讨论】:

            猜你喜欢
            • 2017-11-16
            • 2015-02-17
            • 2019-10-01
            • 2021-12-27
            • 2021-01-25
            • 1970-01-01
            • 1970-01-01
            • 2015-06-04
            • 1970-01-01
            相关资源
            最近更新 更多