1

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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
phpseeker
  • 35
  • 5
  • i think this is answered here https://stackoverflow.com/questions/3684463/php-foreach-with-nested-array – Paddy Popeye Jun 07 '18 at 17:50
  • `"order_XXX" < "organization_YYY"` because `d < g` – Barmar Jun 07 '18 at 18:12
  • You might want to include what your expectations are, and show how reality is at odds with that. –  Jun 07 '18 at 18:24
  • @jdv I think that "I have this array" followed by "And I want to divide it into 3 arrays as below:" is a pretty good identifier of expectations especially considering that they have shown a solid effort with `array_filter()`. If you don't understand PHP then that's okay; just step back and learn for a while. – MonkeyZeus Jun 07 '18 at 18:35
  • @MonkeyZeus "It didn't work as expected" implies that we want to compare something with something else. Part of debugging is working from the known to the unknown, so showing real and expected results is part of that. Your snark about "learning" is unwelcome. Maybe step back if you don't know how to converse with others. And, like, *everyone* knows PHP. *rolls eyes* –  Jun 07 '18 at 19:12
  • @jdv No worries, just because you are not in the "everyone" crowd doesn't mean you should denounce valuable criticism. Beginners are often saddled with overconfidence. It is with experience that you are able to become more reserved. Please take as much time as you need. – MonkeyZeus Jun 07 '18 at 19:28

4 Answers4

1

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
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • @phpseeker Glad I could help – MonkeyZeus Jun 07 '18 at 19:23
  • @phpseeker If this helped you solve your problem, please accept the answer by clicking the checkmark icon next to this answer to mark the question as solved to the system. You will also get a little reputation for doing so! – geisterfurz007 Jun 08 '18 at 13:42
1

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]))));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
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);

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.

Progrock
  • 7,373
  • 1
  • 19
  • 25
0

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',
)
Progrock
  • 7,373
  • 1
  • 19
  • 25