0

Hi i am using CakePHP version 2.x

I am print my array using

pr($this->request->params['named']);

//Output
Array
(
    [street_name] => Bhubaneswar
    [house_number] => 1247
    [phone] => xxxxxxxx
    [zip] => xxxxx
)

In above array i want to view looks like

/street_name:Bhubaneswar/house_number:1247/phone:xxxxxxxx/zip:xxxxx

How to convert above array in string format?

3
  • Hi @Rikesh i tried echo $string = implode("/",$this->request->params['named']); but i am not getting the perfect result. Commented Jul 11, 2014 at 6:59
  • Thanks for giving minus(-) vote. I got my answer. thanks Miraage and Sal00m Commented Jul 11, 2014 at 7:04
  • You probably got the - for not showing any research effort, and not making it clear what you want to do with that string, and why you can't use CakePHP tools like Router::url(). So maybe try to write better questions instead of complaining. Commented Jul 11, 2014 at 8:37

7 Answers 7

3

Try this:

$strResult = "";
foreach ($this->request->params['named'] as $key=>$value){
    $strResult .= "/{$key}:{$value}";
}
Sign up to request clarification or add additional context in comments.

Comments

2

The most easy and speedy solution.

$result = '';
foreach ($this->request->params['name'] as $k => $v) {
    $result .= sprintf('/%s:%s', $k, $v);
}

Comments

2

As a one-liner:

echo '/' . str_replace('=', ':', http_build_query($array, '', '/'));

Would output:

/street_name:Bhubaneswar/house_number:1247/phone:xxx/zip:xxx 

Comments

1
foreach (array_expression as $key => $value){
    echo "/". $key . ":" . $value;
}

1 Comment

+ is not tthe operator to concat strings in PHP, replace with .
0

Try following code:

$ar = array('street_name' => 'Bhubaneswar',
    'house_number' => 1247,
    'phone' => 'xxxxxxxx',
    'zip' => 'xxxxx');
echo convert($ar);

function convert($ar)
{
    $str = '';
    foreach($ar as $i=>$k)
    {
        $str .= '/'.$i.':'.$k;
    }
    return $str;
}

Output

/street_name:Bhubaneswar/house_number:1247/phone:xxxxxxxx/zip:xxxxx

Comments

0
function implode_with_keys($glue, $separator, $pieces)
{
    $string = '';
    foreach ($pieces as $key => $value) {
        $string .= $glue . $key . $separator . $value;
    }
    return $string;
}

In your case you'd use it like this:

implode_with_keys('/', ':', $this->request->params['named']);

This will return the desired string /street_name:Bhubaneswar/house_number:1247/phone:xxxxxxxx/zip:xxxxx.

Comments

0
<?php
$array = array(
    'street_name' => 'Bhubaneswar',
    'house_number' => '1247',
    'phone' => 'xxxxxxxx',
    'zip' => 'xxxxx',
);

while (current($array)) {

        echo '/'.key($array).':'.$array[key($array)];

    next($array);
}
?>

this will return

/street_name:Bhubaneswar/house_number:1247/phone:xxxxxxxx/zip:xxxxx

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.