Display array values in PHP
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $key." has the value". $value;
}
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;
Print only the array values - PHP
print_r
is a function intended for debugging, not formatted output.
The simplest way to achieve what you want is using implode
, which combines the values of an array into a string; and then echo
to display the string.
<div class="courses-search-result standout-box-shadow <?php echo implode(' ', $classes); ?>">
How can I echo or print an array in PHP?
This will do
foreach($results['data'] as $result) {
echo $result['type'], '<br>';
}
how to display array values in php?
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$count=count($_POST['friend']);
for($i=0; $i<$count; $i++)
{
$a=$_POST['friend'][$i];
//echo $a;
$sql = "SELECT Name,EmailAddress,Qualification FROM form WHERE sno='$a'";
$result=$conn->query($sql);
while($row = $result->fetch_assoc()){
$ab=$row['Name'];
$bc=$row['EmailAddress'];
$ca=$row['Qualification'];
echo $ab;
echo'<br/>';
echo $bc;
echo'<br/>';
echo $ca;
echo'<br/>';
$sql1="INSERT INTO arun ". "VALUES('$ab')";
$result1=$conn->query($sql1);
}
}
if (!$result) {
//$_SESSION['message10'] = '<p style="color:green;margin-left: 250px";>Your request has been send </p>';
//header("Location:send.php");
echo "not send";
}
else {
// $_SESSION['message11'] = '<p style="color:red;margin-left: 250px";>you have already send request to the user</p>';
//header("Location:new.php");
echo "send";
}
$conn->close();
?>
How to count and display array values in php
I don't know how you're using array_count_values
because you haven't posted your code, please post your code in future. Below is a working example that gives your desired output.
<?php
$rooms = "Single room,Single room,Single room,Double room";
$rooms = explode(',', $rooms); // array of rooms
//Count each occurence of values
$countedValues = array_count_values($rooms);
//Build strings from value and display them.
foreach( $countedValues as $roomType=>$count ){
echo $count . 'x ' . $roomType . '<br />';
}
?>
This gave me the output:
3x Single room
1x Double room
Please note that your question includes this:
$rooms= Single room,Single room,Single room ;
this is not a valid array format. You should use this instead:
$rooms = array("Single room","Single room","Single room");
How to display multiple array value in PHP?
You have multiple options to output an array.
Echo and foreach
You c an loop through your array and echo each $key
and $value
foreach ($keys as $key => $value) {
echo $key." : ".$value."<br />";
}
print_r and var_dump
This is mostly used in debugging.
print_r($keys);
var_dump($keys);
Imploding
You can implode your array to echo the concatted values.
// The first parameter is the devider or separator
echo implode('', $keys);
Is it possible to display more than one value from the column?
Yes, ofcourse. Look at the following example:
foreach ($keys as $value) {
echo $value['columnone'];
echo $value['columntwo'];
echo $value['columnthree'];
}
Resources
- implode() - PHP Manual
- print_r() - PHP Manual
- var_dump() - PHP Manual
- foreach - PHP Manual
How to print array element with keys & values in PHP?
In the nested foreach
you have to iterate over the $value
which holds the array.
foreach ( $marks as $key => $value) {
foreach ( $value as $key2 => $value2 ) {
// -------^^^^^^-------
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
}
};
PHP Print array values in order which is in another array
This did the trick:
print "<table style=\"width:100%\">";
print " <tr>\n";
foreach ($fields_order_obj as $obj){
print "<th>".$obj->name."</th>\n";
}
print " </tr>\n";
foreach ($data as $row) {
print " <tr>\n";
foreach ($fields_order_obj as $obj){
$fieldName = $obj->name;
print "<td>".$row[$fieldName]."</td>\n";
}
}
print " </tr>\n";
Related Topics
How to Add Private Github Repository as Composer Dependency
How to Get the Request Parameters in Symfony 2
Composer Require Local Package
Calculate Total Seconds in PHP Dateinterval
How to Echo the Whole Content of a .HTML File in PHP
Laravel No Supported Encrypter Found. the Cipher And/Or Key Length Are Invalid
Symfony2 Routing - Route Subdomains
How to Implode Array with Key and Value Without Foreach in PHP
Is "Set Character Set Utf8" Necessary
Mysql_Escape_String VS MySQL_Real_Escape_String
Simple PHP Form: Attachment to Email (Code Golf)
Pdo Try-Catch Usage in Functions
How So I Convert This Sha256 + Base64 from Swift to PHP
Looping Through Query Results Multiple Times with MySQLi_Fetch_Array