0

I've got the following snippet to dump some info on the screen:

for ($i=0; $i < 60; $i++) {
  echo 'output: '.$array[$i]['options'][0]."<br />";
  echo 'output: '.$array[$i]['options'][4]."<br />";
}

which outputs the following:

output: Lorem ipsum
output: 1
output: dolor sit amet
output: 1
output: consectetur adipiscing elit
output: 0

How do I wrap the echo statements in a conditional, so only when ['options'][4] == '1' it will run?

if ($array[$i]['options'][4] == '1') {
...
}

Doesn't work and returns nothing, set it to == '0' and you'll get everything. I've tried assigning it to a variable first, tried converting to ints first, but they all seem to return 0, while an echo displays the value.

Got night-time creative with foreach loops, trying to drill down to the items, only resulting in loss of keys where values were still properly read. Its probably something simple, but its been ages since I dabbled with php and arrays.

2
  • if $array[$i]['options'][4] == '1' does not work, var_dump it first and see what does it contain. Commented Jan 16, 2013 at 7:46
  • Well last night I could have sworn it didn't, but now it did, must have made some typos.. Commented Jan 16, 2013 at 8:00

1 Answer 1

2

Wrap it with a simple if. But why use txt? shouldn't you use options?

if(isset($array[$i]['options'][4]) and $array[$i]['options'][4]==1){
  echo 'output: '.$array[$i]['options'][0]."<br />";
  echo 'output: '.$array[$i]['options'][4]."<br />";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks should have asked this last night instead of wasting nightly hours being blind and get a SO within minutes! The ['txt'] vs ['options'] was a typo in making it more SO friendly.

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.