Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I execute on my main page some php code which includes :
foreach ($fbdata->feed->data as $fbpost)
{
...
}
How can we convert this , into a loop that goes from (i to z)(0 to 10) ?
Simple for loop
for($i = 0; $i < 10; $i++) {
$fbpost = $fbdata->feed->data[$i];
...
}
or if you like to use the as, try using a foreach but slicing the array before using it
$fbPosts = array_slice($fbdata->feed->data, 0, 10);
foreach($fbPosts as $fbpost) {
...
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am trying to count the records in the table, but it's showing me nothing in the response, I am working on an API, can anybody help please ?
public function devicesCount()
{
$x = Device::count();
if ($x) {
return ['devices' => $x];
} else {
return ['message' => 'ranii karrezt ydyyyyyyk'];
}
}
use $x = Device::all()->count(); in place of $x = Device::count();
or use Laravel Aggregates like
$count = DB::table('devices')->count();
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to get specific content from a remote web page
The content I want to get are inside javascript variables, this kind :
var Example1 = 0;
var Example2 = 14;
The name of the variable remain the same and the content is only numbers
Thank you
Find scripts in html source by DomDocument and then variable declaration by regex
$DOM = new DomDocument();
$DOM->loadHTML( $output);
$res = [];
$scripts = $DOM->getElementsByTagName('script');
$lnt = $scripts->length;
for($i=0; $i < $lnt; $i++) {
preg_match_all('/var\s+(\w+)\s*=\s*(\d+)\s*;/', $DOM->saveHtml($scripts->item($i)), $m);
$res = array_merge($res, array_combine($m[1], $m[2]));
}
print_r($res);
demo
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I ask a candidate : What do you like ?
His answer is an array [Math,Physics,Chemistry,IT,Sports]
I do have a list :
Software Analyst : [Math,IT,Programing,Stress,Computer]
Truck drive : [Sports,Driving,Traveling,Exchange]
Physic-chemistry teacher :[Math,Physics,Chemistry,Teaching]
What is the best way in PHP to get the jobs with the maximum common tags with the user input?
You use array_intersect() to find the common tags, then compare among the returned tags amount.
$jobs = array(
'software' => array('Math','IT','Programing','Stress','Computer')
'truck' => array('Sports','Driving','Traveling','Exchange')
'physic' => array('Math','Physics','Chemistry','Teaching')
);
$user_tags= array('Math','Physics','Chemistry','IT','Sports');
$max_common = 0;
$desired_job = '';
foreach($jobs as $key=>$job) {
$common = count(array_intersect($job,$user_tags);
if(max_common < $common) {
$desired_job = $key;
$max_common = $common;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to parse out a very specific part of a page using PHP and i'm currently using:"getElementsByTagName"
and it's actually working BUT, it seems that it doesn't clear everything as there are other lines with similar tags,
so i've tried to look for a better pattern and found a unique attached "class" tag.
<li class="unique">
//get all li
$items = $DOM->getElementsByTagName('li');
//display all LI text
for ($i = 0; $i < $items->length; $i++)
echo $items->item($i)->nodeValue . "<br/>";
You can get all the unique class names using an XPath:
$xpath = new DOMXpath($DOM);
$items = $xpath->query('//li[#class="unique"]');
And loop them over:
for ($i = 0; $i < $items->length; $i++) {
echo $items->item($i)->nodeValue . "<br/>";
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i'm trying to do a simple condition check like:
$test = 1;
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
echo $test_;
The output is Test-0 but must be Test-1.
Which could be the problem?
Try this : Put brackets for conditions.
$test = 1;
$test_ = ($test==-1)?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
echo $test_;
output :
Test-1
change this
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
to
$test_ = $test==-1?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
working example http://viper-7.com/msuCyk
// output Test-1
Ternary operator in PHP is left to right, therefore, its executed like this:
$test_ = (( ( $test==-1 ? "Test--1" :$test==1) ? "Test-1":$test==0)?"Test-0":"Test");