echo when certain value out of array equals your function - php

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

Related

implode numeric nested array of associative array

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

Eliminating duplicating value in 2D array in php

I can't attach images so please find array at link.
I eliminated the duplicated value for this array. I used technique that is bit awful. Array name is $pap.
Here is code
`foreach ($pap as $key => $row)
{
foreach ($row as $subkey => $subvalue)
{ $p[$n]=$pap[$key][$subkey];
$n++;
}
}
$unique = array_unique($p);
$n=0; $abc = array();
foreach ($unique as $key => $row){
$abc[$n]=$unique[$key]; $n++;
}
$m=0;
echo "<table align='center' border='2'>";
foreach ($pap as $key => $row)
{
echo "<tr>";
foreach ($row as $subkey => $subvalue)
{
if($m<$n){
if($pap[$key][$subkey]==$abc[$m])
{
echo "<td>";
echo $pap[$key][$subkey];
echo "</td>";
$m++;
}
}
}
echo "</tr>";
}
echo"</table>";`
Any better technique would be appreciated.
Check this, I think you this will do the job for you.
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = super_unique($value);
}
}
return $result;
}
var_dump(super_unique($details));
?>

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

i have an multidimensional array given below

i have an array like this
$x = array("1" =>array("PHP","code"), "foo" =>array("bar", 5 ,11));
for($i=1;$i<count($x);$i++)
{
foreach($x[$i] as $key=>$value){
echo $key.$value;
}
echo "<br>";
}
and i want output like this:
1.php
1.code
foo.bar
foo.5
foo.11
how could i get this out put.
You are almost there:
foreach ($x AS $key => $val) {
foreach ($val AS $sub) {
echo $key . '.' . $sub . '<br>';
}
}
Loop through each array, like so;
<?php
$x = array("1" =>array("PHP","code"),
"foo" =>array("bar", 5 ,11)
);
foreach($x as $key => $a) {
foreach($a as $c) {
echo $key . "." . $c . PHP_EOL;
}
}
https://eval.in/198869
<?php
$x = array("1" =>array("PHP","code"), "foo" =>array("bar", 5 ,11));
foreach($x as $m=>$n){
foreach($n as $y=>$x){
$arrayList[]= $m.".".strtolower($x);
}
}
echo "<pre>";
print_r($arrayList);
echo "</pre>";
?>
You have to put the line break <br> inside the inner loop, like this:
$x = array( "1" => array("PHP","code"), "foo" => array("bar", 5 ,11));
foreach($x as $key=>$val){
foreach($val as $value){
echo $key.".".$value."<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