这可能是几个问题的重复。 我有一个动态数组
Array
(
[gender] => Male
[age] => Array
(
[0] => 18-25 years
[1] => 26-32 years
)
[name] => Nagesh
[emailid] => nagesh@ccomsys.net
)
我希望将此数组存储在HTML表中作为一列中的键,将值存储在另一列中 实施例。
__________________________________
Gender | Male
_______|__________________________
Age | 18-25 years, 26-32 years
_______|__________________________
Name | Nagesh
_______|__________________________
Email | nagesh@ccomsys.net
_______|__________________________
我得到了关联数组的关键和值,但是我怎么能破坏年龄数组。
我试过下面的代码
$post = $this->request->post;
$age_array = array_column($post, 'age');
$age = '';
foreach($post as $key => $value) {
if($key == "age"){
$age = implode(",", $age_array);
}
echo $age. "<br>";
echo $key. "<br>";
if($key != "age"){
echo $value. "<br>";
} else {
echo $age;
}
}
谢谢你。
答案 0 :(得分:1)
它的工作方式与您的期望相符:
foreach($post as $key => $value) {
echo $key. "<br>";
if($key == "age"){
$age = implode(",", $post[$key]);
echo $age. "<br>";
}
if($key != "age"){
echo $value. "<br>";
} else {
//echo $age;
}
}
如果你得到多个阵列意味着它会起作用。我改变了你的小事情:
foreach($post as $key => $value) {
echo $key. "<br>";
if(is_array($post[$key])){
$age = implode(",", $post[$key]);
echo $age. "<br>";
}
if(!is_array($post[$key])){
echo $value. "<br>";
} else {
//echo $age;
}
}
答案 1 :(得分:1)
只需进行一些小调整:
ucfirst()
添加到第一列以匹配您想要的输出。代码:(Demo)
$post=[
'gender'=>'Male',
'age'=>['18-25 years','26-32 years'],
'name'=>'Nagesh',
'emailid'=>'nagesh@ccomsys.net'
];
echo '<table border=1>';
foreach($post as $k=>$v){
echo '<tr>';
echo '<td>',ucfirst($k),'</td><td>',(is_array($v)?implode(', ',$v):$v),'</td>';
echo '</tr>';
}
echo '</table>';
未渲染的输出:
<table border=1>
<tr>
<td>Gender</td><td>Male</td>
</tr>
<tr>
<td>Age</td><td>18-25 years, 26-32 years</td>
</tr>
<tr>
<td>Name</td><td>Nagesh</td>
</tr>
<tr>
<td>Emailid</td><td>nagesh@ccomsys.net</td>
</tr>
</table>
通过runnable片段提供的渲染输出:
<table border=1><tr><td>Gender</td><td>Male</td></tr><tr><td>Age</td><td>18-25 years, 26-32 years</td></tr><tr><td>Name</td><td>Nagesh</td></tr><tr><td>Emailid</td><td>nagesh@ccomsys.net</td></tr></table>
答案 2 :(得分:0)
替换以下代码:
if($key == "age"){
$age = implode(",", $value);
}
与
public abstract class SomeBaseClass {}
public class SomeSpecificClass: SomeBaseClass {
public int propertyA;
public string propertyB;
}
public delegate void Callback<T>(T data);
public class Foo {
void MethodA <T> (Callback<T> theInstance) where T: SomeBaseClass {
MethodB(theInstance);
}
void MethodB(Callback<SomeBaseClass> theInstance) {
}
void MethodC() {
Callback<SomeSpecificClass> cb = (data) => {};
MethodA(cb);
}
void MethodD <T> (T theInstance) where T: SomeBaseClass {
MethodE(theInstance);
}
void MethodE (SomeBaseClass theInstance) {
}
}
答案 3 :(得分:0)
您有一个数据数组,并且您希望将其显示在表格上。而不是使用implode(),为什么不将两个字符串连接在一起?
Array
(
[gender] => Male
[age] => Array
(
[0] => 18-25 years
[1] => 26-32 years
)
[name] => Nagesh
[emailid] => nagesh@ccomsys.net
)
PHP代码:
$post = $this->request->post;
foreach($post as $key => $value) {
if($key == "age"){
$age = $value[0].','.$value[1];
}
echo $age. "<br>";
echo $key. "<br>";
if($key != "age"){
echo $value. "<br>";
} else {
echo $age;
}
}
答案 4 :(得分:0)
您需要编码如下......
foreach($post as $value){
$gender = $value['gender'];
$age = implode(',',$value['age']);
$name = $value['name'];
$email = $value['email'];
}
答案 5 :(得分:0)
用这个改变你的foreach循环。
foreach($test as $key => $value) {
$age = '';
if($key == "age"){
$age = implode(",", $value);
}
echo $age. "<br>";
echo $key. "<br>";
if($key != "age"){
echo $value. "<br>";
} else {
echo $age;
}
}
答案 6 :(得分:0)
(>>=) :: m a -> (a -> m b) -> m b
printabledate >>= print :: IO ()