82835

How to make this multidimensional array into three specified parts?

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', )

Recommend

  • Not getting all the results
  • WCF Data Services join query
  • Trying to Scrape multiple urls, can only scrape 1. (Any way to generate multiple URL list)?
  • PHP two-dimensional array with empty items
  • How to bind varying number of inputs when some are blob and must be sent send_long_data() in PHP
  • Detect which condition is false in multiple if statement
  • Cannot assign method group to an implicitly-typed local variable in asp.net,linq,c#
  • Filter array by first letter
  • Put Values in Multidimensional Array keys
  • Could an iterative function call itself?
  • cakephp Paginator -> sort - model option
  • how to cast an anonymous type to IQueryable in C#?
  • Android - How to generate touch event from javascript?
  • Retrieve purchased information in In-App purchase
  • how to display data by using AJAX?
  • OleDBConnection Connection string
  • Automating table/object name scan and search in SAS
  • Using LINQ with IBM i
  • What is wrong in my MVC implementation?
  • ORA-12154: TNS:could not resolve the connect identifier specified
  • SSRS 2008 - Sorting within a group
  • Why is it still possible to insert a foreign key that doesn't exist?
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • Shopify API CARTS - Changing line_item line_price for price Override
  • How to get real device model in Android?
  • Unique Permutations - with exceptions
  • Returning the auto incrementing value after an insert using slick
  • Xaml, wpf image position and crop issue
  • Where these are stored?
  • Magento get URL before current
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Master page gives error
  • XCode can't find symbols for a specific iOS library/framework project
  • Matrix multiplication with MKL
  • Getting Messege Twice Using IMvxMessenger
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?