1

This post was very useful: MySQL integer field is returned as string in PHP

In my model, I have a result_array() where I want one of the six elements in each 'row' (bookID) to be an INT when it goes back to the controller. .

$bookList = $this->db->query('SELECT bookID....etc')->result_array();

My var_dump produces this..

1 => 
array (size=6)
  'ownerID' => string '6' (length=1)
  'bookID' => string '53' (length=2)
  'address' => string '123 main road to wherever' (length=25)
  'Company' => string 'xxxxxxxxxx xxxxxxxx' (length=18)
  'FullName' => string 'John Smith' (length=10)
2 => ...etc...
3 => ...etc...
4 => ...etc...



1 => 
array (size=6)
  'ownerID' => string '6' (length=1)
  'bookID' => int 53
  'address' => string '123 main road to wherever' (length=25)
  'Company' => string 'xxxxxxxxxx xxxxxxxx' (length=18)
  'FullName' => string 'John Smith' (length=10)
2 => ...etc...
3 => ...etc...
4 => ...etc...

What is the most efficient way of iterating through the result array, which could potentially have 1,000 rows, changing the second element from a string to an INT before returning the results to the calling function?

return $bookList;

Community
  • 1
  • 1
Maxcot
  • 1,513
  • 3
  • 23
  • 51

2 Answers2

0

It cames to my mind that you can use the array_map() native function. It applies a function that you define to each element of the array and returns the final array after that is applied.

I made an example, since I love this function:

<pre>
<?

$results = array(

    array(
        "val" => "1",
        "name" => "maria"
    ),
    array(
        "val" => "10",
        "name" => "jorge"
    )

);


function castToInt($array) {
    $array['val'] = (int) $array['val'];
    return $array;
}

var_dump(array_map('castToInt', $results)); 

?>
</pre>

That var_dump(...) you give you this output:

array(2) {
  [0]=>
  array(2) {
    ["val"]=>
    int(1)
    ["name"]=>
    string(5) "maria"
  }
  [1]=>
  array(2) {
    ["val"]=>
    int(10)
    ["name"]=>
    string(5) "jorge"
  }
}
D. Melo
  • 2,139
  • 2
  • 14
  • 19
0

The question to be asked is: How can result_array() (or a wrapper of this) know, which column is of which type / to be cast to which type?

In any case, you need to iterate over all returned rows (simply because you want to change each row). You could then do a check like this

foreach ($rows as &$row) {
    foreach ($row as &$column) {
        if ( (int) $column == $column )
            $column = (int) $column;
    }
}

If you are irritated by the &, I recommend the official manual (search for "reference"). However, this method will take quite some time - just because it will iterate over each column in each row and try to find out if casting to integer changes something (simple equality shouldn't be the real check but serves as a snippet here).

To take away some time, you need to define which column should be cast to which type. I suggest creating some structure for this, such as an array - if you want to modify this later, a hardcoded solution will give you additional sorrows. For example like this

$ints = array('ownerID', 'bookID');
foreach ($rows as &$row) {
    foreach ($ints as $intKey) {
        $row[ $intKey ] = (int) $row[ $intKey ];
    }
}

This can be speed up now with a native function, such as array_map() as shown in D. Melo's answer.

Community
  • 1
  • 1
kero
  • 10,647
  • 5
  • 41
  • 51