【问题标题】:reformat multidimensional array based on value根据值重新格式化多维数组
【发布时间】:2011-03-23 12:55:28
【问题描述】:

我有一个以下格式的数组

$op = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [contact_id] => 36
                    [sender_id] => 79
                    [sendto] => 9192
                )

            [1] => Array
                (                       
                    [event_id] => 145
                    [sender_id] => 9139
                    [sendto] => 9192
                )

        )

    [1] => Array
        (
            [0] => Array
                (                       
                    [event_id] => 145
                    [sender_id] => 9272
                    [sendto] => 9290
                )

        )    
    [2] => Array
        (
            [0] => Array
                (                        
                    [event_id] => 145
                    [sender_id] => 9138
                    [sendto] => 9316
                )

            [1] => Array
                (                       
                    [event_id] => 145
                    [sender_id] => 9283
                    [sendto] => 9316
                )

        )
)  

我想过滤数组,结果数组的键应该是不同的 sendto 值,并且所有 sender_idsendto 下都应该在该数组的键下

期望的输出

Array
(
    [9192] => Array
        (
            [0] =>79
            [1] =>9139
        )
    [9290] =>Array
        (
            [0]=>9272
        )
    [9316] =>Array
        (
            [0] =>9138
            [1] =>9283
        )
)
 

虽然我尝试使用以下代码

foreach ($op as $ok=>$ov)
{
    if( array_key_exists($ov['sendto'],$mid))
        $mid[$ov['sendto']][]=$ok;
    else
        $mid[$ov['sendto']]=$ok;
}

但是这个显示notice:Undefined index: sendto

请告诉我哪里做错了??我总是陷入这样的问题

【问题讨论】:

    标签: php multidimensional-array


    【解决方案1】:

    类似这样的:

    <?php
    //Test Array
    $op = array(
        array(
            array(
                  'contact_id' => 36,
                  'sender_id' => 79,
                  'sendto' => 9192
            ),
            array(
                  'contact_id' => 145,
                  'sender_id' => 9139,
                  'sendto' => 9192
            )
        ),
        array(
            array(
                  'contact_id' => 145,
                  'sender_id' => 9272,
                  'sendto' => 9290
            )
        ),
        array(
            array(
                  'contact_id' => 145,
                  'sender_id' => 9138,
                  'sendto' => 9316
            ),
            array(
                  'contact_id' => 145,
                  'sender_id' => 9283,
                  'sendto' => 9316
            )
        ),
    );
    
    //Switch array format
    $new = array();
    foreach($op as $element)
    {
        foreach($element as $entity)
        {
            if(!isset($new[$entity['sendto']]))
            {
               $new[$entity['sendto']] = array();
            }
    
            $new[$entity['sendto']][] = $entity['sender_id'];
        }
    }
    
    //Debug the new array.
    print_r($new);
    

    【讨论】:

      【解决方案2】:

      试试:

      $mid = array();
      
      foreach($op as $tmp_array)
      {
        foreach($tmp_array as $message)
        {
          if (!isset($mid[$message['sendto']]))
            $mid[$message['sendto']] = array();
      
          $mid[$message['sendto']][] = $message['sender_id'];
        }
      }
      

      【讨论】:

      • 为什么你更喜欢isset 而不是array_key_exists()??有什么原因吗??
      • 这两个函数之间的行为差​​异很小,但isset() 处理最常见的情况要快得多。见juliusbeckmann.de/blog/…
      • 我怀疑有什么原因,但由于isset 用于所有形式的变量,因此isset 用于其他函数是很常见的
      【解决方案3】:

      你应该这样做:

      foreach ($op as $ok=>$ov)
      {
          if(!array_key_exists('sendto',$mid))
          {
      
              $mid[$ov['sendto']] = array();
          }
      
          $mid[$ov['sendto']][] = $ov['sender_id'];
      }
      

      【讨论】:

      • 方法错误,sendto 密钥始终存在于$ov,仍然报同样的错误
      • 是的,从那时起我就开始编辑了。但是你想要的输出是模棱两可的,我认为第六行的'145'应该是9139。我说的对吗?
      • 不,145 是 event_id,但我希望在每个 send_to 下都有 sender_id
      • 是的,我明白了。这就是为什么我想知道 145 在您想要的输出数组的 [9192][1] 位置(事件 ID)中做了什么。
      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2021-05-13
      • 1970-01-01
      • 2019-03-17
      • 2021-10-28
      相关资源
      最近更新 更多