0
foreach($cases['rows'] as $i => $item) {
    foreach($array as $key => $val) {
        if($item['status'] == $key) {
            echo $val;
        }
    }
}

Right now this code functions, but if $item['status'] != $key it echoes nothing. I've tried to add an else statement after the if statement except it prints it tens of times.

How can I achieve this functionality? I want it to print $item['status'] if $item['status'] != $key

Help is appreciated.

Thanks.

10
  • Can you provide an example of $cases['Rows'];? Commented Jul 22, 2014 at 16:29
  • Post some example data Commented Jul 22, 2014 at 16:29
  • you have to show more, because if it works for !=, it works for ==, it comes from somewhere else Commented Jul 22, 2014 at 16:30
  • Sounds like you might want to put the if statement outside the inner loop. Commented Jul 22, 2014 at 16:30
  • 2
    So... This code works, but a version you're not showing us doesn't work? Can you maybe share the non-working version so we can help? Commented Jul 22, 2014 at 16:32

3 Answers 3

2

The way I understand the question you have two arrays:

  1. An array containing different abbreviations and their full meaning.
  2. Another multidimensional array containing arrays which again contain status-abbreviations.

To echo the full meaning instead of the abbreviations:

$abbreviations = array('NT' => 'Not taken',
                       'N/A' => 'Not available');

$data = array(array('status' => 'NT'),
              array('status' => 'N/A'));

foreach($data as $item) {
    if(array_key_exists($item['status'], $abbreviations)) {
        echo $abbreviations[$item['status']] . PHP_EOL;
    } else {
        echo $item['status'] . PHP_EOL;
    }
}

Result:

Not taken

Not available

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$test = null;
foreach($cases['rows'] as $i => $item) {
    foreach($array as $key => $val) {
        if($item['status'] == $key) {
            echo $val;
        }
        else {
            $test = $val;
        }
    }
}
if($test != null) {
    echo $test//Or whatever you want to display
}

2 Comments

This appends the last value in my array of key/values to the end of every row.
I changed the echo $test to echo $item['status'] and now it works fine EXCEPT it tacks on the key to the end of each row.
0

Without more info regarding the type of data in both arrays, I would suggest you to try:

  • !($item['status'] == $key) deny the correct statement
  • $item['status'] !== $key try also checking the same type (test this also with the equal statement to see if you get the results you expect)

Comments

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.