Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 itteration 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? Thanks!

share|improve this question
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. –  arkascha Oct 26 '14 at 17:24
1  
initialize a main array before the loop and merge your explode to that (array_merge) –  boulder_02 Oct 26 '14 at 17:26

6 Answers 6

up vote 2 down vote accepted

[]= 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)
share|improve this answer
    
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. –  Pete Sorensen Oct 26 '14 at 17:26
    
see edited answer, you need array_merge –  Dima Oct 26 '14 at 17:27
    
@Niko 100% right, i just prefer using self descriptive method names when im answering on stackoverflow –  Dima Oct 26 '14 at 17:34

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);
share|improve this answer
    
This would replace the elements in the array with each iteration of the foreach() loop –  Pete Sorensen Oct 26 '14 at 17:25
    
You are right. Searching for another solution... –  pbaldauf Oct 26 '14 at 17:27

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

share|improve this answer
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. –  Pete Sorensen Oct 26 '14 at 17:30

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);
share|improve this answer
    
thanks, Niko. Damn copy & paste ;) –  boulder_02 Oct 26 '14 at 17:33

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.

share|improve this answer

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
)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.