组合两个数组并将数组名称作为新组合数组中的键

时间:2018-05-10 02:44:09

标签: php arrays

我有两个数组,$array1$array2。组合这两个数组时,预期的输出是根据它们的键组合两个数组,并将数组名称作为键。我使用组合了两个数组的array_merge_recursive($array1,$array2);,但将数组名称作为键不起作用。

$array1 = array(
'mobile' => array(
    array('item' => 'apple','price' => 4),
    array('item' => 'nokia','price' => 39),
    array('item' => 'samsung','price' => 8)
  ),
  'tv' => array(
     array('item' => 'LG','price' => 39),
     array( 'item' => 'max', 'price' => 8 ),
     array('item' => 'diaken','price' => 3 )
  )
) ;

$array2 = array(
'mobile' => array(
     array('item' => 'HTC','price' => 4 ),
    array('item' => 'OnePlus' ,'price' => 39),
     array ('item' => 'Nexus','price' => 8 )
  ),
'tv' => array( 
    array('item' => 'LG','price' => 39),
    array('item' => 'Panasonic','price' => 8 ),
    array('item' => 'Toshiba' ,'price' => 3 )
  )
);

输出应为:

array(
'mobile' => array(
  'array1' => array(
    '0' => array('item' => 'apple','price' => 4),
    '1' => array('item' => 'nokia','price' => 39),
    '2' => array('item' => 'samsung','price' => 8),
  )
  'array2' => array (
    '0' => array('item' => 'HTC','price' => 4),
    '1' => array('item' => 'OnePlus','price' => 39),
    '2' => array('item' => 'Nexus','price' => 8)
    )  
),
'tv' => array( 
  'array1' => array( 
    '0' => array('item' => 'LG','price' => 39),
    '1' => array('item' => 'max','price' => 8),
    '2' => array('item' => 'diaken','price' => 3)
    ),
    'array2' => array(
    '0' => array('item' => 'LG','price' => 39),
    '1' => array('item' => 'max','price' => 8),
    '2' => array('item' => 'diaken','price' => 3)
    )  
  )
) 

我使用array_merge_recursive($array1,$array2);,而不是上面的内容,我得到以下内容:

    Array
(
   'mobile' => Array
        (
            Array('item' => 'apple','price' => 4),
            Array('item' => 'nokia','price' => 39),
            Array('item' => 'samsung','price' => 8),
            Array('item' => 'HTC','price' => 4 ),
            Array('item' => 'OnePlus' ,'price' => 39),
            Array ('item' => 'Nexus','price' => 8 ),

        )

    'tv' => Array
        (
            Array('item' => 'LG','price' => 39),
            Array( 'item' => 'max', 'price' => 8 ),
            Array('item' => 'diaken','price' => 3 ),
            Array ('item' => 'LG','price' => 39),
            Array('item' => 'Panasonic','price' => 8 ),
            Array('item' => 'Toshiba' ,'price' => 3 )

        )

) 

4 个答案:

答案 0 :(得分:0)

虽然我不知道你将如何自动发现阵列的“发现”,但它应该像这样简单:

$array = array();
$array['mobile'] = array();
$array['mobile'][] = $array1['mobile'];
$array['mobile'][] = $array2['mobile'];
$array['tv'] = array();
$array['tv'][] = $array1['tv'];
$array['tv'][] = $array2['tv'];
// Using the array keys as the worker
$array = array();
foreach( array_keys( array_merge( $array1, $array2 ) ) as $key )
{
  $array[$key] = array();
  $array[$key][] = $array1[$key];
  $array[$key][] = $array2[$key];
}

答案 1 :(得分:0)

第一个数组

    $array1 = array
(
    'mobile' => array
        (
            '0' => array
                (
                    'item' => 'apple',
                    'price' => 4
                ),
            '1' => array
                (
                    'item' => 'nokia',
                    'price' => 39
                ),
            '2' => array
                (
                    'item' => 'samsung',
                    'price' => 8
                ),
        ),

    'tv' => array
        (
            '0' => array
                (
                    'item' => 'LG',
                    'price' => 39
                ),
            '1' => array
                (
                    'item' => 'max',
                    'price' => 8
                ),
            '2' => array
                (
                    'item' => 'diaken',
                    'price' => 3
                ),
        )
) ;

第二个数组

    $array2 = array
(
    'mobile' => array
        (
            '0' => array
                (
                    'item' => 'HTC',
                    'price' => 4
                ),
            '1' => array
                (
                    'item' => 'OnePlus',
                    'price' => 39
                ),
            '2' => array
                (
                    'item' => 'Nexus',
                    'price' => 8
                ),
        ),
    'tv' => array
        (
            '0' => array
                (
                    'item' => 'LG',
                    'price' => 39
                ),
            '1' => array
                (
                    'item' => 'Panasonic',
                    'price' => 8
                ),
            '2' => array
                (
                    'item' => 'Toshiba',
                    'price' => 3
                ),
        )
);

制作两个数组和打印结果

    $array['mobile']['array1'] = $array1['mobile'];
    $array['mobile']['array2'] = $array2['mobile'];
    $array['tv']['array1'] = $array1['tv'];
    $array['tv']['array2'] = $array2['tv'];
    echo '<pre>';
    print_r($array);
    echo '</pre>';

