0

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?

2
  • 1
    Your problem is that explode() returns an array. If you assign arrays as new elements into your target array $user_states you obviously get an array of arrays. Commented Oct 26, 2014 at 17:24
  • 1
    initialize a main array before the loop and merge your explode to that (array_merge) Commented Oct 26, 2014 at 17:26

7 Answers 7

2

[]= 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)
2
  • This makes perfect sense and is what I suspected, I'm just not sure how to merge the results of the exploded string into a single array. Commented Oct 26, 2014 at 17:26
  • @Niko 100% right, i just prefer using self descriptive method names when im answering on stackoverflow Commented Oct 26, 2014 at 17:34
1

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

1
  • 1
    Thank you for the answer. While it is correct, I'm going to give the credit to Dima for his clear explanation of the situation. Commented Oct 26, 2014 at 17:30
1

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

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.

1

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

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);
1
  • This would replace the elements in the array with each iteration of the foreach() loop Commented Oct 26, 2014 at 17:25
0

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

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.