PHP Arrays, displaying fruits like 1.apple 2.banana, etc - php

I have some array:
array
0 => string 'Apple'
1 => string 'banana'
2 => string 'Nanas'
3 => string 'Grape'
What is the best way to display
1. Apple
2. banana
3. Nanas
4. Grape
Any Ideas?

If the output you are after is HTML, then an ordered list makes sense:
<ol>
<?php foreach($fruits as $fruit) { ?>
<li><?php echo $fruit; ?></li>
<?php } ?>
</ol>

How about this?
foreach($fruits as $index => $fruit) {
echo ($index+1).". ".$fruit."\n";
}

If HTML
<ol>
<?php
foreach ($fruits as $fruit) {
echo "<li>", $fruit;
}
?>
</ol>

Related

how would i target different css classes per certain items in a php array?

I have a PHP array such as
$lulz = array ( 'tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes')
I am outputting the array as follows via simple foreach
foreach ($lulz as $key) {
echo $key;
}
I would like to put different CSS classes per each output. so, for example, the output would be
$result = '<div class="one">$tom</div><div class="two">$hojn</div><div class="three">$john</div><div class="four">$kiwi</div><div class="five">$tron</div><div class="six">$jikes</div>';
the current path I am on is putting in an incrementer variable that is incremented ++ after each loop. that works - I get 1, 2, 3, etc - but I am not sure how to translate that to my CSS divs per each item; or if I am even on the right path.
i was hoping to output array such as:
foreach ($lulz as $key) {
echo $key[1];
echo $key[3];
}
where [1] and [3] are just the entry 1 and 3 in the array
but doesn't seem to be possible.
any help would be greatly appreciated.
You can make your array as a class for each output.
Try this
$lulz = array ( 'tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes');
foreach ($lulz as $key){
echo '<div class="'.$key.'">'.$key.'</div>';
}
If you can just use the incrementer in the class name and it doesn't have to be a word representation of the number, this will give you the desired output and your classes would be .el1, .el2, etc.
<?php
$result = '';
$lulz = array ( 'tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes');
foreach ($lulz as $key => $value) {
$result .= '<div class="el' . ($key + 1) . '">' . $value . '</div>';
}
echo $result;
?>
<?php
$lulz = array('tom', 'hojn', 'john', 'kiwi', 'tron', 'jikes');
$catz = array('one', 'two', 'three', 'five', 'eight', 'thirteen');
foreach($lulz as $k => $x) { echo '<div class="'.$catz[$k].'">'.$x.'</div>'; }
?>
or
<?php
$lulz = array(
array('tom', 'one'),
array('john', 'two'),
array('hojn', 'three'),
array('kiwi', 'five'),
array('tron', 'eight'),
array('jikes', 'thirteen')
);
foreach($lulz as $k => $x) { echo '<div class="'.$x[0].'">'.$x[1].'</div>'; }
?>

How to make group (if specific field are same text) in loop?

