2

I have a string which contains quite a bit of data. I want to split the data into a 2D array. The data in the string is split by a ~ (tilde) for the columns and a : (colon) for the different rows.

An example string could be: "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry".

Thanks.

3
  • Can you give an example of what the result should be? Commented Feb 5, 2011 at 21:38
  • what is 2D array ? can you give an example ? Did you mean this : $arr[first][second] Commented Feb 5, 2011 at 21:38
  • possible duplicate of PHP - Loopless way to split a string into a multidimensional array Commented Feb 16, 2014 at 0:49

5 Answers 5

9
$string = "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry";
$array = explode(':', $string);
foreach($array as &$value) $value = explode('~', $value);
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant. Thank you. If my string were to start with a ':' how would I stop the first line of the array being empty?
either remove the colon if ($string[0] == ':') $string = substr($string, 1);, or remove the empty line if (count($array) && !strlen($array[0][0])) $array = array_slice($array, 1); (edit empty no good. changed to strlen approach)
4

Functional way (PHP 5.3.x needed):

$string = "London~10~20~cold:New York~23~53~hot:Madrid~43~12~dry";
$map = array_map(function($el) {
    return explode('~', $el);
}, explode(':', $string));

Comments

3

Another alternative would be:

preg_match_all('/(.*?)~(.*?)~(.*?)~(.*?)(?:$|:)/', $string, $array,
PREG_SET_ORDER);

It's more cumbersome in that you have to predefine the column format. It also returns the complete match in each rows [0]. Otherwise (due to PREG_SET_ORDER) it's in your desired 2d format.

Just posting it here to please and annoy the microoptimizers at the same time. Despite the common Stackoverflow meme, the regex is three times faster than the explode loop.

1 Comment

COuldn't believe that about speed. And tested. Well you are right about that. Though the format is not exactly correct since $array[x][0] contains the complete line which is not desired. Additionally your solution is less readable I think. Anyway, thanks for pointing that out about performance...
0

You can split data in php with explode(). So first you have to split the string, than you have to split your entries again with explode().

$data = explode(':', $string);
$array = array();
foreach($data as $d) {
    $d = explode('~', $d);
    $array[] = $d; //e.g. $array[0][0] --> London
    //$array[$d[0]] = array('temperature' => $d[1], 'dont-know' => $d[2], 'climate' => $d[3]); //e.g. $arra['London'] => array(...)
}

Comments

0

A "functional" style

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

Comments

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.