psorensen
psorensen

Reputation: 827

Explode all delimited strings in a flat array into a flat array of individual substrings

I have a loop that contains a string of comma seperated values.

foreach ($profiles as $profile) {
    $user_states[] = exlpode(', ', '[string of comma seperated states]');
}

The problem I'm experiencing is the $user_states array ends up being two levels, with each iteration of the loop creating a nested array.

array (size=2)
  0 => 
    array (size=3)
      0 => string 'DC' (length=2)
      1 => string 'Maryland' (length=8)
      2 => string 'Northern-Virginia' (length=17)
  1 => 
    array (size=1)
      0 => string 'North-Carolina,Virginia' (length=23)

How can I take exploded values and place them all into a single array?

Upvotes: 0

Views: 892

Answers (7)

mickmackusa
mickmackusa

Reputation: 47863

It is indirect to bother making iterated explosions and merge calls.

Just implode the entire 1d array using the same delimiter that you explode each string with, then explode THAT single string.

Code: (Demo)

$array = [
    'DC, Maryland, Northern-Virginia',
    'North-Carolina, Virginia',
];

var_export(explode(', ', implode(', ', $array)));
/*
array (
  0 => 'DC',
  1 => 'Maryland',
  2 => 'Northern-Virginia',
  3 => 'North-Carolina',
  4 => 'Virginia',
)
*/

If you absolutely need to use a loop (perhaps because you are doing other operations per iteration, then don't bother merging; use array_push() for the only thing it is good for -- pushing multiple elements into an array at once. Use the spread operator to allow array_push() to push the generated elements individually into the result array.

Code: (Demo)

$result = [];
foreach ($array as $string) {
    array_push($result, ...explode(', ', $string));
}
var_export($result);
// same result as previous snippet

Upvotes: 0

pbaldauf
pbaldauf

Reputation: 1681

Did you try this

$user_states = exlpode(', ', '[string of comma seperated states]');

EDIT:

If I am not wrong this code helps you

$profiles = array( "yale, ny, la", "boston, vegas");

$user_states = array();

foreach ($profiles as $profile) {

    $tmp = explode(', ', $profile);
  $user_states = array_merge( $tmp, $user_states);
}


var_dump($user_states);

Upvotes: 1

BentCoder
BentCoder

Reputation: 12730

Since I don't know what you have in $profiles, I'm giving you a simple example.

$user_states = array();
$profiles = array('UK, FR, CA, AU', 'UK, FR, CA, AU', 'NW');

foreach ($profiles as $profile)
{
    $extract = explode(', ', $profile);
    $user_states = array_merge($user_states, $extract);
}

// if you want to remove duplications
$user_states = array_unique($user_states);

echo '<pre>';
print_r($user_states);

Will give you:

Array
(
    [0] => UK
    [1] => FR
    [2] => CA
    [3] => AU
    [8] => NW
)

AND

If you don't use array_unique()

Array
(
    [0] => UK
    [1] => FR
    [2] => CA
    [3] => AU
    [4] => UK
    [5] => FR
    [6] => CA
    [7] => AU
    [8] => NW
)

Upvotes: 1

Titi
Titi

Reputation: 1153

You can try

$user_states = array();
...
$user_states += explode(', ', '[string of comma seperated states]');
...

This will keep adding the 'explode' arrays to the main $user_states array.

Upvotes: 1

boulder_02
boulder_02

Reputation: 301

Use the merging function:

$states=array();

foreach ($profiles as $profile) {
    $user_states = exlpode(', ', '[string of comma seperated states]');
    array_merge($states,$user_states);
}

var_dump($states);

Upvotes: 1

Dima
Dima

Reputation: 8652

[]= operator means add to array. explode method, returns an array, so what you are doing is adding an array into array.

since profiles probably contains 2 elements, you are getting an array of size 2 of exploded strings

what you are probably looking for is array_merge

replace the inner part of the loop with this:

$exploded    = exlpode(', ', '[string of comma seperated states]');
$user_states = array_merge($user_states, $exploded)

Upvotes: 2

karailiev
karailiev

Reputation: 124

What you need is:

$user_states = array();
foreach ($profiles as $profile) {
  $user_states = array_merge($user_states, exlpode(', ', '[string of comma seperated states]'));
}

Regards, Valentin

Upvotes: 1

Related Questions