PHP如果键值相同,则组合2个数组

时间:2017-09-21 05:27:35

标签: php arrays multidimensional-array

我有阵列列表。

$array1 = array('SrNo' => 'xyzO' , 'AirlineCode' => '9E' , 'FlightNo' => '777')

$array2 = array('SrNo' => 'xyzR' , 'AirlineCode' => '6G' , 'FlightNo' => '546')

$array3 = array('SrNo' => 'abcO' , 'AirlineCode' => '5H' , 'FlightNo' => '423')

$array4 = array('SrNo' => 'abcR' , 'AirlineCode' => '2G' , 'FlightNo' => '420')

如果 SrNo 对于2个数组是相同的,那么我想获得如下结果。

details=>[
  [1]=>[
     ['onwards']=>[
          ['SrNo' => 'xyzO' , 'AirlineCode' => '9E' , 'FlightNo' => '777'],
     ['return']=>[
          ['SrNo' => 'xyzR' , 'AirlineCode' => '6G' , 'FlightNo' => '546']
      ],

  [2]=>[
     ['onwards']=>[
          ['SrNo' => 'abcO' , 'AirlineCode' => '5H' , 'FlightNo' => '423'],
     ['return']=>[
          ['SrNo' => 'abcR' , 'AirlineCode' => '2G' , 'FlightNo' => '420']
      ]       

  ]    

]

SrNo的最后一个角色是 O ,而不是它的返回。

我尝试了in_arrayarray_key功能,但我没有按照自己的需要获得输出。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

// somehow we need a common entry binding all the arrays to be considered together ...
$all2=array($array1,$array2,$array3,$array4); 
// collect journeys into a hash according to their `SrNo`
foreach ($all2 as $jrn) $coll[$jrn['SrNo']][]=$jrn; 

// rearrange ...
$k=0;
foreach ($coll as $jrn) {$res[++$k]['onwards']=$jrn[0];$res[$k]['return']=$jrn[1];} 

print_r($res);

这导致以下输出(参见here for a demo):

Array
(
    [1] => Array
        (
            [onwards] => Array
                (
                    [SrNo] => xyz
                    [AirlineCode] => 9E
                    [FlightNo] => 777
                )

            [return] => Array
                (
                    [SrNo] => xyz
                    [AirlineCode] => 6G
                    [FlightNo] => 546
                )

        )

    [2] => Array
        (
            [onwards] => Array
                (
                    [SrNo] => abc
                    [AirlineCode] => 5H
                    [FlightNo] => 423
                )

            [return] => Array
                (
                    [SrNo] => abc
                    [AirlineCode] => 2G
                    [FlightNo] => 420
                )

        )

)

答案 1 :(得分:0)

以下不是完全要求的解决方案。是一种替代方案,易于阅读且易于管理。如果密钥是SrNo,...只需将SrN0存储为密钥,而不是数字索引。

<?php

$array1 = array('SrNo' => 'xyzO' , 'AirlineCode' => '9E' , 'FlightNo' => '777');
$array2 = array('SrNo' => 'xyzR' , 'AirlineCode' => '6G' , 'FlightNo' => '546');
$array3 = array('SrNo' => 'abcO' , 'AirlineCode' => '5H' , 'FlightNo' => '423');
$array4 = array('SrNo' => 'abcR' , 'AirlineCode' => '2G' , 'FlightNo' => '420');

$collection = [
    $array1,
    $array2,
    $array3,
    $array4,
];

$newCollection = [];

passthru('clear');

foreach ($collection as $itemKey => $itemValue) {
    $key = substr($itemValue['SrNo'], 0, 3);
    if (!isset($newCollection[$key])) {
        $newCollection[$key] = [
            'onwards' => $itemValue,
        ];
    } else {
        $newCollection[$key]['return'] = $itemValue;
    }
}

echo json_encode($newCollection, JSON_PRETTY_PRINT);

将输出:

{
    "xyz": {
        "onwards": {
            "SrNo": "xyz",
            "AirlineCode": "9E",
            "FlightNo": "777"
        },
        "return": {
            "SrNo": "xyz",
            "AirlineCode": "6G",
            "FlightNo": "546"
        }
    },
    "abc": {
        "onwards": {
            "SrNo": "abc",
            "AirlineCode": "5H",
            "FlightNo": "423"
        },
        "return": {
            "SrNo": "abc",
            "AirlineCode": "2G",
            "FlightNo": "420"
        }
    }
}

如果您愿意,我们可以找到一种强制键编号的方法。

$position = 0;
foreach ($newCollection as $itemKey => $itemValue) {
    $newCollection[++$position] = $itemValue;
    unset($newCollection[$itemKey]);
}

echo json_encode($newCollection, JSON_PRETTY_PRINT);

这将输出:

{
    "1": {
        "onwards": {
            "SrNo": "xyz",
            "AirlineCode": "9E",
            "FlightNo": "777"
        },
        "return": {
            "SrNo": "xyz",
            "AirlineCode": "6G",
            "FlightNo": "546"
        }
    },
    "2": {
        "onwards": {
            "SrNo": "abc",
            "AirlineCode": "5H",
            "FlightNo": "423"
        },
        "return": {
            "SrNo": "abc",
            "AirlineCode": "2G",
            "FlightNo": "420"
        }
    }
}