/    Sign up×
Community /Pin to ProfileBookmark

Please help me build a multi-dee array from a string.

$string = “1~2~3 4~5~6”;

What i want to end up with is:

$array[0][0] = 1;
$array[0][1] = 2;
$array[0][2] = 3;
$array[1][0] = 4;
$array[1][1] = 5;
$array[1][2] = 6;

I have tried all sorts of combinations of $array = explode(“~”, $string) etc but i cannot get it to work.

Please help if you’re able to ?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscJun 03.2009 — You need to do two explodes:

[code=php]
$string = $string = "1~2~3 4~5~6";
$array = array();
$array = explode(" ",$string);
$array[0] = explode("~",$array[0]);
$array[1] = explode("~",$array[1]);

foreach($array as $key => $value) {
if(is_array($value)) {
foreach ($value as $subkey => $subvalue) {
echo $key." ".$subkey." ".$subvalue."<br />";
}
}
}
[/code]
Copy linkTweet thisAlerts:
@BelnzauthorJun 03.2009 — You the man. Thank you so much. I knew I needed two explodes but couldnt wrap my damn mind around it.

Thank you once again ?
×

Success!

Help @Belnz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...