阵列操作,用于将条目从1添加到另一个

时间:2017-04-28 03:50:25

标签: php arrays laravel

我有两组数组:

数据:

$data = [
    [
        'company_code' => 'ABC',
        'supplier_codes' => [
            'S-2',
            'S-3',
            'S-5',
        ],
    ],
];

来源(来自数据库):

$database = [
    'company_code' => 'ABC',
    'suppliers' => [
        [
            'code' => 'S-1',
        ],
        [
            'code' => 'S-2',
            'reference' => '12345'
        ],
        [
            'code' => 'S-3',
        ],
        [
            'code' => 'S-4',
            'reference' => 'some string',
        ]
    ],
];

我需要实现的目标:

  • 如果$data中缺少供应商代码但$database中存在供应商代码, 将其从$database
  • 中删除
  • 如果$data中存在供应商代码但$database中缺少供应商代码,请将其添加到$database

此处示例的输出应如下所示:

$output = [
    'company_code' => 'ABC',
    'suppliers' => [
        [
            'code' => 'S-2',
            'reference' => '12345'
        ],
        [
            'code' => 'S-3',
        ],
        [
            'code' => 'S-5',
        ]
    ],
];

我正在考虑删除suppliers子阵列,然后根据supplier_codes中的数据重建结构。但问题是suppliers中的某些条目可能包含一个名为reference的可选字段。

2 个答案:

答案 0 :(得分:0)

试试这个

roomListData = {<uuid> : {roomCateghory : Single, number : 2}, 
                <uuid1> : {roomCateghory : Double, number : 2} }

答案 1 :(得分:0)

我最终以这种方式解决了我的问题:

print_r($result);

Array ( [company_code] => ABC [suppliers] => Array ( [0] => Array ( [code] => S-2 [reference] => 12345 ) [1] => Array ( [code] => S-3 ) [2] => Array ( [code] => S-5 ) ) ) 的输出:

 - (void) textFieldDidBeginEditing:(UITextField *)textField
    {
         if (textField == self.countryTextField)
         {
            // pass your countryListArray here in picker view
         }
         else if (textField == self.cityTextField)
        {
           // get city for selected country here.
           // Call web service for city names.
        }
}