1

I would like to convert an Array like this:

 array ( [1_1] => 1 [1_2] => 2 [1_3] => 3 [1_4] => 4 [1_5] => 5 )

to an string like this:

"1_1-1/1_2-2/1_3-3/1_4-4/1_5-5"

how can I do it?

I need the Index and the values in my MySQL-Databse.

I tryed implode() but this is the result:

1/2/3/4/5

thank you

1
  • You can loop and build your own string... Commented Jul 4, 2013 at 22:37

3 Answers 3

1
$out = "";
foreach($arr as $k => $v) {
    $out .= "$k-$v/";
}
$out = substr($out, 0, -1); //this line will remove the extra '/'
Sign up to request clarification or add additional context in comments.

Comments

1

PHP < 5.5

It's better to store all these values in an associative array and then implode them

$final = array();
foreach($array as $key => $val)
{
    $final[] = $key.'-'.$val;
}
$final = implode('/', $final);

PHP = 5.5

You may want to use generators to yeld these values assuming they are regular: See: http://php.net/manual/en/language.generators.overview.php

Comments

1

You can use http_build_query() to do that.

http://www.php.net/http_build_query

Try:

     echo http_build_query($array, '', '/');

Comments

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.