输出

    Array
    (
[mobile] => Array
    (
        [array1] => Array
            (
                [0] => Array
                    (
                        [item] => apple
                        [price] => 4
                    )

                [1] => Array
                    (
                        [item] => nokia
                        [price] => 39
                    )

                [2] => Array
                    (
                        [item] => samsung
                        [price] => 8
                    )

            )

        [array2] => Array
            (
                [0] => Array
                    (
                        [item] => HTC
                        [price] => 4
                    )

                [1] => Array
                    (
                        [item] => OnePlus
                        [price] => 39
                    )

                [2] => Array
                    (
                        [item] => Nexus
                        [price] => 8
                    )

            )

    )

[tv] => Array
    (
        [array1] => Array
            (
                [0] => Array
                    (
                        [item] => LG
                        [price] => 39
                    )

                [1] => Array
                    (
                        [item] => max
                        [price] => 8
                    )

                [2] => Array
                    (
                        [item] => diaken
                        [price] => 3
                    )

            )

        [array2] => Array
            (
                [0] => Array
                    (
                        [item] => LG
                        [price] => 39
                    )

                [1] => Array
                    (
                        [item] => Panasonic
                        [price] => 8
                    )

                [2] => Array
                    (
                        [item] => Toshiba
                        [price] => 3
                    )

            )

    )

答案 2 :(得分:0)

如果根据您的上述评论

修复了array1和array2,请检查以下代码
$array_keys = array_keys( array_merge( $array1, $array2));

$final_array = array();
foreach($array_keys as $akey)
{
    if(array_key_exists($akey, $array1))
        $final_array[$akey]['array1'] = $array1[$akey];

    if(array_key_exists($akey, $array2))
        $final_array[$akey]['array2'] = $array2[$akey];
}
echo "<pre>";
    print_r($final_array);
echo "</pre>";

输入代码:

$array1 = array(
'mobile' => array(
    array('item' => 'apple','price' => 4),
    array('item' => 'nokia','price' => 39),
    array('item' => 'samsung','price' => 8)
  ),
  'tv' => array(
     array('item' => 'LG','price' => 39),
     array( 'item' => 'max', 'price' => 8 ),
     array('item' => 'diaken','price' => 3 )
  )
) ;
$array2 = array (
  'mobile' => array (
    0 => array ('item' =>'HTC','price' => 4,),
    1 => array ('item' =>'OnePlus','price' => 39,),
    2 => array ('item' =>'Nexus','price' => 8,)
  ),
  'tv' => array (
    0 => array ('item' =>'LG','price' => 39,),
    1 => array ('item' =>'Panasonic','price' => 8,),
    2 => array ('item' =>'Toshiba','price' => 3)
  )
); 

<强> O / P:

Array
(
    [mobile] => Array
        (
            [array1] => Array
                (
                    [0] => Array
                        (
                            [item] => apple
                            [price] => 4
                        )

                    [1] => Array
                        (
                            [item] => nokia
                            [price] => 39
                        )

                    [2] => Array
                        (
                            [item] => samsung
                            [price] => 8
                        )

                )

            [array2] => Array
                (
                    [0] => Array
                        (
                            [item] => HTC
                            [price] => 4
                        )

                    [1] => Array
                        (
                            [item] => OnePlus
                            [price] => 39
                        )

                    [2] => Array
                        (
                            [item] => Nexus
                            [price] => 8
                        )

                )

        )

    [tv] => Array
        (
            [array1] => Array
                (
                    [0] => Array
                        (
                            [item] => LG
                            [price] => 39
                        )

                    [1] => Array
                        (
                            [item] => max
                            [price] => 8
                        )

                    [2] => Array
                        (
                            [item] => diaken
                            [price] => 3
                        )

                )

            [array2] => Array
                (
                    [0] => Array
                        (
                            [item] => LG
                            [price] => 39
                        )

                    [1] => Array
                        (
                            [item] => Panasonic
                            [price] => 8
                        )

                    [2] => Array
                        (
                            [item] => Toshiba
                            [price] => 3
                        )

                )

        )

)

答案 3 :(得分:0)

我想干脆做 -

import random
deck= {'K ♥':'10' ,'Q ♥':'10','J ♥':'10','10 ♥':'10','9 ♥':'9','8 
♥':'8','7 ♥':'7','6 ♥':'6','5 ♥':'5','4 ♥':'4','3 ♥':'3','2 ♥':'2','A ♥':'11','K◆':'10','Q◆':'10','J◆':'10','10◆':'10','9◆':'9','8◆':'8','7◆':'7','6◆':'6','5◆':'5','4◆':'4','3◆':'3','2◆':'2','A◆':'11','K ♣':'10','Q ♣':'10','J ♣':'10','10 ♣':'10','9 ♣':'9','8 ♣':'8','7 ♣':'7','6 ♣':'6','5 ♣':'5','4 ♣':'4','3 ♣':'3','2 ♣':'2','A ♣':'11','K ♠':'10','Q ♠':'10','J ♠':'10','10 ♠':'10','9 ♠':'9','8 ♠':'8','7 ♠':'7','6 ♠':'6', '5 ♠':'5','4 ♠':'4','3 ♠':'3','2 ♠':'2','A ♠':'11'}
number= random.choice(list(deck.keys()))
del deck[number]
number2= random.choice(list(deck.keys()))
print(number)
print(number2)
value1 = int(deck.get(number))
value2 =int(deck.get(number2))
print(value1+value2)
  

这将为您提供您想要的确切输出。无论阵列中存在多少个键。您需要确定应该施加哪个数组循环。