Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As we know foreach loop will execute till condition does not become false.
I want to execute it only once.
$i=0;
foreach($html->find('img') as $element)
{
if($i!=0)
break;
$logo= $element->src . '<br>';
$i++;
}
Is there any other solution for this? like foronce in place of foreach?
As we know foreach loop will execute till condition does not become false.
A foreach loop will execute once for every item of the container unless a return or break condition is defined within the foreach block.
I want to execute it only once.
Loops that executes once are called "don't use a loop in the first place". Here's your example fixed:
$logo = $html->find('img')[0]->src . '<br>';
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a textfile which contain words like that:
Yigit Ozkavci 19
Efe Ozkavci 18
Yeni Bar 35
.
.
.
They are exist in database and also in textfile, what I want to do is to read those datas and get them in seperate variables, I know how to read a file line by line, but with a foreach loop can I echo them seperately and send data to another php file ?
Why not just make use of file() ? It reads entire content inside an array.
<?php
$arr = file('yourfile.txt');
You can access Yigit Ozkavci 19 by echo $arr[0]; and others so on...
If you want to process it... you can just run it on foreach like this
foreach($arr as $val)
{
echo strtoupper($val); // Just a demonstration ..
}
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . $line . "<br />\n";
}
?>
Hope This Helps :)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this PHP code that displays values in an array:
$header->from[0]->mailbox . "#" . $header->from[0]->host;
whats the best way to loop round to get [1], [2], [3], etc...
You can do it through a foreach loop like this:
foreach ($header->from as $from){
echo $from->mailbox . "#" . $from->host;
}
If you want to process the first element outside the loop, then you can use array_shift:
$first_from = array_shift($header->from);
$first_from->mailbox . "#" . $first_from->host;
foreach ($header->from as $from){
echo $from->mailbox . "#" . $from->host;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Why can't I call this function with the function and increment it's argument?
It seems to loop endlessly without $i incrementing.
function loopy($i) {
loopy($i+1);
echo $i.'...';
if ($i >=5) return true;
}
Because the if statement needs to go before calling the function again to stop it beign an infinite loop:
function loopy($i) {
if ($i >=5) return true;
loopy($i+1);
echo $i.'...';
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am struggling to get pull the correct values
I am trying to get the following
packages>items then loop through the ids
{"id":1,"title":"Barnsley","slug":"barnsley","packages":[{"id":1,"title":"Group PACKAGE 1","items"
{"id":9,"title":"Guiness","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":10,"title":"John Smith","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":22,"title":"Chicken Cheese and Bacon Melt","band_a":4.5,"band_b":4.75,"band_c":5.0,"band_d":5.25,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null}],"additional_items":null,"event_types":null}]}
Your JSON isn't valid. You're missing two characters :[ that declare the items array. After fixing the JSON (check it on jsonlint.com) you can iterate over the item IDs like so:
$obj = json_decode($str);
foreach($obj->packages[0]->items as $item)
{
echo $item->id;
}
If there could be more than one package you can iterate those too:
foreach($obj->packages as $package)
{
foreach($package->items as $item)
{
echo $item->id;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
foreach(glob($select)) as $filename){
echo $filename;
echo "<a class='vlightbox1' href='$filename' title='$filename'><img src='$filename' style='height:120px; width:160px; alt='$filename'></a>";
echo "<a href='ap_deleteimages.php?id=$filename'>Delete</a>";
}
It is not working Properly. I Have Added $select Value as under
$select=document.write(document.getElementById('flist').value)
and 'flist' is a option tag id in html and glob function do not work with it
foreach(glob($select) as $filename){
echo $filename;
echo "<a class='vlightbox1' href='$filename' title='$filename'><img src='$filename' style='height:120px; width:160px; alt='$filename'></a>";
echo "<a href='ap_deleteimages.php?id=$filename'>Delete</a>";
}
You have the syntax error at glob function in your code. The right syntax is in above code.
Second thing is that glob() is used to find all the search pathname matching a pattern. So you need to check that the value of $select is correct or not.
Read more above glob() function.
How about putting in the parentheses?
foreach ( glob($select) as $filename)