简体   繁体   English

PHP 根据其他两个 arrays 的数据创建数组的最佳方法

[英]PHP Best way to create an array based on data of two other arrays

I am not looking to necessarily merge or combine arrays but instead create a third array based on data from two others.我并不一定要合并或组合 arrays 而是根据其他两个数据创建第三个数组。

Devices Array设备阵列

$devices = [
    4           => 'Web Push',
    8           => 'Web Push Play Sound',
    256         => 'Email (Work)',
    512         => 'Email (Home)',
    32768       => 'App Push Notification',
    4194304     => 'SMS (Work)',
    8388608     => 'SMS (Home)',
];
$token_category = [
    100         => 'Web',
    200         => 'iOS',
    300         => 'Android',
];

So specifically - I need to create a third array that has matched devices to their token categories.特别是 - 我需要创建第三个数组,将设备与其令牌类别相匹配。

Here's what I have, but I imagine there has to be a better way.这就是我所拥有的,但我想必须有更好的方法。


// $device_ids is an array of ints. [4, 8, 32768]
// $user_tokens_list is a list of objects with their specific token.
// $device_arr is an associative array I want with device_token under their categories. ('ios', 'android', 'web', 'email_work', 'email_home', 'sms_work', 'sms_home)

foreach ( $user_tokens_list as $user_token ) {

    if( $user_token->getPlatform() == 200 && in_array( 32768, $device_ids )  ) {
        $device_arr['ios'][] = $user_token->getDeviceToken();
    } else if( $user_token->getPlatform() == 300 && in_array( 32768, $device_ids )  ) {
        $device_arr['android'][] = $user_token->getDeviceToken();
    } else if( $user_token->getPlatform() == 100 ) {
        if( in_array( 4, $device_ids ) || in_array( 8, $device_ids ) ) {
            $device_arr['web'][] = $user_token->getDeviceToken();
        }
    } else {
        if ( in_array( 256, $device_ids ) ) {
            $device_arr['email_work'][] = $user_token->getDeviceToken();
        } else if ( in_array( 512, $device_ids ) ) {
            $device_arr['email_home'][] = $user_token->getDeviceToken();
        }
    }
}



The above works but it feels sloppy.上面的方法有效,但感觉很草率。

I have added the resulting array it produces from print_r().我已经添加了它从 print_r() 生成的结果数组。 It is the result I need but I feel it is very sloppy.这是我需要的结果,但我觉得它很草率。

Array
(
    [ios] => Array
        (
            [0] => Test Device Token
        )

    [web] => Array
        (
            [0] => Another Test Device Token
        )

    [android] => Array
        (
            [0] => Again Another Test Device Token
        )

)

Not entirely sure, if this is what you want to achieve but, you could also just save the devices to the user id under the platform:不完全确定,如果这是您想要实现的,您也可以将设备保存到平台下的用户 ID:

$device_arr = [];

foreach ( $user_tokens_list as $user_token ) {
    if ( $token_category[ $user_token->getPlatform() ] ) {
        $device_arr[ $token_category[ $user_token->getPlatform() ] ][ $user_token->getDeviceToken() ] = $devices;
    }
    else{
        $device_arr[ 'other' ][ $user_token->getDeviceToken() ] = $devices;
    }
}

// Result
$device_arr = [
    'iOS' => [
        'uuid1' => [
            4       => 'Web Push',
            8       => 'Web Push Play Sound',
            512     => 'Email (Home)',
            32768   => 'App Push Notification',
            8388608 => 'SMS (Home)',
        ],
        'uuid2' => [
            4       => 'Web Push',
            8       => 'Web Push Play Sound',
            256     => 'Email (Work)',
            512     => 'Email (Home)',
            32768   => 'App Push Notification',
            4194304 => 'SMS (Work)',
            8388608 => 'SMS (Home)',
        ],
        // ...
    ],
    'Android' => [
        'uuid3' => [
            4       => 'Web Push',
            8       => 'Web Push Play Sound',
            512     => 'Email (Home)',
            32768   => 'App Push Notification',
            8388608 => 'SMS (Home)',
        ],
        // ...
    ],
    'Web' => [
        'uuid4' => [
            4       => 'Web Push',
            8       => 'Web Push Play Sound',
            512     => 'Email (Home)',
            32768   => 'App Push Notification',
            8388608 => 'SMS (Home)',
        ],
        // ...
    ],
    'other' => [
        'uuid5' => [
            4       => 'Web Push',
            8       => 'Web Push Play Sound',
            512     => 'Email (Home)',
            32768   => 'App Push Notification',
            8388608 => 'SMS (Home)',
        ],
        // ...
    ],
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM