1

Two array : Fee and Amount, have been formed as below based on MYSQL JOIN Clause and Table structure.

Array Fee

Array
(
    [0] => Annual Fee-Disability Scholarship
    [1] => Annual Fee-First rank Scholarship
    [2] => Monthly Fee-First rank Scholarship
)

Array Amount

Array
(
    [0] => 1000-20
    [1] => 1000-10
    [2] => 560-5
)

Array Fee contains the category of fee And assigned scholarship to each category

Array Amount contains the fee rate And the assigned scholarship percentage in each fee category.

I need to remove fee category and it's amount if this is already echoed.

Normally, using following PHP code:

$cat = array('Annual Fee-Disability Scholarship','Annual Fee-First rank Scholarship','Monthly Fee-First rank Scholarship');
$amount = array('1000-20','1000-10','560-5');
foreach (array_combine($cat, $amount) as $cat => $amount){
    $cat_ry = explode('-', $cat);
    $amount_ry = explode('-', $amount);

    $fee_cat = $cat_ry[0]; 
    $fee = $amount_ry[0];
    $sch_cat = $cat_ry[1];
    $sch = $amount_ry[1];

    echo "
        <tr>
            <td>$fee_cat</td>
            <td>$fee</td>
        </tr>
        <tr>
            <td>$sch_cat</td>
            <td>$sch</td>
        </tr>";
}

following table format is echoed.

PHP Fiddle - complete code and result

enter image description here

Here Annual Fee category is being duplicated, cause two scholarships has been associated with this fee category.

So my requirement is to remove duplicate fee category and its related amount. Required Output

enter image description here

So I try: not echo fee category if it is already echoed

$fee_cat = "";
//code
//...
if($cat_ry[0] != $fee_cat){
    $fee_cat = $cat_ry[0];
    //code
    //...
}

but here, with duplicated fee category another scholarship First Rank Scholarship is also removed

PHP Fiddle - complete code and result

enter image description here

1
  • You rely a lot on the order of items in the arrays. Your (updated) if statement will break if the order is Annual, monthly, annual. It's fine for now, but just giving you some food for thought :) Same issue of course for using array_combine, but I don't know the rest of the logic. Commented Oct 21, 2018 at 15:26

1 Answer 1

2

You can get expected result by separating echo output.
PHP Fiddle: https://wtools.io/php-sandbox/pP

$cat = array('Annual Fee-Disability Scholarship','Annual Fee-First rank Scholarship','Monthly Fee-First rank Scholarship');
$amount = array('1000-20','1000-10','560-5');

$outputed_fee_cat = [];
foreach (array_combine($cat, $amount) as $cat => $amount){
    $cat_ry = explode('-', $cat);
    $amount_ry = explode('-', $amount);

    $fee = $amount_ry[0];
    $sch_cat = $cat_ry[1];
    $sch = $amount_ry[1];
    $fee_cat = $cat_ry[0];
    if (!in_array($fee_cat, $outputed_fee_cat)) {
        $outputed_fee_cat[] = $fee_cat;

        echo "
        <tr>
            <td class='bold'>$fee_cat</td>
            <td>$fee</td>
        </tr>";
    }
        echo "
        <tr>
            <td>$sch_cat</td>
            <td>$sch</td>
        </tr>";
}
Sign up to request clarification or add additional context in comments.

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.