0

I'm working on a tool to query a government database or information. Sam.gov actually. They have a public API and up until now everything has been going smooth but I ran into an issue with an array that returns a 1 instead of the word "Yes" That I'd like it to.

http://gsa.github.io/sam_api/sam/fields.html (This is the API documentation)

Here is the code that I can't figure out...

foreach($decoded_results['sam_data']['registration']['naics'] as $naics){
    echo '<strong>Is Primary: </strong>'.$naics['isPrimary'].'</br>';
    echo '<strong>Naics Code: </strong>'.$naics['naicsCode'].'</br>';
    echo '<strong>Naics Name: </strong>'.$naics['naicsName'].'</br>';
}

I need the isprimary to return a Yes or a No instead of nothing of a number 1, anyone have any solutions?

3
  • 1
    '.$naics['isPrimary'].' to '. ($naics['isPrimary'] == true)?'yes':'no' .' Commented Apr 2, 2016 at 7:39
  • In many programming language 1 == true and 0 == false Commented Apr 2, 2016 at 7:39
  • echo '<strong>Is Primary: </strong>' . ($naics['isPrimary'] ? 'yes' : 'no') .'</br>'; Commented Apr 2, 2016 at 10:01

1 Answer 1

1

From the api link isPrimary is boolean type so

your code should be

foreach($decoded_results['sam_data']['registration']['naics'] as $naics)
{
    echo '<strong>Is Primary: </strong>'.($naics['isPrimary'] === true)?'yes':'no'.'</br>';
    echo '<strong>Naics Code: </strong>'.$naics['naicsCode'].'</br>';
    echo '<strong>Naics Name: </strong>'.$naics['naicsName'].'</br>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Tried what you suggest and now every line returns a value of yes although I know only one of them should say yes. You can check out what's happening for yourself by going to samhelpdesk.us and using the search tool on the home page. In the search box enter 609108654 and select DUNS Number as the criteria. Click Search and when the results return scroll down and click see more information, on that page scroll down the NAICS section and you'll see. It just repeats yes a bunch of times. I know it's returning an array but I'm at a total loss how to parse it.
Still getting a return of yes for everything displayed in the array.
@JohnChase can you do var_dump($naics['isPrimary']); in foreach for testing..... let me know the output... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.