Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.2.0dev1

Bug #9914 Incorrect array index assumption
Submitted: 2007-01-22 15:12 UTC
From: rjbarbour at gmail dot com Assigned: wiesemann
Status: Closed Package: Structures_DataGrid_DataSource_Array (version 0.1.1)
PHP Version: 5.0.5 OS: XP SP2
Roadmaps: (Not assigned)    

 [2007-01-22 15:12 UTC] rjbarbour at gmail dot com (Rob)
Description: ------------ The array $_ar may have integer indexes. The script assume, on line 120 of Array.php, that the first element of the array will be at $_ar[0]. This can be an incorrect assumption. See example below. Test script: --------------- $my_array = array( 5 => 'five', 3 => 'three' ); $datagrid->bind($my_array); //$this->_ar[0] on line 120 of Array.php will evaluate to NULL Expected result: ---------------- No warning. Actual result: -------------- Warning: array_keys() [function.array-keys]: The first argument should be an array in C:\<my_path>\PEAR\pear\Structures\DataGrid\DataSource\Array.php on line 120


 [2007-01-22 15:13 UTC] rjbarbour at gmail dot com
changed my email address
 [2007-01-23 17:47 UTC] wiesemann (Mark Wiesemann)
This looks like a wrong assumption about the structure of the array passed to bind(). bind() expects an array with entries for each row, and each of these entries needs to be an array of entries for each column. Assuming that you want to have to columns, named 5 and 3, with one row, and the values 'five' and 'three', you need to call bind() like this: $datagrid->bind(array(array(5 => 'five', 3 => 'three')); There is no need to specify a key for the top-level (= row) array. And without such keys, access to key 0 is valid.
 [2007-01-23 22:23 UTC] rjbarbour at gmail dot com
Access to key 0 may be invalid if the first element of the shiny new array is unset: eg: $my_array = array( array( 'name' => 'Alice', 'address' => 'foo' ), array( 'name' => 'Bob', 'address' => 'bar') array( 'name' => 'Charlie', 'address' => 'quxx') ); unset($my_array['0']); /* Array now looks like this: array( 1 => array( 'name' => 'Bob', 'address' => 'bar') 2 => array( 'name' => 'Charlie', 'address' => 'quxx') ) */ $datagrid->bind($my_array); If $my_array is re-indexed with array_merge() then Array.php will not complain. I don't think it should be necessary to do this.
 [2007-01-24 11:07 UTC] wiesemann (Mark Wiesemann)
Okay, although the non-existing key 0 might not happen really often, it makes sense to remove this assumption. I'll take care of fixing this.
 [2007-01-24 19:57 UTC] wiesemann (Mark Wiesemann)
Thank you for your bug report. This issue has been fixed in the latest released version of the package, which you can download at