Tania Rascia
Tania Rascia

Reputation: 1775

Return an else statement for foreach loop

I've spent about two hours trying to figure this out so finally I'm going to post it here.

I have a listing of jobs in a JSON feed that I'm pulling into PHP. I want to get all the jobs for a specific location. The location logic works fine, and I'm getting all the proper jobs.

My problem comes with getting an else statement to work. If I use the code below, it will return 'No jobs available' for each instance of a job. I can use i++ to only return it once, but it will return no matter what - whether my jobs match or not.

// This code does not work properly

foreach ( $json['jobs'] as $job ) {

$location = $job['job']['location'];

    if ( $location === $current_location ) {
        echo 'Job title';
    } else {
        echo 'No jobs available';
    }
}

This will return something like:

Job title
Job title
No jobs available
Job title

So basically I need to somehow get that else statement out of the foreach loop, and only display once.

I'm looking for, in English: for each job, list the jobs for the current location. If the current location has no jobs available, print a statement saying "No jobs available for your location."

I apologize for such a novice logic related question. Couldn't find the correct answer after a bit of searching.

Upvotes: 2

Views: 1338

Answers (3)

Felippe Duarte
Felippe Duarte

Reputation: 15131

Use a variable to put this information:

$jobs = [];

foreach ( $json['jobs'] as $job ) {

    $location = $job['job']['location'];

    if ( $location === $current_location ) {
        $jobs[] = $job['job']['title'];
    }
}
if(count($jobs) === 0) {
    echo '"No Jobs Found" found';
} else {
    foreach($jobs as $job) {
        echo "See this awesome job: ".$job;
    }
}

Upvotes: 3

Luke Mlsna
Luke Mlsna

Reputation: 481

Use array_filter() to filter out job items with matched locations using a simple custom function instead of a foreach loop.

Then just check if that filtered array is empty. If it is then echo "no jobs" message; if it is not empty then loop with foreach job item for that matched location.

<?php

function is_current_location($job) {
    if ( $job ['location'] == CURRENT_LOCATION ) {
        return true;
    } else {
        return false;
    }
}

$jobs = array_filter( $json['jobs'], "is_current_location" );

if ( empty ( $jobs ) ) {
    echo 'No jobs available';
} else {
    foreach ( $jobs as $job ) {
        echo $job['job_title'];
    }
}

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24661

If all you want to do is tell whether or not a single location is represented inside of your $jobs array, you don't need your own explicit loop to search it. You can combine array_search with array_column to get either an index of a job at the location or a boolean false:

$location = 'some location';
$key = array_search($location, array_column($json['jobs'], 'location'));

if(false === $key) {
    // Array has no jobs at $location
} else {
    // Array has jobs at $location
}

Upvotes: 1

Related Questions