
Question:
I have this array
$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',
],
];
And I want to divide it into 3 arrays as below:
$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',
]
I tried using array_filter
for orgnization_details as below
foreach ($data as $details)
{
$orgnization_details = array_filter($details, function($key) { return $key <= 'organization_url'; }, ARRAY_FILTER_USE_KEY);
}
But it didn't work as expected.
Please help me with this.
Answer1:You are very close.
The array_filter()
needs to be modified to check if the beginning of the key is a specific string:
$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
Answer2:You could define the keys and check for an intersection of the keys in the main array:
$orginazation_keys = array_flip(['id',
'organization_name',
'organization_telephone',
'organization_email',
'organization_url' ]);
$orgnization_details = array_intersect_key($data[0], $orginazation_keys);
Or you can grep for them since they follow a pattern:
$orgnization_details = array_intersect_key($data[0],
array_flip(preg_grep('/^(organization|id)/', array_keys($data[0]))));
Answer3:
<?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);
Output:
array (
'id' => '114',
'organization_name' => 'ABC Ltd',
'organization_telephone' => '01234 112233',
'organization_email' => 'admin@example.com',
'organization_url' => 'http://www.example.com',
)
This creates a new multi-dimensional array with the corresponding associated keys by mapping the existing key prefixes (part before the underscore). Then it's just a case of using extract on that array to create the named variables.
This results in the three variables: $organization_details, $order_details and $product_details that you desire.
Answer4:If your sub-arrays are consistently in the same order you can simply slice:
<?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);
Output:
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',
)