0

i have created a function in php to convert an string like:

[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 20110924, [20110926, 20110927], 20110929]

to php array like:

Array
(
    [0] => 20110911
    [1] => 20110913
    [2] => Array
        (
            [0] => 20110915
            [1] => 20110918
        )

    [3] => 20110920
    [4] => 20110922
    [5] => 20110924
    [6] => Array
        (
            [0] => 20110926
            [1] => 20110927
        )

    [7] => 20110929
    [8] => Array
        (
            [0] => 20111001
            [1] => 20111002
        )

    [9] => 20111004
    [10] => Array
        (
            [0] => 20111006
            [1] => 20111007
        )

)

The function is:

function dates2Array($d){
    if($d!==''){
        $d=substr($d, 1, strlen($d)-2);
        $d=explode(', ', $d);
        $dates=array();
        if(!empty($d)){
            $j=1;
            foreach($d as $k=>$v){
                if(substr($v, 0, 1)==='[') $dates[]=array(substr($v, 1, strlen($v)));
                elseif(substr($v, strlen($v)-1, strlen($v))===']'){
                    $dates[$k-$j][1]=substr($v, 0, strlen($v)-1);
                    $j++;
                }
                else $dates[]=$v;
            }
        }
    }

    return $d!==''?$dates:'';
}

I am not fully happy with my function. I think it can be more optimized and compressed for speed.. Can it be?

2 Answers 2

8

Use JSON (json_decode() and json_encode()) instead

http://sandbox.phpcode.eu/g/b3814/2

result:

Array
(
    [0] => 20110911
    [1] => 20110913
    [2] => Array
        (
            [0] => 20110915
            [1] => 20110918
        )

    [3] => 20110920
    [4] => 20110922
    [5] => 20110924
    [6] => Array
        (
            [0] => 20110926
            [1] => 20110927
        )

    [7] => 20110929
)
Sign up to request clarification or add additional context in comments.

6 Comments

will it convert like the same array as mentioned above? I do but its doing nothing... :(
@Vaib take a look at my demo. Be sure you have PHP with PECL json >= 1.2.0
@VaibhavGupta: it is. try again
@Vaibhav Gupta: The link works, it's something in your system that doesn't!
@VaibhavGupta: Do you think you could share your IP here?
|
2

You can pass your string in {...} and pass it to json_decode(str, true) to get back an array.

>> json_decode("[20110911, 20110913, [20110915, 20110918], 20110920, 20110922, 2
0110924, [20110926, 20110927], 20110929]")
array (
  0 => 20110911,
  1 => 20110913,
  2 =>
  array (
    0 => 20110915,
    1 => 20110918,
  ),
  3 => 20110920,
  4 => 20110922,
  5 => 20110924,
  6 =>
  array (
    0 => 20110926,
    1 => 20110927,
  ),
  7 => 20110929,
)

1 Comment

it is exactly the answer I posted 13 minutes ago

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.