基于值重新格式化多维数组

时间:2011-03-23 12:55:29

标签: php multidimensional-array

我有一个以下格式的数组

$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_id下的所有sendto都应该位于该数组的键下

期望的输出

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;
}

但是这一个显示通知:未定义的索引:发送到

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

3 个答案:

答案 0 :(得分:2)

这样的事情:

<?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);

答案 1 :(得分:0)

尝试:

$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'];
  }
}

答案 2 :(得分:-1)

你应该这样:

foreach ($op as $ok=>$ov)
{
    if(!array_key_exists('sendto',$mid))
    {

        $mid[$ov['sendto']] = array();
    }

    $mid[$ov['sendto']][] = $ov['sender_id'];
}