0

I'm having specific problem, I've got 2 arrays:

One, which extracts specific fields from order data, it iterates and extracts id, title, amount and price as many times as many products are on current order, so it looks like this:

$array1 = array( 'id_1' => '2975', 'title_1' => 'first_title', 'amount_1' => '1', 'price_1' => 55, 'id_2' => '2973', 'title_2' => 'second_title', 'amount_2' => '1', 'price_2' => 95, )

There are 2 products on order, if there were 3 there would be another set of values, so I have another array, to which I'd like to map the values, it should be exatly in this form(it's transferred to another website so it only receives data in this form):

$products = Array(

Array
    (
        'id' => 'first_id',
        'title' => 'first_title',
        'amount' => 'first_amount',
        'price' =>  'first_price',
        'type' => 0,
        'installmenttype' => 0
    ),

Array
    (
        'id' => 'second_id',
        'title' => 'second_title',
        'amount' => 'second_amount',
        'price' =>  'second_price',
        'type' => 0,
        'installmenttype' => 0
    )

);

I need to map first four values for each sub array in $products variable, and I also need to have as many sub arrays as many products are in current order, it should work like first array in that regard.

I looked through this question: create multidimensional array using a foreach loop

But couldn't quite get there, so how should the code that achieves that look like?

4 Answers4

0
<?php
$array1 = array( 'id_1' => '2975', 'title_1' => 'first_title', 'amount_1' => '1', 'price_1' => 55, 'id_2' => '2973', 'title_2' => 'second_title', 'amount_2' => '1', 'price_2' => 95, );
$products = [];
if (!empty($array1)) {
    foreach ($array1 as $key => $val) {
        list($property,$id) = explode('_',$key);
        $id--; // make it zero based
        if (!isset($products[$id])) $products[$id] = [];
        $products[$id][$property] = $val;
    }
}

print_r($products);

resulting in

Array
(
    [0] => Array
        (
            [id] => 2975
            [title] => first_title
            [amount] => 1
            [price] => 55
        )

    [1] => Array
        (
            [id] => 2973
            [title] => second_title
            [amount] => 1
            [price] => 95
        )

)
David Hayes
  • 196
  • 8
  • It eventually get the result, but I also need to add these 2 key-value pairs:'type' => 0, 'installmenttype' => 0. these are not changing and are the same all the time. – Sandro Togonidze Aug 16 '17 at 18:44
  • You can just add them in the if block that initializes the $products[$id] = []; array. – David Hayes Aug 16 '17 at 19:08
  • `if (!isset($products[$id])) $products[$id] = [];` is not necessary because you are using square brace syntax (parent elements don't need to be initialized when using square brace pushing). – mickmackusa Sep 23 '22 at 09:54
0

You can use a series of native functions:

$new    =   array_map(function($v) {
        foreach($v as $key => $value) {
            unset($v[$key]);
            $key        =   preg_replace('/\_[0-9]{1,}/','',$key);
            $v[$key]    =   $value;
        }
        return $v;
    },array_chunk($array1,4,true));


print_R($new);

Gives you:

Array
(
    [0] => Array
        (
            [id] => 2975
            [title] => first_title
            [amount] => 1
            [price] => 55
        )

    [1] => Array
        (
            [id] => 2973
            [title] => second_title
            [amount] => 1
            [price] => 95
        )

)
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
0

You can do a while on count(array).
I use array_splice to cut the first four items to the new array.

$array1 = array('id_0' => '2975', 'title_0' => 'first_title', 'amount_0' => '1', 'price_0' => 55, 'id_1' => '2975', 'title_1' => 'first_title', 'amount_1' => '1', 'price_1' => 55, 'id_2' => '2973', 'title_2' => 'second_title', 'amount_2' => '1', 'price_2' => 95, );

$res =array();
While(count($array1)){
   $res[] = array_splice($array1, 0,4);
}
Var_dump($res);

https://3v4l.org/7Gt5k

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Define the keys that you want in the output array.

$keys = ['id', 'title', 'amount', 'price', 'type', 'installmenttype'];

Then map array_combine over 4-count chunks of your original array.

$products = array_map(function($product) use ($keys) {
    return array_combine($keys, array_merge($product, [0,0]));
}, array_chunk($array1, 4));

(array_merge adds the zero values for the extra keys so that array_combine can be used.)

Don't Panic
  • 41,125
  • 10
  • 61
  • 80