Reputation: 578
This might be duplicate of several questions. I am having an dynamic array
Array
(
[gender] => Male
[age] => Array
(
[0] => 18-25 years
[1] => 26-32 years
)
[name] => Nagesh
[emailid] => [email protected]
)
I want to store this array in HTML table as key in one column and value in another Example.
__________________________________
Gender | Male
_______|__________________________
Age | 18-25 years, 26-32 years
_______|__________________________
Name | Nagesh
_______|__________________________
Email | [email protected]
_______|__________________________
I am getting key and value for associative array but how can I implode age array.
I tried below code
$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;
}
}
Thanks advance.
Upvotes: 2
Views: 157
Reputation: 47902
Just a few minor adjustments:
ucfirst()
to the first column to match your desired output.Code: (Demo)
$post=[
'gender'=>'Male',
'age'=>['18-25 years','26-32 years'],
'name'=>'Nagesh',
'emailid'=>'[email protected]'
];
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>';
Unrendered Output:
<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>[email protected]</td>
</tr>
</table>
Rendered output available via runnable snippet:
<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>[email protected]</td></tr></table>
Upvotes: 1
Reputation: 571
$output = [];
//$array - your input data
foreach ($array as $key => $item) {
$output[$key] = (is_array($item)) ? implode(', ', $item) : $item;
}
//print output
Upvotes: 0
Reputation: 470
Change Your foreach loop with this one.
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;
}
}
Upvotes: 0
Reputation: 1692
It is working like what you expecting:
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;
}
}
If you get multiple arrays means it will work. I changed small things in your condition:
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;
}
}
Upvotes: 1
Reputation: 2327
You need to code as follows...
foreach($post as $value){
$gender = $value['gender'];
$age = implode(',',$value['age']);
$name = $value['name'];
$email = $value['email'];
}
Upvotes: 0
Reputation: 885
Replace the following code:
if($key == "age"){
$age = implode(",", $age_array);
}
with
if($key == "age"){
$age = implode(",", $value);
}
Upvotes: 0
Reputation: 40668
You have an array of data and you want to display it on a table. Instead of using implode(), why not just concatenate the two strings together?
Array
(
[gender] => Male
[age] => Array
(
[0] => 18-25 years
[1] => 26-32 years
)
[name] => Nagesh
[emailid] => [email protected]
)
PHP code:
$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;
}
}
Upvotes: 0