implode numeric nested array of associative array - php

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] => nagesh#ccomsys.net
)
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 | nagesh#ccomsys.net
_______|__________________________
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.

Just a few minor adjustments:
I added ucfirst() to the first column to match your desired output.
I used an inline conditional to check if array values were arrays, to appropriately imploded array before echoing.
Code: (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>';
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>nagesh#ccomsys.net</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>nagesh#ccomsys.net</td></tr></table>

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;
}
}

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] => nagesh#ccomsys.net
)
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;
}
}

Replace the following code:
if($key == "age"){
$age = implode(",", $age_array);
}
with
if($key == "age"){
$age = implode(",", $value);
}

You need to code as follows...
foreach($post as $value){
$gender = $value['gender'];
$age = implode(',',$value['age']);
$name = $value['name'];
$email = $value['email'];
}

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;
}
}

$output = [];
//$array - your input data
foreach ($array as $key => $item) {
$output[$key] = (is_array($item)) ? implode(', ', $item) : $item;
}
//print output

Related

Php foreach in_array keeps returning false

I built a foreach which loops through a specific folder and gets an array:
foreach ($watchFolder as $key => $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
if((in_array('xml', $watchFolder))) {
echo "true";
} else {
echo "false";
}
foreach ($value as $key => $valueSub) {
$currentWatchPath2 = $currentWatchPath . '\\' . $key;
foreach ($valueSub as $content) {
$currentWatchPath3 = $currentWatchPath2 . '\\' . $content;
}
}
}
If I print out the $watchfolder I get this:
Array (
[test] => Array([videos] => Array()
[xml] => Array())
[test2] => Array([json] => Array([0] => test.json)
[videos] => Array())
)
If I try this:
dd(in_array('xml', $value));
it returns null, whereas it should return true or?
I am really confused at the moment, so I appreciate every tips and suggestion.
xml is a key of $value array so I think you should check like this:
foreach ($watchFolder as $key => $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
if(isset($value['xml'])) { // <--- here is the changed.
echo "true";
} else {
echo "false";
}
foreach ($value as $key => $valueSub) {
$currentWatchPath2 = $currentWatchPath . '\\' . $key;
foreach ($valueSub as $content) {
$currentWatchPath3 = $currentWatchPath2 . '\\' . $content;
}
}
}
In replace of in_array('xml', $watchFolder), it would be used in_array('xml', $value)
foreach ($watchFolder as $key => $value) {
echo '<pre>';
print_r($value);
echo '</pre>';
if((in_array('xml', $value))) {
echo "true";
} else {
echo "false";
}
foreach ($value as $key => $valueSub) {
$currentWatchPath2 = $currentWatchPath . '\\' . $key;
foreach ($valueSub as $content) {
$currentWatchPath3 = $currentWatchPath2 . '\\' . $content;
}
}
}

echo when certain value out of array equals your function

I have an array that has this:
$autos[] = array(
"auto" => "Audi A6",
"Max snelheid" => "240 km/h",
"Verbruik" => "1 op 12");
$autos[] = array(
"auto" => "Porsche 911",
"Max snelheid" => "290 km/h",
"Verbruik" => "1 op 8");
and beneath I use a foreach that echo's this.
foreach ($autos as $cars => $arr){
echo '<tr>';
foreach($arr as $test => $data){
echo '<td>'.$data.'</td>';
}
echo '</tr>';
}
How can I echo something when this value is "Verbruik" => "1 op 11"
If the 2nd number is higher then 11 I have to echo "That's Correct".
So example would be:
$autos[] = array(
"auto" => "Fiat Panda",
"Max snelheid" => "140 km/h",
"Verbruik" => "1 op 11");
So when I would have that code above. How could I echo "That's correct" ?
Use explode() function to explode your string "1 op 12" into array with 2 numbers, then check if the second number is bigger than 11 or whatever you want.
foreach ($autos as $cars => $arr){
echo '<tr>';
foreach($arr as $test => $data){
echo '<td>'.$data." ";
if($test == "Verbruik"){
$temp = explode(" op ",$data);
if($temp[1] > 11 ){
echo "correct "; // do whatever you want
}
}
echo '</td>';
}
echo '</tr>';
}
Try this:
foreach ($autos as $cars => $arr){
echo '<tr>';
foreach($arr as $test => $data){
$numbers = explode(" op ", $data);
if($test == 'Verbruik'){
if((int) $numbers[1] >= 11)
echo '<td>'.$data." , That's correct!</td>";
}
else
echo '<td>'.$data.'</td>';
}
echo '</tr>';
}

