Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to array with string issue

Tags:

arrays

regex

php

Having a little issue with RegEx, I have the strings

AM.name:ASC,AMAdvanced.start:DESC,

AMAdvanced.start:DESC,AM.Genre:Action

and need to break them into

array(0){

AM.name => ASC,

AMAdvanced.start => DESC
}

and 

array(0){

AMAdvanced.start => DESC,

AM.Genre => Action

}

Any help would be fantastic since completely new to regex

like image 990
B1scuit Avatar asked May 29 '26 00:05

B1scuit


1 Answers

No need of regex here.

Steps:

  1. explode by comma ,
  2. Loop over exploded array
  3. Explode by colon :
  4. Push into new array

Code:

$newArr = array();
foreach (explode(',', trim($str, " ,")) AS $el) {
    $el = explode(':', $el);
    $newArr[$el[0]] = $el[1];
}
print_r($newArr);
like image 67
Tushar Avatar answered May 31 '26 13:05

Tushar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!