flux
flux

Reputation: 1648

Using explode to add contents to existing array

I have this statement inside a loop.

$chosenUrls = explode ( ',' , $connect[$i]['url'] );

Is there anyway to get it to add to the $chosenUrls array, rather than replacing what's in there?

Upvotes: 1

Views: 1756

Answers (3)

pat34515
pat34515

Reputation: 1979

Your current code tells it to replace $chosenUrls every time. You need to modify it:

for(...){

 $chosenUrls[] = explode ( ',' , $connect[$i]['url'] );

}

Note the [] after $chosenUrls. This will push a new element into $chosenUrls on each iteration.

Upvotes: 1

Floricel
Floricel

Reputation: 801

You can have $chosenUrls[] = explode ( ',' , $connect[$i]['url'] ) to add new url in your array in every iteration.

Upvotes: 0

Stoia Alex
Stoia Alex

Reputation: 161

Try:

$chosenUrls=array();

 for(...)
{
  array_push($chosenUrls,explode ( ',' , $connect[$i]['url'] ));
}

Upvotes: 1

Related Questions