Getting the value outside the foreach php loop - php

Here is the code used to get the ids of a images in a gallery
<?php $images = get_field('photogallery');?>
<?php foreach( $images as $image ): ?>
<?php echo $image['ID']; ?>
<?php echo ','; ?>
<?php endforeach; ?>
I get the output
1102 , 3380 , 3348 , 3354 , 3355 ,
I would like to get this outside the loop because the result must be used in other shortcode also I see there is a whitespace after every number.
the result must be
1102,3380,3348,3354,3355
Please help me.. thanks

You don't need to put <?php ... ?> everytime everywhere for each statement. Keep in mind that each time you close with ?> all characters are sent to the client until the next opening <?php, that's why you obtain spaces around each comma:
<?php foreach( $images as $image ): ?>#
#####<?php echo $image['ID']; ?>#
#####<?php echo ','; ?>#
<?php endforeach; ?>
(I changed white-spaces to #, this way you can see characters sent to the client (the browser)).
You can use array_map to "filter" only ID items and implode to join them , then you only need to store the result in a variable ($result here).
<?php
$images = get_field('photogallery');
$result = implode(',', array_map(function ($i) { return $i['ID']; }, $images));
echo $result;
?>
Now you can use $result later everywhere you want.

Much simpler:
<?php echo implode(',' array_column($images, 'ID')); ?>
extract the ID values using array_column()
implode() those array values using a comma

Related

How to separate each result with a comma in PHP

So I'm using:
<?php print $variable; ?>
and sometimes there are multiple variables being printed, but I'm not able to tell how many. Is there a way of printing the variables separated by a comma, but only when there are more than one?
Thanks!
This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
<?php
var_dump($variable);
?>
if your $variable is an array count it like count($variable) and than put you condition like
if(count($variable)>1) { $imp=implode(',',$variable); }
hope this will help you..
If it's not an array
<?php /* somwhere before... */ $sep = ''; ?>
<?php print $sep.$variable; $sep = ',' ?>
If it's array
<?php print implode( ',', $variable ); ?>

Include single lines of php in ordered list

I am fairly new to php and not sure if it is possible to include php file content line by line.
Say I have an ordered list and a php file with three lines. I can include the whole content using
<?php include 'myfile.php' ?>
my question is, if it is possible to include single lines.
myfile.php
apples
bananas
oranges
index.php
<ol>
<li>???????</li>
<li>???????</li>
<li>???????</li>
</ol>
Thanks
There are more possibilities one of them is to put your content in a text file and load the content with file for example. Then you can iterate through every line with foreach.
The other way is to put your values in an array if your don't need it in a file.
$fruits = array(
'apples',
'bananas',
'oranges'
);
Then you can iterate with foreach over that array.
http://php.net/manual/en/language.types.array.php
Before you start you should read about that things and perhaps you start at the beginning of that documentation.
You can read in the file as an array:
$myFile = file('myfile.php', FILE_IGNORE_NEW_LINES); // FILE_IGNORE_NEW_LINES strips the new line characters from the array
now you can either print a single line:
echo $myFile[2]; // echo the third item
or output everything as a HTML list:
echo '<ol>';
foreach ($myFile as item) {
echo '<li>'. $item .'</li>';
}
echo '</ol>';
You can do in various ways:
declaring variables for each one:
myfile.php
<?php
$apples = "apples";
$bananas = "bananas";
$oranges = "oranges";
and print them in the list:
index.php
<ol>
<li><?php echo $apples; ?></li>
<li><?php echo $bananas; ?></li>
<li><?php echo $oranges; ?></li>
</ol>
OR declaring one array with all of them.
myfile.php
<?php
$fruits = array( 'apples', 'bananas', 'oranges' );
and print them in the list using a foreach:
index.php
<ol>
<?php
foreach ($fruits as $fruit){
echo '<li>'.$fruit.'</li>';
}
?>
</ol>
Good Luck!

Foreach - show the number of items

I have this code to count the objects in the foreach loop (images in a gallery):
<?php $images = get_field('galerie'); if( $images ): ?>
<?php $i=1; foreach( $images as $image ): ?>
<?php echo $i++; ?>
<?php endforeach; ?>
<?php endif; ?>
So when i have 4 items (images) it gives me
1 2 3 4
But i need only the highest number like:
4
when i have 4 imgages. Any idea how do i get this number?
Why not just count the images ?
<?php
$images = get_field('galerie');
if($images && is_array($images)) { // in case get_galerie returns null or empty strings when no images...
echo count($images);
}
?>
Also, no need to put <?php ... ?> tags on each line.
Enclose a whole block of PHP code into this tag, it's far more readable, and works the same.
There is a count function.
$length = count($images);
it's simple, just do it like this:
<?php $images = get_field('galerie'); if( $images ): ?>
<?php $i=0; foreach( $images as $image ): ?> //$i=0 not 1
<?php $i++; ?>
<?php endforeach; ?>
<?php echo $i; ?>
<?php endif; ?>
L.E: just for those who down-voted my answer, I kept the users way of coding for 2 (I thought obvious) reasons:
He might actually need the foreach()
He might be new to PHP, and if the iterator method ($i = 1; $i++) is what he knows he should stick with it until he discovers something new. This is a algorithm method inherited from C, Pascal and other much "older" programming languages.
Of course, the count() method is faster and gives a way less pain in the behind, but this is NOT a bad way to code for a beginer.
Hope this helps! :D

How to get an array of posts ids in types_child_posts?

<?php $session = types_child_posts("child");?>
<?php foreach ($session as $session_posts):?>
<?php session_start(); $_SESSION['posts'] = "$session_posts->ID"; echo $_SESSION['posts']?>
<?php endforeach; ?>
I want to send posts IDs as array to session, but in this case I received a space separated numbers.
How to translate it to array ?
<?php $explodedPostIds = explode(' ', $yourPostIDs); ?>
You can just explode them by the space.
Like this you always create a new array index when the function reaches a space ' '

Php Implode Not Working with my code

i have an array and i want to convert this array in comma seprated string by implode function but this is not working. my code is below.
<?php foreach ($article['hashtags'] as $hashtags) { ?>
<?php $hastagg=mysql_real_escape_string(implode(',',$hashtags)) ?>
<a><?php echo $hastagg; ?></a>
<?php } ?>
Have you tried this:
<?php
$hastagg = htmlentities(implode(',' ,$article['hashtags'])) ;
echo '<a>'.$hastagg.'</a>';
?>
I don't know about structure of $article array. But I have a feeling that foreach is unnecessary:
$hashtagg = implode(',', $article['hashtags']);
And why are you using mysql_real_escape_string for output? You can use htmlspecialchars or some other functions.

Categories