0

i have array value like this

Array ( 
    [0] => A1
    [1] => A2
) 
Array ( 
    [0] => B1 
    [1] => B2
    [2] => B3
)

For exploding

$explodedeductedfinal = explode(",",$CODE);

this is my php code for outputting the data in td

foreach($explodedeductedfinal as $is){
    $bankname = trim($is);

     if (!empty($bankname)) {
         echo   "<td class='center'>$bankname</td>";
     }else{
         echo   "<td class='center'>'novalue'</td>";
     }
}   

Output:

|Column A|Column B|Column C| 
    A1       A2
    B1       B2      B3

Desired Output:

 |Column A|Column B|Column C| 
      A1      A2    novalue
      B1      B2      B3

i just want to output when the $bankname is empty it will show no value

4
  • 1
    What is the value of $CODE ? Commented Sep 29, 2017 at 17:30
  • the array...... Commented Sep 29, 2017 at 17:35
  • 1
    The problem is your foreach never goes to column c in your first array because there's only two elements. Commented Sep 29, 2017 at 17:38
  • its from my database.. Commented Sep 29, 2017 at 17:39

3 Answers 3

1

The third element is not empty, it doesn't exist, so foreach will only loop two. You might force it to the length you want and supply default values:

$default = array_fill(0, 3, 'novalue');
$explodedeductedfinal = array_replace($default, explode(",", $CODE));

foreach($explodedeductedfinal as $is){
     $bankname = trim($is);
     echo "<td class='center'>$bankname</td>";
}   
Sign up to request clarification or add additional context in comments.

10 Comments

Call to undefined function arrray_fill() i
if you ever need it for an undetermined amount of columns, max(array_map('count', $The_array_of_arrays)); should return you the number of columns you need
i have quick question how about if the array all have no value ?
Then you will get 3 columns of novalue
@AbraCadaver sir how about if theres no array value?
|
0

well the foreach will go through every element of the array.

if (!empty($bankname)) {

Would not be empty since you are in a foreach. If there is always 3 columns, you could look into using a simple for statement, or alternatively, use javascript to fill in all empty cells in a table with a default value. for that, see this fiddle. I have had to use it in the past.

http://jsfiddle.net/yL8t1psf/1/

1 Comment

OK, and is your array 3 columns at all times?
0

Calculate the maximum index of your arrays and loop on that instead of foreach. From a database, you could get the max number of values. Then use isset() to verify if the value is set for that index or not.

I coded this (quick and dirty, no database, no output formatting):

<table>
<?php
    $arA = array(0 => 'A1', 1 => 'A2');
    $arB = array(0 => 'B1', 1 => 'B2', 2 => 'B3');

    function output_ar($ar,$maxindex)
    {
        for ($i = 0; $i <= $maxindex; $i++)
        {
            if (isset($ar[$i]))
            {
                echo "<td>" . $ar[$i] . "</td>";
            }
            else
            {
                echo "<td>novalue</td>";
            }
        }
    }

    $maxindex = max(count($arA),count($arB)) -1;
    echo "<tr>\n";
    output_ar($arA,$maxindex);
    echo "</tr>\n<tr>\n";
    output_ar($arB,$maxindex);
    echo "</tr>\n";
?>
</table>

3 Comments

The code to fill your array from the database is left to you as an exercise :-) . Your initial question does not ask for that.
how ? im newbee in terms of php sorry
PHP: w3schools.com/php. And for MySQL connectivity, w3schools.com/php/php_mysql_connect.asp. StackOverflow is not a school, but w3schools is!

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.