php数组未按预期排序

时间:2018-04-23 07:06:13

标签: php arrays

我有两个复选框列表。我正在检查选中了哪个复选框,并根据我正在尝试获取数组。

让我们说两个列表的大小是检查了小,中,大盒子,另一个是检查了红色,绿色,蓝色框的颜色。该数组应该类似于:

array[['small', 'medium', 'large']['red', 'green', 'blue']]

但我得到了这个:

array[["small"],["medium"],["large"]] [["red"],["green"],["blue"]] 

这是代码:

$counter = 0;
$attributes_list = [];
foreach($features_cuts_list as $k => $features_cut) {
    $inner_counter = 0;
    if ($features_cut["selectedid"] != "") {
        $attributes_list[$counter] = [];
        $title_to_get = $features_cut["features_cuts_id"];

        /* Gets the name of the box that is checked */
        $query = "SELECT title FROM features_cuts_translations WHERE lang = '$lang' AND features_cuts_id = '$title_to_get' LIMIT 1;";

        $result = mysql_query($query) or die("Cannot query");
        $attribute_name = mysql_fetch_row($result);
        foreach ($attribute_name as $q) {
            array_push($attributes_list[$counter], $q);
        }
        $counter++;
    } else {

    }
}

编辑:

这是$features_cuts_list的减速过程:

    function getListValuesSql($sql){
            global $link;         //Database connection
            $data=array();
            $subData{0}=array();

            $res=mysql_query($sql,$link);

            if(mysql_num_rows($res)>0){
                $i=0;
                while($row=mysql_fetch_array($res)){
                for($j=0;$j<mysql_num_fields($res);$j++){

                        $field=mysql_field_name($res, $j);
                        $subData{$i}[$field]=$row[$field];
                    }
                    $data[$i]=$subData{$i};
                    $i++;
                }
                    return $data;
            }else{
                return 0;
            }
        } 
$feature_id = $feature["features_id"];
$features_cuts_list = $data->getListValuesSql("
    SELECT DISTINCT fct.*, fc.sort, fc.inner_id, fc.price,
    fcp.features_cuts_id AS selectedid, IFNULL(fcpv.price,fc.price)
    AS price, fcpv.sku
    FROM `features_cuts` as fc
    JOIN `features_cuts_translations` as fct ON fct.features_cuts_id=fc.id
    LEFT JOIN `features_cuts_product_values` as fcpv ON fc.id=fcpv.features_cuts_id AND fcpv.product_id='$pageid'
    LEFT JOIN `features_cuts_products` as fcp ON fc.id=fcp.features_cuts_id AND fcp.product_id='$pageid'
    WHERE fc.features_id='$feature_id' AND fct.lang='$lang'
    Order by fc.sort
");

希望这有用

1 个答案:

答案 0 :(得分:1)

从阅读代码开始,我认为你已经不必要地提供了$ counter变量。试试这个修改过的代码:

$attributes_list = [];
foreach($features_cuts_list as $k => $features_cut) {
$inner_counter = 0;
if ($features_cut["selectedid"] != "") {
    $title_to_get = $features_cut["features_cuts_id"];

    /* Gets the name of the box that is checked */
    $query = "SELECT title FROM features_cuts_translations WHERE lang = '$lang' AND features_cuts_id = '$title_to_get' LIMIT 1;";

    $result = mysql_query($query) or die("Cannot query");
    $attribute_name = mysql_fetch_row($result);
    foreach ($attribute_name as $q) {
        array_push($attributes_list, $q);
    }
    $counter++;
} else {

}

}

如果不完全,它将主要解决您的问题。 运行这段代码后,让我知道结果是什么。