PHP how to foreach atts to list item - php

i have array
$id = $atts['home_blog_id'];
$slug = str_replace( array('post:','post'), array('',''), $id);
if echo $id have array: post:du-an and post:tin-tuc
if echo $slug have array: du-an and tin-tuc
how to foreach $slug to list item as
<li>du-an</li>
<li>tin-tuc</li>
Thanks.

Explode the items to an array the either foreach as you say or use implode.
$atts['home_blog_id'] ="post:du-an,post:tin-tuc";
$id = $atts['home_blog_id'];
$slug = str_replace( array('post:','post'), array('',''), $id);
echo "<li>" . implode("</li><li>", explode(",", $slug)) . "</li>";
//<li>du-an</li><li>tin-tuc</li>
https://3v4l.org/4j5OK
As Nigel requsted, a foreach version.
foreach(explode(",", $slug) as $val){
echo "<li>" . $val . "</li>";
}

Related

create a string variable with all of an array's items [duplicate]

This question already has answers here:
PHP Implode wrap in tags
(3 answers)
Closed 11 months ago.
I'd like to create a string variable from an array.
Here's a non working code for conceptualizing it:
$list = array('elem1', 'elem2', 'elem3', 'elem4');
$myString = foreach ( $list as $element ){
'<span>' . $element . '</span><span>|</span>';
};
You can do something like this
<?php
$lists = array("elem1", "elem2", "elem3", "elem4");
$string = '';
foreach ($lists as $element){
$string = $string . ' | '. $element;
};
echo '<span>' . $string . '</span>';
?>
$list = array('elem1', 'elem2', 'elem3', 'elem4');
foreach( $list as $element ){
$myString[] = '<span>' . $element . '</span><span>|</span>';
}
print(implode('', $myString));
What you could do is concatenate the results of each iteration of your loop to a variable that exists outside of the loop.
Example:
$list = array('elem1', 'elem2', 'elem3', 'elem4');
$myString = '';
foreach ( $list as $element ){
$myString .= $myString . '<span>' . $element . '</span><span>|</span>';
};
print($myString);

How to remove first element of an array using loop

