Recently I am working with nest for loop but one loop work and another not working suppose I have 2 for loop.
For example
$data =DB::table('data')->get();
$job =DB::table('job')->get();
$recruiter =DB::table('recruiter')->get();
$admin =DB::table('commission')->get();
for($i=0;i<count($job);i++){
if(!$job->isEmpty()){
for($j=0;j<count($job);j++){
if( $data[$i]->job_id == $admin[$j]->job_id )
$job[$i]=$data[$i];
}
//if checking complete then skip $i or increment it (less than count($job)) or skip
//this index $i and continue with outer loop mean i++
}
}
I had done lots of research but haven't found any solution with this kind of problem
You have missed the $ in i and j
$data = DB::table('data')->get();
$job = DB::table('job')->get();
$recruiter = DB::table('recruiter')->get();
$admin = DB::table('commission')->get();
for($i=0; $i < count( $job ); $i++){
if($job->isEmpty()){
continue;
}
for($j=0; $j < count( $job ); $j++){
if( $data[$i]->job_id == $admin[$j]->job_id ){
$job[$i]=$data[$i];
}
}
}
Related
I still don't get how to create a dynamic table using PHP.
I would like to display a table with a variable amount of columns and rows containing numbers between 1 and 200.
Example of the expected output
Can anyone help me with this?
A nested loop within an outer loop should do it - the outer loop controlling the rows and the inner loop controlling the columns.
$rows=20;
$cols=10;
$x=1;
$html=array();
$html[]="<table>";
for( $i=0; $i < $rows; $i++ ){
$html[]="<tr>";
for( $j=0; $j < $cols; $j++ ){
$html[]="<td data-cell='r{$i}c{$j}'>$x</td>";
$x++;
}
$html[]="</tr>";
}
$html[]="</table>";
echo implode( PHP_EOL, $html );
To cease creation when a pre-defined limit is reached
$rows=100;
$cols=10;
$x=1;
$limit=200;
$html=array();
$html[]="<table>";
for( $i=0; $i < $rows; $i++ ){
$html[]="<tr>";
for( $j=0; $j < $cols; $j++ ){
$html[]="<td data-cell='r{$i}c{$j}'>$x</td>";
$x++;
if( $x > $limit ) break;
}
$html[]="</tr>";
if( $x > $limit ) break;
}
$html[]="</table>";
echo implode( PHP_EOL, $html );
<?php
for($i=1;$i<=30;$i++) {
echo "*$i*";
if($i%10==0){
echo "<br>";
}
}
LIVE DEMO
Please tell me how can I display array in a for loop in php?
I have this array:
while($q = mysqli_fetch_assoc($selectQuestResult)) {
$answers[] = array(array($q['name']),array(round($total,1)));}
then I want display it:
for($i = 0; $i < count($answers); $i++)
{
echo($answers[$i][0][0].':'.$answers[$i][1][0]."<br />\n");
}
I must use a for loop makes this sound like stupid college professor tricks. But, that's ok. for loops can have break statements in them. And, they don't have to contain end conditions in the for statement.
for( $i = 0; ; $i++) {
$q = mysqli_fetch_assoc($selectQuestResult);
if (!$q) break;
$answers[] = array($q['name'], round( $total, 1));
}
for ( $i = 0; $i < count( $answers ), $i++) {
/* do something with $answers[i] */
}
I am trying to remove duplicates from a set of tokens using unset (not considering array_unique for now), however I am getting a few issues.
$keywords = parseTweet ( $tweet );
$term_freq = array(count($keywords));
for($i = 0; $i < count($keywords); $i++){
$term_freq[$i] = 1;
for($j = 0; $j < count($keywords); $j++){
if (($i != $j) && (strcmp($keywords[$i],$keywords[$j]) == 0)){
unset ( $keywords [$j] );
unset ( $term_freq [$j] );
$term_freq[$i]++;
}
}
}
print_r ( $keywords );
print_r ( $term_freq );
I am aware of why I am getting an error; while the duplicate $j is removed, the for loop still has to reloop for the rest of the keywords and hence will fail when it encounters the missing $j. Checking the contents of the array, I found out that the index of the array skips the index $j. So it reads; [1], [2], [4], ... etc where $j = [3]
I thought that unset also rebalances the array index, am I doing something wrong or missing something completely? I am new to PHP so please bear with me!
Check if the index is set or not.
You're making needless, repetitive comparisons. Basically n² comparisons when at most n²/2 are required to compare every value in an array to every other value.
So:
$c = count($keywords)
for($i = 0; $i < $c; $i++){
$term_freq[$i] = 1;
for($j = $i+1; $j < $c; $j++){ // magic is $j = $i+1
if( ! isset($keywords[$j]) { continue; } // skip unset indices
else if ( strcmp($keywords[$i],$keywords[$j]) == 0 ){
unset ( $keywords [$j] );
unset ( $term_freq [$j] );
$term_freq[$i]++;
}
}
}
Basically you know you've already checked everything prior to $i, so you can start your inner loop at $i+1 instead of zero.
Also, you only need to count $keywords once, not n² times.
Use foreach instead of for.
foreach ($keywords as $i => $kw1){
$term_freq[$i] = 1;
foreach ($keywords as $j => $kw2){
if (($i != $j) && ($kw1 == $kw2){
unset ( $keywords [$j] );
unset ( $term_freq [$j] );
$term_freq[$i]++;
}
}
}
I have an array of route elements (point1, point2, ...) to provide to a map engine.
I don't know how many points I have. Each point is an array of possible addresses.
I need to perform a check for every combination possible of these points, but only one successful check is required.
My array looks like something akin to:
$point[0] = array($address1, $address2, $adress3);
$point[1] = array($address1, $address2);
$point[2] = array($address1, $address2, $adress3, $adress4);
$point[n] = ...
I want to perform a test for combination: $point[0][0] - $point[1][0] - $point[2][0], $point[0][1] - $point[1][0] - $point[2][0], and so on ! :)
The first successful test (route found) should end the function.
I'm trying to do something with recursion but have spent many hours on this without success.
If I got you right, you want to have a "cartesian product".
This is an example function for it:
It first checks, if there are any subvalues in one of the subarrays and then it creates an array with all possible arraycombinations and returns it.
<?php
function array_cartesian_product($arrays)
{
$result = array();
$arrays = array_values($arrays);
$sizeIn = sizeof($arrays);
$size = $sizeIn > 0 ? 1 : 0;
foreach ($arrays as $array)
$size = $size * sizeof($array);
for ($i = 0; $i < $size; $i ++)
{
$result[$i] = array();
for ($j = 0; $j < $sizeIn; $j ++)
array_push($result[$i], current($arrays[$j]));
for ($j = ($sizeIn -1); $j >= 0; $j --)
{
if (next($arrays[$j]))
break;
elseif (isset ($arrays[$j]))
reset($arrays[$j]);
}
}
return $result;
}
?>
what I want to do is run this for loop and within there is a foreach searching the positions. what I want to do is once there it returns false I want it to break and save the position of $i in a variable. I'm using simple_html_dom.php but I don't think that matters since this is more of a basic programming problem.
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
break;
}
}
//this is not valid, but essentialy this is what I want to do.
$stop = $i;
}
To break multiple levels in a loop you simply specify the levels, eg, break 2 - see the manual on break - http://www.php.net/manual/en/control-structures.break.php.
As such your code might work as
for($i = $0; $i < $20; $i++){
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i; // Set variable
break 2; // break both loops
// or alternatively force the outer loop condition to expire
//$i = 21; // Force the outer loop to exit
//break;
}
}
}
I have expanded to question to set $i = 21 to break the outer loop with a single break.
Untested code but syntax checked...
<?php
// Untested code...
// Assume that you WILL break out of the loops...
$currentForIdx = -1; // so we can test that 'for' loop actually ran
$quitLoops = false;
for($i = 0; $i < $20 && !quitLoops; $i++) {
$currentForIdx = $i; // in case we break out of the loops
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false) {
$quitLoops = true;
break;
}
}
}
// test $quitLoops and $currentForIdx to work out what happened...
?>
I havent tested this, but I would try something like this:
for($i = $0; $i < $20; $i++){
$stop = false;
foreach($html->find('div[class=cate_link]',$i) as $a){
if (strpos($a->plaintext,'+info') == false){
$stop = $i;
}
}
if ($stop !== false) {break;}
}