0

Is there any possibility to explode values of an array to create direct a new array with these values. I'm able to do this only with a intemidiate step.

I have following array:

Array (
[0] => OtherMachines, Polymers
[1] => Engines/Pumps/Turbines, Measurement
[2] => Materials/Metallurgy
[3] => Materials/Metallurgy, ThermProcesses
)

Then I explode its values:

foreach ($array00 as $v) {
    $array01 = explode(", ", $v);
}

and receive following array:

Array
(
[0] => Array
    (
        [0] => OtherMachines
        [1] => Polymers
    )

[1] => Array
    (
        [0] => Engines/Pumps/Turbines
        [1] => Measurement
    )

[2] => Array
    (
        [0] => Materials/Metallurgy
    )

[3] => Array
    (
        [0] => Materials/Metallurgy
        [1] => ThermProcesses
    )
)

Than I have to flatten the array to get the one I wish:

// Flatten the array.
$array02 = call_user_func_array('array_merge', $array01);

Final result:

Array (
[0] => OtherMachines
[1] => Polymers
[2] => Engines/Pumps/Turbines
[3] => Measurement
[4] => Materials/Metallurgy
[5] => Materials/Metallurgy
[6] => ThermProcesses
)

Is there any way to do this direct?

Many thanks in advance.

0

3 Answers 3

3

Yes, just implode it and then explode.

$result = explode(', ', implode(', ', $array));
Sign up to request clarification or add additional context in comments.

Comments

1
$arr = array (
    'OtherMachines, Polymers',
    'Engines/Pumps/Turbines, Measurement',
    'Materials/Metallurgy',
    'Materials/Metallurgy, ThermProcesses'
);

print_r(array_map('trim', explode(',', implode(',', $arr))));

Array
(
    [0] => OtherMachines
    [1] => Polymers
    [2] => Engines/Pumps/Turbines
    [3] => Measurement
    [4] => Materials/Metallurgy
    [5] => Materials/Metallurgy
    [6] => ThermProcesses
)

http://codepad.org/h0n6NU3V

Comments

0

Or simply:

$arr = array (
    'OtherMachines, Polymers',
    'Engines/Pumps/Turbines, Measurement',
    'Materials/Metallurgy',
    'Materials/Metallurgy, ThermProcesses'
);

print_r(explode(', ', implode(', ', $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.