-1

How to convert a string to an array in PHP? I've a string like this:

$str = "php/127/typescript/12/jquery/120/angular/50";

The output:

Array (
    [php]=> 127
    [typescript]=> 12
    [jquery]=> 120
    [angular]=> 50
)
Zhorov
  • 28,486
  • 6
  • 27
  • 52
  • 1
    Does this answer your question? [PHP Split Delimited String into Key/Value Pairs (Associative Array)](https://stackoverflow.com/questions/5290342/php-split-delimited-string-into-key-value-pairs-associative-array) – whilrun Jun 23 '20 at 07:23
  • Since this is closed I put a simple PHP way as answer to the one above. Look at `Split into segments and loop thru incrementing by two.`. I was wondering why all are juggling with costly regex, when it is a simple for loop. – Markus Zeller Jun 23 '20 at 07:29
  • @Markus Zeller i am giving as your comment & result is coming so why i giving down vote insted of up vote? – KUMAR Jun 23 '20 at 07:48
  • @KUMAR I did not see your answer while I was writing mine. Downvote did not come from me. Btw, you need to count() - 1. When you post a faulty answer too quickly you must expect downvotes. – Markus Zeller Jun 23 '20 at 07:53
  • @MarkusZeller `count() - 1` is not needed or technically correct, the for loop is checking for less than (`<`) not less than or equal (`<=`). – Geoffrey Jun 23 '20 at 07:59
  • When count is not even (for example extra slash at the end without a value) and you use $i+1 in the loop, you are out of bounds. – Markus Zeller Jun 23 '20 at 08:04
  • True, but that is not what the OP has presented as sample data, and this subtraction would be an extra operation to perform uselessly. – Geoffrey Jun 23 '20 at 08:09
  • Handling (possible and predictable) errors is a style of good code. A -1 does not cost too much or will over complicate that. – Markus Zeller Jun 23 '20 at 08:20

3 Answers3

2

You can use preg_match_all (Regular Expression), and array_combine:

RegEx used : ([^\/]*?)\/(\d+), eplanation here (by RegEx101)

$str = "php/127/typescript/12/jquery/120/angular/50";

#match string
preg_match_all("/([^\/]*?)\/(\d+)/", $str, $match);

#then combine match[1] and match[2] 
$result = array_combine($match[1], $match[2]);

print_r($result);

Demo (with steps) : https://3v4l.org/blZhU

Illya
  • 1,268
  • 1
  • 5
  • 16
0

One approach might be to use preg_match_all to extract the keys and values from the path separately. Then, use array_combine to build the hashmap:

$str = "php/127/typescript/12/jquery/120/angular/50";
preg_match_all("/[^\W\d\/]+/", $str, $keys);
preg_match_all("/\d+/", $str, $vals);
$mapped = array_combine($keys[0], $vals[0]);
print_r($mapped[0]);

This prints:

Array
(
    [0] => php
    [1] => typescript
    [2] => jquery
    [3] => angular
)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • It is very useful. Thanks for reading and the solution. The code's beauty. – Duykhang Phamthe Jun 23 '20 at 07:42
  • Downvoter: While perhaps not the most optimal approach for _this_ question, one can easily imagine a scenario where making two separate calls to `preg_match_all` might be beneficial, e.g. if the keys/values were not always adjacent, and continuous throughout the path string. – Tim Biegeleisen Jun 23 '20 at 08:04
-2

You can use explode() with for()Loop as below:-

<?php
    
    $str = 'php/127/typescript/12/jquery/120/angular/50';
    $list = explode('/', $str);
    $list_count  = count($list);

    $result = array();
    for ($i=0 ; $i<$list_count; $i+=2) {
        $result[ $list[$i] ] = $list[$i+1];
    }
    
    print_r($result);
    ?>

Output:-

 Array
    (
        [php] => 127
        [typescript] => 12
        [jquery] => 120
        [angular] => 50
    )
    

Demo Here :- https://3v4l.org/8PQhd

KUMAR
  • 1,993
  • 2
  • 9
  • 26