0

So, I have this type of array

$fields = array('result'=>234, '1'=>1, '2'=>2, '3'=>4, ....'20'=>5);

and I have o display it in a table, but the 'result' key has to be the last column. Until now I've used this approach, but the problem arrives as soon as i need to add other keys besides 'results', to the end of the table.

foreach($fields as $key=>$value) {
  if(strcmp('key', 'result') != 0)
    echo "<td>$value</td>";
  }
echo "<td>$fields['result']</td>";

I assume that is has to be a better approach. What do you think?

5 Answers 5

3

I assume you know the names of the keys you want to display at the end, so you can do something like the following:

$result = $fields['result'];
$otherResult = $fields ['other_result'];
unset($fields['result'], $fields['other_result']);

foreach($fields as $key=>$value) {
    echo "<td>$value</td>";
}

echo "<td>$result</td>";
echo "<td>$otherResult</td>";

This is only useful to do for a small number of keys.

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

Comments

2
// array of columns you want to add at the end of the table in sequence
// I used an array of columns and an exrta foreach loop to be able to support possible future more columns to be added
$last_cols = array('result'=>null, 'other'=>null);

foreach($fields as $key=>$value) {
    // check if current column is one of the cols to be added at the end
    if (in_array($key, array_keys($last_cols))) {
        $last_cols[$key] = $value;
        continue;
    }

    echo '<td>'.$fields['result'].'</td>';
}
// loop through last columns
foreach($last_cols as $col) {
    echo '<td>'.$col.'</td>'
}

Comments

1

There is faster way:

$result = $fields['result'];
$otherResult = $fields['otherResult'];

unset($fields['result'], $fields['otherResult']);
foreach($fields as $key=>$value) {
  echo '<td>' . $value . '</td>';
}

echo '<td>' . $result . '</td>';
echo '<td>' . $otherResult . '</td>';

You don't check key every time.

However if you really want - do it in following way:

if( $key == 'result' )

2 Comments

Where is the extra key OP requested at the end of the table?
Huh - I've noticed that @Jan-Henk has an identical answer. o0
1

In this case your Datastructure is not well formed. Why dont you use

$result = 123;

? Or a simple object containing $result and $values like so:

class MyDataContainer {
   public $result = 0;
   public $values = array();
}

? I would recommend this, since it would be a lot cleaner.

1 Comment

If you have completely different types of values, they should be structured correctly. since array(1,2,3,4) is a list of numbers and array('result' => 999) is a single scalar result they should not be mixed in one structure in the first place.
0

Make the first element the last element in an array (as you don't use the keys in your question):

$fields = array('result'=>234, '1'=>1, '2'=>2, '3'=>4, ....'20'=>5);

$fields[] = array_shift($fields);

Demo

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.