Zend Exam Tips III

I finally found a very smart question on this mock exams. Check this out:

What is the best approach for converting this string:

$string = "a=10&b[]=20&c=30&d=40+50";

Into this array?

array(4) {
  ["a"]=>
  string(2) "10"
  ["b"]=>
  array(1) {
    [0]=>
    string(2) "20"
  }
  ["c"]=>
  string(2) "30"
  ["d"]=>
  string(5) "40 50"
}

Answer…

A

Write a parser completely by hand, it’s the only way to make sure it’s 100% accurate

B

Use the parse_str() function to translate it to an array()

C

Pass the variable to another PHP script via an HTTP GET request and return the array as a serialized variable

D

Just call unserialize() to translate it to an array()

E

Write a string parser using strtok() and unserialize() to convert it to an array

My first thought  was to use unserialize and there you go, everything should be right, but as you know, not always I’m right. D was my answer at the exam and after I runned some tests I have found out that C is the correct answer.

Take a look again at the string a=10&b[]=20&c=30&d=40+50.

If you put a index.php? before the string you will get a full correct $_GET request: index.php?a=10&b[]=20&c=30&d=40+50.

Why the heck din’t I think on that!??????

Advertisements

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

8 responses to “Zend Exam Tips III

  • Zend Exam Tips - be careful with serialize « Mcloide’s resources library

    […] I did I made the same mistake twice: the “serialize” mistake. If you have read my post Zend Exam Tips III you will see that I believed that unserialize would take care of a string that seemed to be […]

  • cy

    Come on the answers B, C also does the job but its just a stupid way.

  • mcloide

    Not exactly. Take look. Parse_str (http://us2.php.net/parse_str) it will create a variable for each entry and the question asks you to output an array from that string, so if you use parse_str you will have a variable for $a = 10; $b = array[0] = 20; $c = 30; and $d = 40 50; leaving us with the only available choice of using a $_GET request. This is why this question is so frustrating.

  • Daniel Lopez

    B is definitely correct… just take a look at parse_str @ php.net. The output you described only occurs when you don’t provide the second parameter. If you provide a variable in the second parameter. The variable is filled with the desired output per the question.

    Example (tested):

    OUTPUT:
    array(4) {
    [“a”]=>
    string(2) “10”
    [“b”]=>
    array(1) {
    [0]=>
    string(2) “20”
    }
    [“c”]=>
    string(2) “30”
    [“d”]=>
    string(5) “40 50”
    }

  • Daniel Lopez

    When using:

    $string = “a=10&b[]=20&c=30&d=40+50”;

    parse_str($string,$array);

    var_dump($array);

  • mcloide

    Daniel,

    I have to admit that I was wrong and you are right. I have not read all the information about the parse_str and bypassed completely the second parameter for the function which will return an array from that string.

    Thanks for the help

  • Mat Wright

    The only correct answer is B.

    C would return something lik:

    a:4:{s:1:”a”;s:2:”10″;s:1:”b”;a:1:{i:0;s:2:”20″;}s:1:”c”;s:2:”30″;s:1:”d”;s:5:”40 50″;}

    which is not the same as:

    array(4) {
    [“a”]=>
    string(2) “10”
    [“b”]=>
    array(1) {
    [0]=>
    string(2) “20”
    }
    [“c”]=>
    string(2) “30”
    [“d”]=>
    string(5) “40 50”
    }

    • mcloide

      Hey Mat,

      After reviewing the functionality for answer B, I have to agree with you that B seems to be the correct answer, but parse_url is the function that you are looking for, therefore, even knowing that they have more options, C still being the correct answer (Disclaimer: This question was copied, with some little modification on the values, from the mock exam, so was the answers.)

      Anyway, the best way to get if the answer is correct or not is by running the code with every option. It might not seem, but, it really helps on the exam.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: