0
    $sql = "SELECT catgy.category_id, catgy.category_title 
            FROM categories catgy
              INNER JOIN subj_category_relation scr
                ON scr.scr_id = catgy.category_id  
              INNER JOIN subjects subj
                ON subj.subject_id = '{$sId}'
            WHERE subj.subject_id = '{$sId}'
                AND subj.subject_id = scr.subject_id";

    $res = $this->db->query( $sql );

    if ($res) { 



        $results = $res->result_array();

        echo "<pre>";
        var_dump( $results );
        echo "</pre>"
        exit;       

vardump

array(5) {
  [0]=>
  array(2) {
    ["category_id"]=>
    string(1) "1"
    ["category_title"]=>
    string(5) "terms"
  }
  [1]=>
  array(2) {
    ["category_id"]=>
    string(1) "2"
    ["category_title"]=>
    string(6) "people"
  }
  [2]=>
  array(2) {
    ["category_id"]=>
    string(1) "3"
    ["category_title"]=>
    string(8) "places
"
  }
  [3]=>
  array(2) {
    ["category_id"]=>
    string(1) "4"
    ["category_title"]=>
    string(7) "works
"
  }
  [4]=>
  array(2) {
    ["category_id"]=>
    string(1) "5"
    ["category_title"]=>
    string(8) "events
"
  }
}

Getting Database int type in string. What is the proper solution? Instead converting each array may be not good solution.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alizain Prasla
  • 744
  • 2
  • 15
  • 37

3 Answers3

0

Database object should return correct value type in object. there is issue in PHP/MySQL older versions.

https://dev.mysql.com/downloads/connector/php-mysqlnd/

Install php5-mysqlnd driver

apt-get install php5-mysqlnd
service apache2 restart

Reference: MySQL integer field is returned as string in PHP

Check @advait answer

Alizain Prasla
  • 744
  • 2
  • 15
  • 37
-1

Try array_walk to convert string to int:

array_walk($results, function(&$a) {
    $a['category_id'] = (int)$a['category_id'];
});

var_dump($results);

DEMO

When retrieving even integers from DB it is converted to string so you have to manually convert them back to int

nanobash
  • 5,419
  • 7
  • 38
  • 56
  • I have list of function like these.. Do i use this solution each time? – Alizain Prasla Mar 27 '14 at 15:25
  • @AlizainPrasla I can't guess your comment, though like you see you've to pass `array` into function and it will return `int` instead of `string`. Use function based on your needs – nanobash Mar 27 '14 at 15:27
  • I think iteration is not right way. Its should return INT from database because Primary keys are in Int value. – Alizain Prasla May 13 '16 at 08:19
-1
$new_int_array = array();
foreach($results as $value){
    $new_int_array [] = array_map('intval', $value);
}
print_r($new_int_array); //this is new array with int type values.
Muhammad Irfan
  • 228
  • 1
  • 8