0

Currently I have this working code that will replace value from old multidimensional array with new value taken from db. Firstly I declare array for 12 month. Code below:

$base = Array(
    Array
        (
            "month" => '2010-01',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),

    Array
        (
            "month" => '2010-02',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),

    Array
        (
            "month" => '2010-03',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ), 

    Array
        (
            "month" => '2010-04',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-05',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-06',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-07',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),


    Array
        (
            "month" => '2010-08',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               

    Array
        (
            "month" => '2010-09',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),               


    Array
        (
            "month" => '2010-10',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),

    Array
        (
            "month" => '2010-11',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),   

    Array
        (
            "month" => '2010-12',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
         )                                         
    );

$result = mysqli_query($con, "SELECT brand, 
DATE_FORMAT( date_time, '%Y' ) AS 'year', 
DATE_FORMAT( date_time, '%m' ) AS 'month', 
SUM(brand='PZH') AS PZH,
SUM(brand='KFC') AS KFC,
SUM(brand='SUB') AS SUB
FROM scan_report WHERE DATE_FORMAT( date_time, '%Y' ) =2010 AND id = ".$_SESSION['userinfo']['id']."
GROUP BY DATE_FORMAT( date_time, '%Y%m' )") or die(mysqli_error($con));
$num_rows = 1;  

while ($row = mysqli_fetch_array($result)) {
foreach($base as &$value) {

    echo $num_rows;     
    if ($value['month'] == $row['year'].'-'.$row['month']){     
        $value['KFC'] = $row['KFC'];
        $value['PZH'] = $row['PZH'];
        $value['SUB'] = $row['SUB'];

     }

}
$num_rows++;
}   

because the multidimensional array, the for each loop for 12 times. During echo there were 60 loop for 5 matched record when query from db. Is there any elegant solution than what I'm currently doing? Any idea to reduce loop?

3 Answers 3

1

Your best shot would be to take the month value and use it as the key in your array:

$base = Array(
    '2010-01' => Array
        (
            "month" => '2010-01',
            "KFC" => '0',
            "PZH" => '0',
            "SUB" => '0'
        ),
     // ... and so on
    );

Then you can search as such:

while ($row = mysqli_fetch_array($result)) {
    $searchKey = $row['year'].'-'.$row['month'];
    if (isset($base[$searchKey])) {
        $base[$searchKey]['KFC'] = $row['KFC'];
        $base[$searchKey]['PZH'] = $row['PZH'];
        $base[$searchKey]['SUB'] = $row['SUB'];
    } else {
        $base[$searchKey] = array(
            'KFC' => $row['KFC'],
            'PZH' => $row['PZH'],
            'SUB' => $row['SUB'],
        );
    }
    $num_rows++;
}   
Sign up to request clarification or add additional context in comments.

Comments

0

I would not predefine the array values and would prefer to use year-month as key (it's unique because it's grouped by that).

$base = array();
while($row = mysqli_fetch_array($result)) {
    echo $num_rows;
    $key = $row['year'].'-'.$row['month'];
    $base[$key] = array();
    $base[$key]['KFC'] = $row['KFC'];
    $base[$key]['PZH'] = $row['PZH'];
    $base[$key]['SUB'] = $row['SUB'];
    $num_rows++;
}

Comments

0

I like to use associate multidimensional arrays in cases like this. For example, you could arrange your base array as follows:

$base = array('2010-01' => array(
        "KFC" => '0',
        "PZH" => '0',
        "SUB" => '0'
    ),
    '2010-01' => array(
        "month" => '',
        "KFC" => '0',
        "PZH" => '0',
        "SUB" => '0'
    ) //etc
    );

Then, when your processing your results from the database you can access a specific section of the $base array like:

while ($row = mysqli_fetch_array($result)) {

$base[$row['year'] . '-' . $row['month']]['KFC'] = $row['KFC'];
$base[$row['year'] . '-' . $row['month']]['PZH'] = $row['pZH'];
$base[$row['year'] . '-' . $row['month']]['SUB'] = $row['SUB'];

}

Then you only have to loop through the database rows to achieve the updating process. Make sense?

1 Comment

Sorry, other people beat me to it apparently.

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.