基于值重新格式化多维数组 [英] reformat multidimensional array based on value

查看:47
本文介绍了基于值重新格式化多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以下格式的数组

i have an array in below format

$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 值,并且 sendto 下的所有 sender_id 都应该属于该数组的键

i want to filter array in a way that resultant array's key should be different sendto values and all sender_id under that sendto shoud come under that array's key

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

虽然我尝试了下面的代码

although i tried with below code

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

但是这个显示注意:未定义索引:发送到

请告诉我我哪里做错了??我总是卡在这样的问题中

please tell me where i m doing wrong?? i always stuck in such problem

推荐答案

是这样的:

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

这篇关于基于值重新格式化多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