0

Hello everyone i have some question about query on PHP using MySQL database.

I have table name test2 like this

testid  asd
1       A
2       B
3       C
4       D

and then i have query like this

public function m_get_wer() {
    $sql = "SELECT `asd` FROM test2";
    $data = $this->db->query($sql)->result_array();
    return $data;
}

when i used print_r on that query, the result like this

Array ( [0] => Array ( [asd] => A ) [1] => Array ( [asd] => B ) [2] => Array ( [asd] => C ) [3] => Array ( [asd] => D ) )

but the result that i need is like this

Array ( [0] => A [1] => B [2] => C [3] => D )

How i can get result like that. thanks.

4
  • 1
    Please read this: stackoverflow.com/questions/14896633/… Commented Apr 24, 2018 at 18:23
  • Possible duplicate of Codeigniter when does result_array() return a single or multi dimensional array? Commented Apr 24, 2018 at 18:25
  • @Maykonn so whats the solution? is it possible i get result like i want? Array ( [0] => A [1] => B [2] => C [3] => D ) Commented Apr 24, 2018 at 18:40
  • Is not possible with codeigniter functions, I guess. You need a loop like @pradeep answered. Codeigniter provide to you an array like it because is expected that you will use the array values in this way for example: echo $result[0]["asd"]. I recommend you to write a new layer between your data access layer and your business code to translate the responses of queries in a way that make sense for your business code. Commented Apr 25, 2018 at 20:51

1 Answer 1

0

Hope this will help you :

public function m_get_wer()
    {   
        $sql = "SELECT asd FROM test2";
        $data = $this->db->query($sql)->result_array();

        if ($data)
        {
            foreach ($data as $key => $item) 
            {
                $result[$key] = $item['asd'];
            }
        }
        //print_r($result);die;
        return $result;
    }

Output will be :

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
)
Sign up to request clarification or add additional context in comments.

8 Comments

wow thank you so much. its work. but can you tell me how thats work? i just want to know. Thanks
$data is the array of array to get the item of inner array a foreach loop is needed to loop through
what if i want to select 2 column like testid and asd. the result like this Array ( [0] => Array ( [0] => 1 [1] => A ) [1] => Array ( [0] => 2 [1] => B ) [2] => Array ( [0] => 3 [1] => C ) [3] => Array ( [0] => 4 [1] => D ) )
Like this? if ($data) { foreach ($data as $key => $item) { $result[$key] = $item['testid']; $result[$key] = $item['asd']; } } i already try that, but result just same like before, return Array ( [0] => A [1] => B [2] => C [3] => D ) sorry im not expert at coding
if u want both testid and asd then change sql as select * from test2 or select testid, asd from test2 and u will get the desired result
|

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.