如何将这个多维数组分成三个指定的部分?

时间:2018-06-07 17:42:43

标签: php

我有这个数组

$data = [
    0 => [
        'id' => '114',
        'organization_name' => 'ABC Ltd',
        'organization_telephone' => '01234 112233',
        'organization_email' => 'admin@example.com',
        'organization_url' => 'http://www.example.com',
        'order_id' => '119',
        'order_delivery_address_1' => '55',
        'order_delivery_address_2' => 'High Street',
        'order_delivery_address_3' => '',
        'order_delivery_postcode' => 'LL27 0YX',
        'product_colour_name' => 'Red',
        'product_size_name' => '8/9',
        'product_price' => '7.5',
        'product_quantity' => '10',
        'product_line_price' => '75',
    ],
];

我想把它分成3个数组,如下所示:

$orgnization_details = [
    'id' => '114',
    'organization_name' => 'ABC Ltd',
    'organization_telephone' => '01234 112233',
    'organization_email' => 'admin@example.com',
    'organization_url' => 'http://www.example.com',
]

$order_details = [
    'order_id' => '119',
    'order_delivery_address_1' => '55',
    'order_delivery_address_2' => 'High Street',
    'order_delivery_address_3' => '',
    'order_delivery_postcode' => 'LL27 0YX',
]

$product_details = [
    'product_colour_name' => 'Red',
    'product_size_name' => '8/9',
    'product_price' => '7.5',
    'product_quantity' => '10',
    'product_line_price' => '75',
]

我尝试将array_filter用于orgnization_details,如下所示

foreach ($data as $details)
{
    $orgnization_details = array_filter($details, function($key) { return $key <= 'organization_url'; }, ARRAY_FILTER_USE_KEY);
}

但它并没有像预期的那样发挥作用。

请帮助我。

4 个答案:

答案 0 :(得分:1)

你非常接近。

需要修改array_filter()以检查密钥的开头是否为特定字符串:

$orgnization_details = array_filter($details, function($key){
    // do the first 13 chars equal "organization_" or is the key "id"?
    return substr( $key, 0, 13 ) === 'organization_' || $key === 'id';
}, ARRAY_FILTER_USE_KEY);

// Do similar logic for setting $order_details

// Do similar logic for setting $product_details

答案 1 :(得分:1)

您可以定义键并检查主阵列中键的交叉点:

$orginazation_keys = array_flip(['id',
                                 'organization_name',
                                 'organization_telephone',
                                 'organization_email',
                                 'organization_url' ]);

$orgnization_details = array_intersect_key($data[0], $orginazation_keys);

或者你可以为他们加油,因为他们遵循一种模式:

$orgnization_details = array_intersect_key($data[0],
                       array_flip(preg_grep('/^(organization|id)/', array_keys($data[0]))));

答案 2 :(得分:1)

<?php
$data = [
    0 => [
        'id' => '114',
        'organization_name' => 'ABC Ltd',
        'organization_telephone' => '01234 112233',
        'organization_email' => 'admin@example.com',
        'organization_url' => 'http://www.example.com',
        'order_id' => '119',
        'order_delivery_address_1' => '55',
        'order_delivery_address_2' => 'High Street',
        'order_delivery_address_3' => '',
        'order_delivery_postcode' => 'LL27 0YX',
        'product_colour_name' => 'Red',
        'product_size_name' => '8/9',
        'product_price' => '7.5',
        'product_quantity' => '10',
        'product_line_price' => '75',
    ],
];

$prefix_map = [
    'id' => 'organization_details',
    'organization' => 'organization_details',
    'order' => 'order_details',
    'product' => 'product_details'
];
foreach($data[0] as $k => $v) {
    $key_prefix = explode('_', $k)[0];
    $new_key = $prefix_map[$key_prefix];
    $out[$new_key][$k] = $v;
}
extract($out);
var_export($organization_details);

输出:

array (
    'id' => '114',
    'organization_name' => 'ABC Ltd',
    'organization_telephone' => '01234 112233',
    'organization_email' => 'admin@example.com',
    'organization_url' => 'http://www.example.com',
  )

通过映射现有的键前缀(下划线之前的部分),创建一个带有相应关联键的新多维数组。然后,只是在该数组上使用extract来创建命名变量。

这会产生三个变量:$ organization_details,$ order_details和$ product_details。

答案 3 :(得分:0)

如果您的子阵列始终处于相同的顺序,则只需切片:

 <?php

 $data = [ 
    0 => [
        'id' => '114',
        'organization_name' => 'ABC Ltd',
        'organization_telephone' => '01234 112233',
        'organization_email' => 'admin@example.com',
        'organization_url' => 'http://www.example.com',
        'order_id' => '119',
        'order_delivery_address_1' => '55',
        'order_delivery_address_2' => 'High Street',
        'order_delivery_address_3' => '',
        'order_delivery_postcode' => 'LL27 0YX',
        'product_colour_name' => 'Red',
        'product_size_name' => '8/9',
        'product_price' => '7.5',
        'product_quantity' => '10',
        'product_line_price' => '75',
    ]
 ];
$first = $data[0];
$organisation_details = array_slice($first, 0, 5);
$order_details        = array_slice($first, 5, 5);
$product_details      = array_slice($first, -5);

var_export($organisation_details);
var_export($order_details);
var_export($product_details);

输出:

 array (
  'id' => '114',
  'organization_name' => 'ABC Ltd',
  'organization_telephone' => '01234 112233',
  'organization_email' => 'admin@example.com',
  'organization_url' => 'http://www.example.com',
)array (
  'order_id' => '119',
  'order_delivery_address_1' => '55',
  'order_delivery_address_2' => 'High Street',
  'order_delivery_address_3' => '',
  'order_delivery_postcode' => 'LL27 0YX',
)array (
  'product_colour_name' => 'Red',
  'product_size_name' => '8/9',
  'product_price' => '7.5',
  'product_quantity' => '10',
  'product_line_price' => '75',
)