Array inside assosiative array

I want to create an assosiative array which it will have as one of its elements an other array (not assosiative)
$temp= array(
'elem1' => $data1,
'elem2' => $data2,
'elem3' => $data5,
);
$data2 is a simple array already constructed. However, I cannot find the right syntax for doing that. By applying the classic following I cannot get the values of elem2.
while ($element=each($temp))
{
echo $element['key'];
echo " - ";
echo $element['value'];
echo "<br/>";
}
It's not quite clear from your question, but depending on the structure of $data2, printing its values separated by commas may suffice:
foreach ($temp as $key => $value) {
echo $key, ' - ';
if (is_array($value)) {
echo implode(',', $value);
}
else {
echo $value;
}
echo '<br/>';
}
try this
$data = ['elem1' => '1', 'elem2' => ['red','blue','orange' ],'elem3' => '3'];
$tmp ='';
foreach ($data as $key => $value ) {
$tmp .= $key.' - ' ;
if (is_array($value)) {
$tmp.= implode(',', $value).' ';
}else {
$tmp .= $value. ' ';
}
}
echo json_encode($tmp);
You can echo the values in this way:
foreach($temp as $key => $value) {
echo $key;
echo " - ";
if(is_array($value)) {
echo "Array: # ";
foreach($value as $value_key => $value_value) {
echo $value_key." - ".$value_value." # ";
}
} else {
echo $value;
}
echo "<br/>";
}

Count duplicate values in PHP array

my problem is, i have this script
foreach ($arr1 as $k => $val) {
if (in_array($val, $arr2)) {
echo 'Obsazeno <br>';
} else {
echo $val . "<BR>";
}
}
Where $arr1 is generated array of days and $arr2 is array from mysql results. Actually it works fine. If one of days from $arr1 is in database, it will echo "Obsazeno" instead of day value.
But i need small upgrade. Script check if $val is in $arr2 and i need small counting contidion that will do this:
If $val is in $arr2 once - it will echo $val.
If $val is in $arr2 twice - it will echo $val.
But if $val is in $arr2 thrice - it will echo "Obsazeno".
I hope you understand my question.
I quest, it is possible by array_count_values. I try it by myself but no success.
You can achieve this with array_count_values.
$values = array_count_values($arr2);
foreach ($arr1 as $k => $val) {
if (array_key_exists($val, $values) && $values[$val] >= 3) {
echo 'Obsazeno <br>';
} else {
echo $val . '<br>';
}
}
try
$i=0;
foreach($arr1 as $k=>$val) {
if(in_array($val,$arr2)) {
$i++;
if($i == 3) {
echo 'Obsazeno <br>';
}
else {
echo $val."<BR>";
}
}
}
$data = array();
foreach($arr1 as $k=>$val)
{
if(!array_key_exists($val,$data)){
$data[$val] = 0;
}
if(in_array($val,$arr2))
{
$data[$val]++;
if($data[$val]==3){
echo 'Obsazeno <br>';
}else{
echo $val."<BR>";
}
}
else
{
echo $val."<BR>";
}
}

Looping through an array of arrays, changing output on a given line(s)

This is what im using to loop through an array of arrays.
$csvpre = explode("###", $data);
$i = 0;
$bgc = 0;
foreach ( $csvpre AS $key => $value){
$info = explode("%%", $value);
$i++;
if($i == "1"){
echo "<tr bgcolor=#efefef><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
} else {
if($bgc=1) { $bgcgo = "bgcolor=\"#b9b9b9\"" ;} else { $bgcgo = "bgcolor=\"#d6d6d6\""; }
echo "<tr $bgcgo><td></td>";
foreach ( $info as $key => $value ){ echo "<td>$value</td>"; }
echo "</tr>";
$bgc++;
}
}
How can i add an if/elseif statement to the last foreach, so that the output changes on a given line of the array.
Say i want <td>$value</td> for all unless specified, but on line 30, i want <textarea>$value</textarea>
You mean like this:
<?php
.......
echo "<tr $bgcgo><td></td>";
$j = 0; //you need a counter
foreach ( $info as $key => $value ) {
$j++;
if ($j != 30) {
echo "<td>$value</td>";
} else {
echo "<textarea>$value</textarea>";
}
}
echo "</tr>";

Categories