Hi i am using CakePHP version 2.x
嗨,我正在使用CakePHP版本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?
如何以字符串格式转换上面的数组?
7 个解决方案
#1
3
Try this:
尝试这个:
$strResult = "";
foreach ($this->request->params['named'] as $key=>$value){
$strResult .= "/{$key}:{$value}";
}
#2
2
The most easy and speedy solution.
最简单快捷的解决方案。
$result = '';
foreach ($this->request->params['name'] as $k => $v) {
$result .= sprintf('/%s:%s', $k, $v);
}
#3
2
As a one-liner:
作为单线:
echo '/' . str_replace('=', ':', http_build_query($array, '', '/'));
Would output:
输出:
/street_name:Bhubaneswar/house_number:1247/phone:xxx/zip:xxx
#4
1
foreach (array_expression as $key => $value){
echo "/". $key . ":" . $value;
}
#5
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
#6
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
.
这将返回所需的字符串/ street_name:Bhubaneswar / house_number:1247 / phone:xxxxxxxx / zip:xxxxx。
#7
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
#1
3
Try this:
尝试这个:
$strResult = "";
foreach ($this->request->params['named'] as $key=>$value){
$strResult .= "/{$key}:{$value}";
}
#2
2
The most easy and speedy solution.
最简单快捷的解决方案。
$result = '';
foreach ($this->request->params['name'] as $k => $v) {
$result .= sprintf('/%s:%s', $k, $v);
}
#3
2
As a one-liner:
作为单线:
echo '/' . str_replace('=', ':', http_build_query($array, '', '/'));
Would output:
输出:
/street_name:Bhubaneswar/house_number:1247/phone:xxx/zip:xxx
#4
1
foreach (array_expression as $key => $value){
echo "/". $key . ":" . $value;
}
#5
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
#6
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
.
这将返回所需的字符串/ street_name:Bhubaneswar / house_number:1247 / phone:xxxxxxxx / zip:xxxxx。
#7
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