If I have an array:
$array = array ( [rock] => 40, [pop] => 30, [rap] => 20 ) etc...
how can I do something like:
foreach key in $array
{
if (array_value > 30) echo "> 30:" . $array_key . "<br>";
if (array_value < 30) echo "< 30:" . $array_key . "<br>";
}
So that the result would be:
> 30:rock<br>
< 30:pop<br>
< 30:rap<br>
Thanks! I hope this makes sense...
foreach ($array as $key => $value) {
if ($value ...) echo $key...
else if ($value ...) echo $key...
...
}
deceze's answer is correct in general, but more specifically, the following code should work:
foreach ($array as $key => $value) {
if ($value > 30) {
echo '> 30:' . $key . '<br>';
} elseif ($value <= 30) { // Changed this to <= to cover the case of $value = 30
echo '< 30:' . $key . '<br>';
}
}
Related
I want to echo something only on the first time foreach() runs!
let's say if I have an array $i
$i = array(............);
and I want to use it in foreach like...
foreach($i as $key => $value) {
echo '<h1>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
while this foreach runs the first time only i want to add class="show" to the h1 tag.
How to do that?
Update code:
$i=['hello','world'];
foreach($i as $key => $value) {
$class= ($key == 0)?'show':'';
echo '<h1 class="'.$class.'">'.$key.'</h1>';
echo '<p>'.$value.'</p>';
}
Output:
<div>
<h1 class="show">0</h1><p>hello</p>
<h1 class="">1</h1><p>world</p>
</div>
You'll need to add a var to track that
$first = 0;
foreach($i as $key => $value) {
$show=($first == 0) ? 'h1 class=show' : 'h1';
echo '<$show>'.$key.'</h1>';
echo '<p>'.$value.'</p>';
$first++;
}
There are many ways to do it, but this might be easier to read
if you have single dimension array then you can use
$i=['test1','test2','test3'];
foreach($i as $key => $value) {
if($key == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
}
if you have an associative array
$i=array('a'=>'test1','b'=>'test2','c'=>'test3');
$j = 0;
foreach($i as $key => $value) {
if($j == 0){
echo $key;
echo '<br>';
echo '<p>'.$value.'</p>';
}
$j++;
}
output will be
I have a nested array on this link Array Sample
I am using code below to parse this, but second and beyond depth it's returning nothing. However tried with recursive function.
printAllValues($ArrXML);
function printAllValues($arr) {
$keys = array_keys($arr);
for($i = 0; $i < count($arr); $i++) {
echo $keys[$i] . "$i.{<br>";
foreach($arr[$keys[$i]] as $key => $value) {
if(is_array($value))
{
printAllValues($value);
}
else
{
echo $key . " : " . $value . "<br>";
}
}
echo "}<br>";
}
}
What I am doing Wrong? Please help.
Version of J. Litvak's answer that works with SimpleXMLElement objects.
function show($array) {
foreach ($array as $key => $value) {
if (!empty($value->children())) {
show($value);
} else {
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
You can use recurcive function to print all values:
function show($array) {
foreach( $array as $key => $value) {
if (is_array($value)) {
show($value);
} else{
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
How can I generate header for printed 2d array in php?
So my array look like this:
$tab=array(
array(0,1,2,3),
array(1,2,3,4),
array(2,3,4,5)
)
This code:
foreach ($tab as $key => $row){
echo '<b>o<sub>'.($key+1).'</sub></b> ';
foreach ($row as $item) {
echo $item.' ';
}
echo '<br>';
}
Print this:
o1 0 1 2 3
o2 1 2 3 4
o3 2 3 4 5
But I need this:
a1 a2 a3 d
o1 0 1 2 3
o2 1 2 3 4
o3 2 3 4 5
Where lenght of rows may be diferent and last column always must be d
Thanks for help
You can check if this is the first iteration in the first foreach and if so, add the first line.
foreach ($tab as $key => $row) {
// If first iteration, add the header
if ($key === 0)
{
foreach ($row as $i => $item)
{
// Last header must be 'd'
if ($i === count($row) - 1)
echo '<b>d</b>';
else
echo '<b>a<sub>' . ($i + 1) . '</sub></b> ';
}
echo '<br />';
}
// Add the current row
echo '<b>o<sub>' . ($key + 1) . '</sub></b> ';
foreach ($row as $item) {
echo $item . ' ';
}
echo '<br />';
}
If the first row is a static row, then you can simply print it using echo function at first.
But if its a dynamic tab as well, then you can use the following code:
$count = count($tab[0]);
for($i = 1; $i <= $count; $i++)
{
if($i != $count) echo '<b>a<sub>' . ($i + 1) . '</sub></b> ';
else echo '<b>d</b> ';
}
foreach ($tab as $key => $row){
echo '<b>o<sub>'.($key+1).'</sub></b> ';
foreach ($row as $item) {
echo $item.' ';
}
echo '<br>';
}
You can try this, except use <table> or <div> to align your elements.
foreach ($tab[0] as $key => $item)
echo $key === 0 ? title(' ', ' ') : title('a', $key);
echo title('d', '') . '<br>';
function title(string $letter, string $index)
{
return "<b>{$letter}<sub>{$index}</sub></b>";
}
I have a multi-dimension array in php like this
$shop = array(
array("name","point","number"),
array('Ranjit', 1.25 , 15),
array('Pitabas', 0.75 , 25),
array('Khela', 1.15 , 7)
);
Now I have to show the output like this
name-> ranjit
Point-> 1.25
number->15
name->Pitabas
Point->0.75
number->25
name->Khela
Point->1.15
number->7
I am trying for loop, but I could get the result in nested forloop. Please help me to get the answer.
My solution:
$headings = array_shift($shop);
foreach ($shop as $item) {
foreach ($item as $key => $value) {
echo $headings[$key], '=>', $value;
}
}
Here's a simple loop: Observe that we skip the first element of the outer array, which is deemed to contain the headers:
for ($i = 1; $i != count($shop); ++$i)
{
print $shop[0][0] . ": ". $shop[$i][0] . "\n";
print $shop[0][1] . ": ". $shop[$i][1] . "\n";
print $shop[0][2] . ": ". $shop[$i][2] . "\n";
}
You know the first row will be the titles, so store them separately:
$titles = $shop[0];
That will give you
$titles = array('name', 'point', 'number');
Then loop through your array:
foreach ($shop as $index => $row) {
if ($index == 0)
continue;
foreach($row as $column => $item) {
echo $titles[$column] . " -> " . $item . "<br />";
}
}
This should give the desired output:
for($x = 1, $lim = sizeof($shop); $x < $lim; $x++)
{
echo $shop[0][0]."->".$shop[$x][0]."<br>";
echo $shop[0][1]."->".$shop[$x][1]."<br>";
echo $shop[0][2]."->".$shop[$x][2]."<br>";
}
I have
foreach ($a as $key => $value) {
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
which displays result one below other,
like
1234
5678
2010-05-20
5678
1590
2010-05-19
but i want it in a table like structure like
1234 5678 2010-05-20
5678 1590 2010-05-19
how can i do that?
You could keep track with for example modulus if you should begin a new row or not.
EDITED after reformatting of the question
I think what you need is simply something on the line of:
echo '<table><tr>';
$n = 0;
foreach ($a as $key => $value)
{
if ($n==3)
{
$n = 0;
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$n++;
}
echo '</tr></table>';
You can do like:
$counter = 0;
foreach ($a as $key => $value) {
$counter++;
if (($counter % 3) === 0)
{
echo "<DIV id='container'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
else
{
echo "<DIV id='container' style='float:left;'>
<DIV id='middle'>$value</DIV>
</DIV>";
}
}
Kind of what nico was saying:
foreach ($a as $key => $value)
{
echo '<div style="width:33%;float:left">'.$value.'</div>';
}
Or what don was saying:
$i = 0;
$open = false;
echo '<table>';
foreach( $a as $key => $value )
{
if( 0 == $i % 3 ) {
echo '<tr>';
$open = true;
}
echo "<td>$value</td>";
if( 2 == $i % 3 ) {
echo '</tr>';
$open = false;
}
$i++;
}
if( $open )
echo '</tr>';
echo '</table>';
Simply use like this,
foreach ($a as $key => $value) {
echo "<DIV id='container' style='display:inline'>
<DIV id='middle' style='display:inline'>$value</DIV>
</DIV>";
//echo "<br />$key:$value<br />\n";
}
add the break statement depending on your second line.
By default, div's don't float next to each other. You could give all the div's, except every third one float: left in the style. However it's okay to use table's if your displaying a table.
If you can derive from the value of $key wether to start a new row or not you can do something like:
echo '<table><tr>';
foreach ($a as $key => $value) {
if($key == 'somevalue'){
//start a new row if the $key allow
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
}
echo '</tr></table>';
If you can't derive from the value of $key wether to start a new row or not, use a counter, like so:
echo '<table><tr>';
$cnt = 0;
foreach ($a as $key => $value) {
if($cnt % 3 == 0){
//start a new row if this row contians 3 cells
echo '</tr><tr>';
}
echo '<td>'.$value.'</td>';
$cnt++;
}
echo '</tr></table>';