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
    )

)

3 Answers 3

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

@FadlyDzil check it now
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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.