3

I have a 2D array and I want to generate a formatted array. Actually I want to genetate multiple rows at a time by restructuring the input array.

My 2D array:

$occupied_ids = [
    [8457, 6584],
    [9874, 4586],
];

Expected output:

array (
  0 => 
  array (
    'occupied_id' => 8457,
    'feed' => 2,
    'status' => 1,
    'status_date' => '2022-09-13',
  ),
  1 => 
  array (
    'occupied_id' => 6584,
    'feed' => 2,
    'status' => 1,
    'status_date' => '2022-09-13',
  ),
  2 => 
  array (
    'occupied_id' => 9874,
    'feed' => 2,
    'status' => 1,
    'status_date' => '2022-09-13',
  ),
  3 => 
  array (
    'occupied_id' => 4586,
    'feed' => 2,
    'status' => 1,
    'status_date' => '2022-09-13',
  ),
)

My working code:

foreach($occupied_ids as $ele){
    $attributes = array_map(function($v){
        $feed = isset($_GET['feed']) ? $_GET['feed'] : 2;
        $status = 1;
        return [
            'occupied_id'   =>  $v,
            'feed'          =>  $feed,
            'status'        =>  $status,
            'status_date'   =>  date('Y-m-d'),
       ];
    }, $ele);

    /*Call function to multi-insert*/
    //multi_insert($attributes);
}

But I'm looking for something a way without the outer loop.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

3 Answers3

1

Not sure how much better it is but I took a stab at it. Off to lunch but I'll revisit in a bit to see what can be optimized:

$occupied_ids = call_user_func_array('array_merge', $occupied_ids);

$temp = [   'feed'          =>  isset($_GET['feed']) ? $_GET['feed'] : 2,
            'status'        =>  1,
            'status_date'   =>  date('Y-m-d'),
       ];

$attributes = array_map(function($v) use($temp) {
                            $r = $temp;
                            $r['occupied_id'] = $v;
                            return $r;
                        },
                        $occupied_ids);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

Use array_reduce to flatten the first level of the array

$occupied_ids = [
    [8457, 6584],
    [9874, 4586],
];

function format_elem($v)
{
        $feed = isset($_GET['feed']) ? $_GET['feed'] : 2;
        return [
            'occupied_id'   =>  $v,
            'feed'          =>  $feed,
            'status'        =>  1,
            'status_date'   =>  date('Y-m-d'),
       ];
}
$ids=array_reduce($occupied_ids,'array_merge',[]);
$attributes =array_map(format_elem,$ids);
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
0

By pre-flattening your data (by spreading the rows inside of an array_merge() call), you can call array_map() instead of array_reduce() because now the input will have the same number of rows as the desired output.

Code: (Demo)

var_export(
    array_map(
        fn($v) => [
            'occupied_id'   =>  $v,
            'feed'          =>  $_GET['feed'] ?? 2,
            'status'        =>  1,
            'status_date'   =>  date('Y-m-d'),
        ],
        array_merge(...$occupied_ids)
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136