Search code examples
phparraysloopsmultidimensional-array

array_push() inside of nested loops is not populating the correct array structure


I have two lists of check boxes. I am checking which check box is selected and based on that I am trying to get an array.

Let's say the two lists are size which have small, medium, large boxes checked and the other one is color which have red, green, blue boxes checked. The array should look something like:

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

But I am getting this:

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

This is the code:

$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 {

    }
}

EDIT:

This is the deceleration process for $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
");

Solution

  • From reading your code I think you have unnecessarily provided the $counter variable.Try this modified code:

    $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 {
    
    }
    

    }

    If not completely, it will mostly solve your issue. Let me know what the result are , after running this piece of code.