2

The string:

user:hello,user2:world

Desired Output:

$string = array(
   1 => array( 1 => "user", 2 => "hello"),
   2 => array( 1 => "user2", 2 => "world")
);

What I've tried (that doesn't work):

$string = explode(',',$string);
$string = explode(':',$string);

The error I get: explode() expects parameter 2 to be string
How can I get from the string to the desired output? Thanks!

4
  • ................. Didn't expect this question, use explode then a foreach loop then explode again ... Commented May 15, 2013 at 23:22
  • loop over the output from the first explode and explode the second time on each value. Commented May 15, 2013 at 23:22
  • when you explode a string you obtain an array of strings not a string Commented May 15, 2013 at 23:23
  • Here you go. Commented May 15, 2013 at 23:28

4 Answers 4

6

loop over the output from the first explode and explode the second time on each value.

$string = "user:hello,user2:world";
$array = explode(',', $string);

foreach($array as $k=>$v){
    $array[$k] = explode(':', $v);
}
Sign up to request clarification or add additional context in comments.

4 Comments

just one little improvement: foreach($array as &$v) $v = explode(':', $v); see codepad.viper-7.com/5ExRH1
there is no need to make a reference out of $v, when he is reassigning the value through the key. either way works, to be honest
@michi I just prefer not to use references with foreach loops, especially with really generic variable names like $v simply because I don't like leaving the open reference to $v when the loop is done. If a value later on assigns to $v, it will still have the reference to the last entry from the loop unless you explicitly unset($v). I find setting the value in the array using the key to be more readable as well. Example: codepad.viper-7.com/tnIVSu If you leave out the possibility for the error of not unset'ing the reference, I feel it makes for better code.
@JonathanKuhn: Right, this can be a problem.
5
<?php

$string = "user:hello,user2:world";

$array = array_map(function ($input) {
    return explode(':',$input);
}, explode(',', $string));

print_r($array);

Comments

3

Please try this:

$string = 'user:hello,user2:world';
$output = explode(',',$string);
foreach ($output as &$e) {
    $e = explode(':', $e);
}
print_r($output);

1 Comment

Don't forget to doing unset($e); after foreach. It may cause a new issue.
0

You can use example of user from manual.

function multiexplode ($delimiters,$string) {
    $ary = explode($delimiters[0],$string);
    array_shift($delimiters);
    if($delimiters != NULL) {
        foreach($ary as $key => $val) {
             $ary[$key] = multiexplode($delimiters, $val);
        }
    }
    return  $ary;
}

// Example of use
$string = "1-2-3|4-5|6:7-8-9-0|1,2:3-4|5";
$delimiters = Array(",",":","|","-");

$res = multiexplode($delimiters,$string);
echo '<pre>';
print_r($res);
echo '</pre>';

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.