0

I created a function that should in theory create an array from another array (basically what i want to do its bring it down 1 level).

My problem is that somehow the code creates another array inside the new array when it shouldn't.... and even more the output is rather strange. The final array should all be in the same level but somehow it creates it multidimensional and with extra values.

The problem seems to be in the loop, but i can't see where

any help is appreciated.

    public function pullCompetencyByCID ( $cID ) {
                    $sql = 'SELECT * FROM ldc_competency_'.$this->lang.' WHERE cID='.$cID;
                    $result = $this->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);                   
                         foreach ( $result as $key2 => $val2 ){
                                switch ($key2){
                                    case 'cName':       $cName = $val2;         
                                                        break;
                                    case 'cID':         $cID = $val2;
                                                        break;
                                    case 'cLevel':      $cLevel = $val2;
                                                        break;
                                    case 'cDetails':    $cDetails = $val2;
                                                        break;
                                    case 'cSummary':    $cSummary = $val2;
                                                        break;
                                }
                        }
                    $pullCompetency = array('cName'=>$cName,'cID'=>$cID, 'cLevel'=>$cLevel, 'cDetails'=>$cDetails, 'cSummary'=>$cSummary);          

                    return $pullCompetency;
    }

The output of that code is

 Array ( 
    [cName] => Array ( 
                        [cID] => 2 
                        [cName] => dos
                        [cLevel] => 2
                        [cDetails] => doss
                        [cSummary] => dosssss 
                         ) 
    [cID] => 2
    [cLevel] => 
    [cDetails] => 
    [cSummary] => 
)

The output of just the $result is

Array ( 
    [0] => Array ( 
                    cID] => 2 
                    [cName] => dos 
                    [cLevel] => 2 
                    [cDetails] => doss 
                    [cSummary] => dosssss
                     )
     )

to avoid confusion the desired result should be

 Array ( 
        [cID] => 2 
        [cName] => dos 
        [cLevel] => 2 
        [cDetails] => doss 
        [cSummary] => dosssss
         )
2
  • Am I missing something, or could you replace that whole loopy thingy with return $result? What is that loop supposed to do?! Commented Nov 15, 2012 at 15:28
  • You would save yourself some trouble by selecting exactly what you want SELECT cName, cID, cLevel, cDetails, cSummary ... Commented Nov 15, 2012 at 15:45

2 Answers 2

2

Try this function instead:

public function pullCompetencyByCID ( $cID ) {
    $sql = 'SELECT * FROM ldc_competency_'.$this->lang.' WHERE cID='.$cID;
    return $this->db->query($sql)->fetch(PDO::FETCH_ASSOC);                   
}

Your function seems to be doing a lot of redundant work with the array that is returned from:

PDOStatement::fetchAll(PDO::FETCH_ASSOC);

Also: If no Competency is found in your current function, PHP will spit out/log error notices because variables such as $cName won't be initialized.

Sign up to request clarification or add additional context in comments.

Comments

1

Since you're doing ->fetchAll(), $result is going to be a 2-D array of results:

foreach ( $result as $key2 => $val2 ){
                      ^-- row number
                               ^--- array row contents

you then stuff that sub-array into your new array, causing your sub-arrays.

1 Comment

Although this answer did not provide the solution it helped me to think why could i be getting this and how to solve it. The solution is just to use ->fetch().

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.