0

for example:

$value_array = array("fu" => "bar", "banana" => "apple");

for example:

echo $value_array["fu"]; # output will be bar

okay, i have this value:

$value = "fuu:bar:12:apple";

okay, i'd like to parse the $value and write "bar" value to the screen, but i don't know how i can do this job.

5
  • Where does "fuu:bar:12:apple" come from? Commented Sep 21, 2011 at 17:35
  • @FelixKling, it comes from mysql table Commented Sep 21, 2011 at 17:35
  • Your $value reminds me serialized string. Does it actual $value value? Commented Sep 21, 2011 at 17:36
  • OK, what I actually want to know is how do you create this value in the first place? Instead of creating your own format, you could e.g. use serialize and unserialize. Commented Sep 21, 2011 at 17:36
  • @FelixKling, mate, thanks for your help. i solved my problem. btw, thanks for your kindly suggestion. my friend, i cam creating mysql table with my hand. value comes from mysql table. anyway, regards mate. Commented Sep 21, 2011 at 17:48

3 Answers 3

3

umm, why delimit by :? It'd be a lot easier with fuu:bar;12:apple but to go with what you have...

$value = explode(':',$value);
$values = array();
foreach ($value as $k => $v) {
   if ($k %2 != 0)
      $values[$value[($k - 1)]] = $v;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Dammit! Exactly the solution I had. Good one.
Don't you love when that happens?
No, but yes because now you get free rep :-)
haha, I like seeing my function submitted seconds before I submit it.
The thing that gets me is all the answers that are just pastes of code get all the upvotes, whereas my nicely written and explained answer just gets told to go and stand in the corner. Sort of an okay moment.
1

Try using explode function.

Comments

1
$bar_val  = explode(":", $value);

echo $bar_val[1];

Demo: http://codepad.org/pyoyfk8n

3 Comments

How does your answer satisfy the echo $value_array["fu"]; example?
@mario -- the OP did not ask for that, that was just an example. The OP asked how he/she would output bar from the given string
Question title says parsing. Nowhere does it assert that it's actually an ordered list.

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.