skip to Main Content

I have an array returned from a form as below:

[ticket_detail] => Array
        (
            [ticket_id] => Array
                (
                    [0] => 101
                    [1] => 102
                    [2] => 103
                )

            [price] => Array
                (
                    [0] => 10
                    [1] => 20
                    [2] => 30
                )
        )

I could extract a single column values using the below code:

$arr = array_map(function ($x) {
    return $x[0];
}, $ticket_detail);

Array
(
    [ticket_class_id] => 101
    [price] => 10
}

How do I combine this with array_walk so I can get the below result?

[ticket_detail] => Array
        (
            [0] => Array
                (
                    [ticket_id] => 101
                    [price] => 10
                )

            [1] => Array
                (
                    [ticket_id] => 102
                    [price] => 20
                )
            [2] => Array
                (
                    [ticket_id] => 103
                    [price] => 30
                )
        )

PS: I’m looking for an alternate solution without using a foreach loop.

2

Answers


  1. Here is the code to get your required result.

    $arr = array_map(function ($ticket_id, $price) {
        return array(
            'ticket_id' => $ticket_id,
            'price' => $price
        );
    }, $ticket_detail['ticket_id'], $ticket_detail['price']);
    
    $result = array('ticket_detail' => $arr);
    

    UPDATE

    Here is the way to make it more dynamic

    $keys = array_keys($ticket_detail);
    $arrayValues = array_values($ticket_detail);
    
    $arr = array_map(function (...$values) use ($keys) {
        return array_combine($keys, $values);
    }, ...$arrayValues);
    
    $result = array('ticket_detail' => $arr);
    
    Login or Signup to reply.
  2. Ok, since you wish to make this generic, you can do the below.

    • Get the subarray keys like ticket_id, price etc since they need to be attached as keys for each resultant row using array_keys.

    • Get no. of rows for any individual subarray(any because the rows would be symmetric, as in same for every other key).

    • Use array_walk to walk over these column values one by one. Use them as index in your array_column function on your ticket_detail array and use array_combine to combine the initially retrieved keys(as done in the 1st step) with the current row data picked up from each individual subarray and add them to the result.

    Snippet:

    <?php
    
    $keys = array_keys($ticket_detail['ticket_detail']);
    
    $result = ['ticket_detail' => []];
    $columns = range(1, count(current($ticket_detail['ticket_detail'])));
    
    array_walk($columns, function($index) use ($ticket_detail, $keys, &$result){
        $result['ticket_detail'][] = array_combine($keys, array_column($ticket_detail['ticket_detail'], $index - 1)); 
    });
    
    print_r($result);
    

    Live Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search