I tried render data in loop and if extend_tag field have same text group in one container.
I only know hard code like below, loop data base on how many known extend_tag group, but actually the extend_tag numbers is unknown might be tag_ and any digit , any idea how to solve it?
data
[tag] => Array (
[0] => Array (
[id] => 1
[extend_tag] => tag_0
)
[1] => Array (
[id] => 2
[extend_tag] => tag_11
)
[2] => Array (
[id] => 3
[extend_tag] => tag_4
)
)
<ul class="container">
<?php foreach($rows['tag'] as $eachRowsTag) { ?>
<?php if ($eachRowsTag['extend_tag'] == 'tag_0') { ?>
<li>><?php echo $eachRowsTag['id']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<ul class="container">
<?php foreach($rows['tag'] as $eachRowsTag) { ?>
<?php if ($eachRowsTag['extend_tag'] == 'tag_1') { ?>
<li>><?php echo $eachRowsTag['id']; ?></li>
<?php } ?>
<?php } ?>
</ul>
...
Why not group them first, then iterate over the resulting array. Something like the following.
foreach ($tags as $tag) {
$grouped[$tag['extend_tag']][] = $tag;
}
// Now $grouped is something along the lines of:
// [
// 'tag_0' => [
// [ 'id' => 1, 'extend_tag' => 'tag_0'],
// ..
// ],
// ..
// ]
foreach($grouped as $extend_tag => $tags) {
echo "All tags in $extended_tag.";
foreach($tags as $tag) {
echo $tag['id'];
}
}
// For something like:
// All tags in tag_0.
// 1
// 4
// All tags in tag_1.
// ..

loop through array of objects

I Have an array called $pages content is as follows:
Array
(
[01-about-us] => Page Object
(
[_uri] => about-us
[_menuItem] => 01
[_visable] => 1
)
[02-contact] => Page Object
(
[_uri] => contact
[_menuItem] => 02
[_visable] => 1
)
[_sitemap] => Page Object
(
[_uri] => sitemap
[_menuItem] =>
[_visable] =>
)
[home] => Page Object
(
[_uri] => home
[_menuItem] =>
[_visable] => 1
)
)
is there an easy way to loop through and get page objects by there properties ie:
<?php foreach($pages->_visible() AS $p): ?>
<li> page </li>
<?php endforeach ?>
No. You will have to use an if:
foreach ($pages as $page) {
if ($page->_visible == 1) {
echo "<li>page</li>";
}
}
(Note also you misspelt visible in the array, perhaps a typo?)
Or you can utilize PHP's array_filter function:
$pagesVisible = array_filter($pages, function($page) {
return (bool) $page->_visible;
});
foreach ($pagesVisible as $key => $page) {
print '<li>' . $key . '</li>';
}
Or shorthand it to:
$filter = function($page) {
return (bool) $page->_visible;
};
foreach (array_filter($pages, $filter) as $key => $page) {
print '<li>' . $key . '</li>';
}
You just need to loop through the pages array and inside the loop access the object properties like:
<?php foreach($pages as $k => $p): ?>
<?php if ($p->_visable === 1): ?>
<li><?php echo $k; ?></li>
<?php endif; ?>
<?php endforeach; ?>
Please note that visable is misspelled but thats how it is in your question

PHP Shuffle Array improve output?

I am using the following tutorial to learn how to shuffle in PHP, but the output is ugly. How do I change the output of the shuffle to be more in line with the poll I want to display in list format?
I am using the following tutorial to learn how to do this:
http://www.w3schools.com/php/func_array_shuffle.asp
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
?
But the output is ugly: Array ( [0] => Cat [1] => Dog [2] => Horse )
How do I make the shuffle array look nice an pretty like this?
<ul>
<li><input id="pollRadioButton1" name="pollAnswerID" type="radio" value="1" /> Answer1 for Poll1</li>
<li class="pollChart pollChart1"> </li>
<li><input id="pollRadioButton2" name="pollAnswerID" type="radio" value="2" /> Answer2 for Poll1</li>
<li class="pollChart pollChart2"> </li>
</ul>
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
?>
<ul>
<?php foreach ($my_array as $key => $element): ?>
<li>
Whatever you want to do with
<?php echo htmlspecialchars($element) ?>
(<?php echo htmlspecialchars($key) ?>).
</li>
<li class="pollChart pollChart1"> </li>
<?php endforeach ?>
</ul>
print_r() is just mentioned for debugging only.
To get a 'clean' output you have to specify how things should be ouput:
<?php
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
foreach ($my_array as $key=>$val)
echo sprintf('%s: %s<br>'."\n",$key,$val);
?>
In your case the output might be something like:
echo sprintf('<li><input id="pollRadioButton[]" name="%1$s" '.
'type="radio" value="%2$s" />%2$s</li>'."\n".
'<li class="pollChart pollChart1"> </li>'."\n",$key,$val);

Trying to have an array with 2 values in side by side

I need to create an array where i can use 1,2,3 but have 1v1, 2v2, Clubs associated with them.
I am going to use 1,2,3 for option value and 1v1,2v2 and clubs to display to the user.
How can I store this in an array and then use foreach to extract?
Thanks
$data = array(
1 => '1v1',
2 => '2v2',
3 => 'Clubs';
);
echo '<select>';
foreach($data as $value => $title) {
echo '<option value="'.$value.'">'.$title.'</option>';
}
echo '</select>';
You can make your original values (1,2,3) the keys and the (1v1, 2v2, Clubs) the values.
$data = array(1 => '1v1', 2 => '2v2', 3 => 'Clubs');
foreach($data as $key => $value) {
print $key.' - '.$value.'<br/>';
}
Maybe, I don't understand your question. try to use the next array:
$array = array(1 => '1v1', 2 => '2v2');
And foreach:
<select>
<? foreach ($array as $k => $v) { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<? } ?>
</select>
I don't fully understand the question, but it seems like you want to map the values 1,2,3 to the text '1v1','2v2','Clubs'. PHP supports associative arrays that are suitable for this purpose:
$a = Array(
1 => '1v1',
2 => '2v2',
3 => 'Clubs'
);

Categories