I have very little experience with PHP, but I'm taking a class that has PHP review exercises. One of them is to create a function that uses a loop to return all values of an array except the first value in an unordered list. I'm assuming there's a way to do this using a foreach loop but cannot figure out how. This is what I had but I feel like I am far off:
<?php
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');
$myName = $array['myName'];
$favColor = $array['favColor'];
$favMovie = $array['favMovie'];
$favBook = $array['favBook'];
$favWeb = $array['favWeb'];
echo '<h1>' . $myName . '</h1>';
function my_function() {
foreach($array == $myName){
echo '<ul>'
. '<li>' . $favColor . '</li>'
. '<li>' . $favMovie . '</li>'
. '<li>' . $favBook . '</li>'
. '<li>' . $favWeb . '</li>'
. '</ul>';
}
}
my_function();
?>
The correct syntax of foreach is
foreach (array_expression as $key => $value)
instead of
foreach($array == $myName){
function that uses a loop to return all values of an array except the
first value
I'm not sure, what exactly you mean by except the first value. If you are trying to remove first element from the array. Then you could have used array_shift
If you are supposed to use loop then
$count = 0;
foreach ($array as $key => $value)
{
if ($count!=0)
{
// your code
}
$count++;
}
Change the code to following
<?php
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');
$myName = $array['myName'];
echo '<h1>' . $myName . '</h1>';
function my_function($array)
{
$count = 0;
echo "<ul>";
foreach($array as $key => $value)
{
if($key != "myName")
{
echo "<li>".$value."</li>";
}
}
echo "</ul>";
}
my_function($array);

PHP: Explode two delimiters with two foreach loop

I am trying to get two foreach loop by explode with two delimiters <> and "\n" but getting error. Warning: Invalid argument supplied for foreach()
Here is my code
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
$descs = explode("\n", $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';
?>
The value entering into textarea something like this Title here<>this is the first scooter ever made.
Title here 2<>another line for specification In fact I would like to make it like <title 1> here detail text
Thanks a lot
actually you should do something like this
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$descs = explode("\n", $specifications);
echo '<dl>';
foreach($descs as $desc){
$title = explode('<>', $desc);
echo '<dt>' . $title[0] . '</dt>';
for($i=1; $i<=count($title); $i++){
echo '<dd>' . $title[$i] . '</dd>';
}
}
echo '</dl>';
?>
The $descs variable isn't an array because the first foreach loop sets $descs.
See this line :
foreach($titles as $title => $descs){
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
$descs = explode("\n", $descs);
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';

PHP - Split an array into chunks.

I have a variable on my page called, $recipe['ingredients'];
inside the var you have as follows,
100ml milk, 350ml double cream, 150ml water
and so on. Now I'm trying to split it up so it looks as follows
<ul>
<li>100ml milk</li>
<li>350ml double cream</li>
<li>150ml water</li>
</ul>
So far I have the following code,
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$ingredients = array($ingredientsParts);
while (! $ingredients) {
echo" <li>$ingredients</li>";
}
But for some reason it doesn't work and I do not have the experience with explode to fix it.
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$li = '<ul>';
foreach($ingredientsParts as $key=>$value){
$li.=" <li>$value</li>";
}
$li.= '</ul>';
echo $li;
this should be enough:
$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{
echo "<li>$ingredient</li>";
}
or you can explode it by ',' and use echo '<li>' . trim($ingredient) . '</li>'; to remove whitespace from beginning/end of that string
When you explode() a string it is automatically converted into an array. You do not need to convert it to an array type as you did on the second line.
You want to use a foreach() loop to iterate through an array, not a while loop.
$ingredientsAry = explode(',', $row_rs_recipes['ingredients']);
foreach($ingredientsAry as $ingredient){
echo "<li>$ingredient</li>";
}
In fact you can just do a foreach() loop on the explode() value
foreach(explode(',', $row_rs_recipes['ingredients']) as $ingredient){
echo "<li>$ingredient</li>";
}
The explode method already return an array, so you don't have to transform your variable $ingredientsParts into an array.
Just do:
$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{
echo "<li>$ingredient</li>";
}
if (!empty($recipe['ingredients'])) {
echo '<ul><li>' . implode('</li><li>', explode(', ', $row_rs_recipes['ingredients'])) . '</li></ul>';
}
You can do this:
$ingredients = explode(',', $row_rs_recipes['ingredients']);
$list = '<ul>';
foreach ($ingredients as $ingredient)
{
$list .= '<li>' . $ingredient . '</li>';
}
$list .= '</ul>';
echo $list;

Need help with foreach and XML

I have the following output (via link) which displays the var_dump of some XML im generating:
http://bit.ly/aoA3qY
At the very bottom of the page you will see some output, generated by this code:
foreach ($xml->feed as $entry) {
$title = $entry->title;
$title2 = $entry->entry->title;
}
echo $title;
echo $title2;
For some reason $title2 only outputs once, where there are multiple entries?
Im using $xml = simplexml_load_string($data); to create the xml.
You re-assign a value to $title and $tile2 in each iteration of the foreach loop. After the loop is finished only the last assigned value is accessible.
Possible alternatives:
// print/use the values within the loop-body
foreach ($xml->feed as $entry) {
$title = $entry->title;
$title2 = $entry->entry->title;
echo $title, ' ', $title2, "\n";
}
// append the values in each iteration to a string
$title = $title2 = '';
foreach ($xml->feed as $entry) {
$title .= $entry->title . ' ';
$title2 .= $entry->entry->title . ' ';
}
echo $title, ' ', $title2, "\n";
// append the values in each iteration to an array
$title = $title2 = array();
foreach ($xml->feed as $entry) {
$title[] = $entry->title;
$title2[] = $entry->entry->title;
}
var_dump($title, $title2);

Categories