Split the strings of a flat array to create an array of associative rows
Asked Answered
H

3

0

How to make an element array inside an array explode again. I have an array like this

echo "<pre>";
print_r($pks);

The Result.

Array
(
    [0] => 2017-04-15||KMTC_HOCHIMINH
    [1] => 2017-04-15||OOCL_NAGOYA
)

I expected like this,

Array
(
    [record] => 
       [1] 2017-04-15
       [2] KMTC_HOCHIMINH
    [record] => 
       [1] 2017-04-15
       [2] OOCL_NAGOYA
)

What keys on php to process array like this. Please advise.

UPDATE How about this .

Array
(
[0] => Array
    (
        [date] => 2017-04-15
        [vessel] => KMTC_HOCHIMINH
    )

[1] => Array
    (
        [date] => 2017-04-15
        [vessel] => OOCL_NAGOYA
    )

)
Helve answered 19/5, 2017 at 2:41 Comment(0)
B
1

You can use array_walk() (or just foreach if you want):

array_walk($pks, function(&$a) {
    $a = array_combine(['date', 'vessel'], explode('||', $a));
});

Foreach method:

foreach($pks as $k => $v) {
    $pks[$k] = array_combine(['date', 'vessel'], explode('||', $v));
}

However, the key of each array won't be record, since it's impossible to have the same key multiple times.

Output:

Array
(
    [0] => Array
        (
            [date] => 2017-04-15
            [vessel] => KMTC_HOCHIMINH
        )

    [1] => Array
        (
            [date] => 2017-04-15
            [vessel] => OOCL_NAGOYA
        )

)

Bonus method because I like messing with arrays:

$pks = array_map('explode', array_fill(0, count($pks), '||'), $pks);
Blocker answered 19/5, 2017 at 2:46 Comment(0)
W
1

Try this, check the live demo

foreach($pks as $k => $v) {
    $values = explode('||', $v);
    $result[] = array_combine(range(1, count($values)), $values);

    //added
    $res[] = array_combine(['date', 'vessel'], $values) ;

}

result

Array
(
    [0] => Array
        (
            [1] => 2017-04-15
            [2] => KMTC_HOCHIMINH
        )

    [1] => Array
        (
            [1] => 2017-04-15
            [2] => OOCL_NAGOYA
        )

)
Whipperin answered 19/5, 2017 at 3:3 Comment(1)
@FadlyDzil check it nowWhipperin
T
0

Some more solutions which provide the same, desired result. Demo

$arr = [
    '2017-04-15||KMTC_HOCHIMINH',
    '2017-04-15||OOCL_NAGOYA',
];

Make iterated get_defined_vars().

var_export(
    array_map(
        fn($v) => (fn($date, $vessel) => get_defined_vars())(...explode('||', $v)),
        $arr
    )
);

Make iterated array_combine() calls.

var_export(
    array_map(
        fn($v) => array_combine(['date', 'vessel'], explode('||', $v)),
        $arr
    )
);

Push reference variables into the result.

$result = [];
foreach ($arr as $v) {
    $result[] =& $row;
    [$row['date'], $row['vessel']] = explode('||', $v);
}
var_export($result);

Modify by reference using an Immediately Invoked Functional Expression.

foreach ($arr as &$v) {
    $v = (fn($date, $vessel) => get_defined_vars())(...explode('||', $v));
}
var_export($arr);
Tempera answered 28/9, 2024 at 13:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.