如何对关联数组进行排序并根据值分成两个 div?

发布于 2024-10-27 19:15:58 字数 1084 浏览 1 评论 0原文

我喜欢根据值将关联数组排序为两个 div。

这就是我现在所拥有的:

 function showDivs($array) {
    /*
     * sort en group by
     */
    $luxury = false;
    $no_luxury = false;
    $return = '';
    while (list($key, $value) = each($array)) {

        if ($value["MinimumRate"] < 500) {
            if ($no_luxury === false) {
                $return .= '<div id="no_luxury" >';
            }
            $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";

            if ($no_luxury === false) {
                $return .='</div>';
                $no_luxury = true;
            }
        }

        if ($value["MinimumRate"] > 500) {
            if ($luxury === false) {
                $return .='<div id="luxury" >';
            }
            $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";

            if ($luxury === false) {
                $return .='</div>';
                $luxury = true;
            } 
        } 


    }
    return $return;
}

我的代码无法正常工作。

I like to sort an associative array into two div's based on a value.

Here's what I have now:

 function showDivs($array) {
    /*
     * sort en group by
     */
    $luxury = false;
    $no_luxury = false;
    $return = '';
    while (list($key, $value) = each($array)) {

        if ($value["MinimumRate"] < 500) {
            if ($no_luxury === false) {
                $return .= '<div id="no_luxury" >';
            }
            $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";

            if ($no_luxury === false) {
                $return .='</div>';
                $no_luxury = true;
            }
        }

        if ($value["MinimumRate"] > 500) {
            if ($luxury === false) {
                $return .='<div id="luxury" >';
            }
            $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";

            if ($luxury === false) {
                $return .='</div>';
                $luxury = true;
            } 
        } 


    }
    return $return;
}

My code doesn't work correctly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-11-03 19:15:58

仅在第一个项目上创建和关闭 div。我认为你的意思是在开始时打开它并在最后关闭它。试试这个:

function showDivs($array) {
    /*
     * sort en group by
     */

    $luxury = array();
    $not_luxury = array();
    foreach ($array as $value) {
        if ($value["MinimumRate"] > 500) {
            $luxury[] = $value;
        } else {
            $not_luxury[] = $value;
        }
    }

    $return = '<div id="no_luxury" >';

    foreach ($luxury as $value) {
        $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
    }

    $return .= '</div>';

    $return .= '<div id="luxury" >';

    foreach ($luxury as $value) {
        $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
    }

    $return .= '</div>';

    return $return;
}

The div is being created and closed on the first item only. I think you meant to open it on start and close it in the end. Try this:

function showDivs($array) {
    /*
     * sort en group by
     */

    $luxury = array();
    $not_luxury = array();
    foreach ($array as $value) {
        if ($value["MinimumRate"] > 500) {
            $luxury[] = $value;
        } else {
            $not_luxury[] = $value;
        }
    }

    $return = '<div id="no_luxury" >';

    foreach ($luxury as $value) {
        $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
    }

    $return .= '</div>';

    $return .= '<div id="luxury" >';

    foreach ($luxury as $value) {
        $return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
    }

    $return .= '</div>';

    return $return;
}
独﹏钓一江月 2024-11-03 19:15:58

如果问题是当MinimumRate = 500时,您应该这样做:

if ($value["MinimumRate"] <= 500)    // this is your first 'if' in your code

if ($value["MinimumRate"] >= 500)    // this is the second 'if' in your code

If the problem is when MinimumRate = 500, you whould do:

if ($value["MinimumRate"] <= 500)    // this is your first 'if' in your code

or

if ($value["MinimumRate"] >= 500)    // this is the second 'if' in your code
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文