Array problems in PHP 4...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Array problems in PHP 4...

Post by Wolf_22 »

The following code works fine in my dev station running PHP 5, but I just uploaded it to a server with PHP 4 and it broke. I believe it has something to do with the object in use:

Code: Select all

function array_combine($arr1, $arr2) {
    $out = array();
    $arr1 = array_values($arr1);
    $arr2 = array_values($arr2);
    foreach($arr1 as $key1 => $value1) {
        $out[(string)$value1] = $arr2[$key1];
    }  
    return $out;
}
 
function get_vtl_user_roles(){
    global $vtl_roles,$vtldb;
    $this_role = "'[[:<:]]administrator[[:>:]]'";
    $query = "SELECT DISTINCT id, user_login FROM $vtldb->users WHERE ID = ANY (SELECT DISTINCT user_id FROM $vtldb->usermeta WHERE meta_value RLIKE 'administrator') ORDER BY ID ASC LIMIT 10000";
    $result = $vtldb->get_results($query);
    foreach ($result as $id=>$user) {
        $partA[] = $user->user_login;
        $partB[] = $user->id;
    } 
    $users_of_this_role = array_unique(array_combine($partA,$partB));
    return $users_of_this_role;
}

I think where this thing bombs out is where $partA[] = $user->user_login... Is there an easy way around this?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array problems in PHP 4...

Post by aceconcepts »

Do you get an error? If so what is it?
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

Warning: array_values() [function.array-values]: The argument should be an array in /home/tutlib/public_html/vtl_content/plugins/to-do-list/to-do-list.php on line 31

Warning: array_values() [function.array-values]: The argument should be an array in /home/tutlib/public_html/vtl_content/plugins/to-do-list/to-do-list.php on line 32

Warning: Invalid argument supplied for foreach() in /home/tutlib/public_html/vtl_content/plugins/to-do-list/to-do-list.php on line 33
(Line 31 is "$arr1 = array_values($arr1);")
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Array problems in PHP 4...

Post by AlanG »

Could you post up the code for the get_results() function in the $vtldb object? Or better yet, the entire file?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array problems in PHP 4...

Post by aceconcepts »

Why do you use array_values()?
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

aceconcepts wrote:Why do you use array_values()?
The function that resides in was something I got from the PHP manual from one of the comments that discussed issues with array_combine in PHP 4. The poster of that function mentioned that it fixed that and when I tried it on the server, it seemed to work as the array_combine issue was the first problem I ran into prior to what I'm running into now. Should I use something else?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array problems in PHP 4...

Post by aceconcepts »

If you use arr_combine() then I guess it depends on how you've implemented it.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: Array problems in PHP 4...

Post by redmonkey »

Have you checked and confirmed that '$vtldb->get_results($query);' actually does return a result array? Does the database on the server support sub queries?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array problems in PHP 4...

Post by aceconcepts »

I ask about array_cmbine() because it was introduced for PHP 5.

You can use it in PHP 4 but with a work-around.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

I found this function and so far, it fixed the problem:

Code: Select all

function arr_combine($arr1,$arr2){
                if(!is_array($arr1))
                        $arr1 = array();
                if(!is_array($arr2))
                        $arr2 = array();
                $keys1 = array_keys($arr1);
                $keys2 = array_keys($arr2);
                $keys  = array_merge($keys1,$keys2);
                $vals1 = array_values($arr1);
                $vals2 = array_values($arr2);
                $vals  = array_merge($vals1,$vals2);
                $ret    = array();
 
                foreach($keys as $key)
                {
                        list($unused,$val) = each($vals);
                        $ret[$key] = $val;
                }
 
            return $ret;
        }
Credit goes to someone who posted over on the array_merge() manual comments section.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array problems in PHP 4...

Post by aceconcepts »

The foreach is the main reason for this work around.

Nice one :D
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

redmonkey wrote:Have you checked and confirmed that '$vtldb->get_results($query);' actually does return a result array? Does the database on the server support sub queries?
It's funny you mention this because after applying the workaround, I noticed that I'm having problems getting things to and from the database. Is this going to require a consult with the systems admin or is there yet again another workaround for this too?
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

I changed the query to the following:

Code: Select all

SELECT id, user_login FROM $vtldb->users INNER JOIN $vtldb->usermeta ON $vtldb->users.id = $vtldb->usermeta.user_id WHERE $vtldb->usermeta.meta_value = 'administrator' ORDER BY id ASC LIMIT 10000
And I'm still not getting anything from the MySQL database. Have I done something wrong here?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Array problems in PHP 4...

Post by aceconcepts »

Have you looked at mysql_error()?
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Array problems in PHP 4...

Post by Wolf_22 »

mysql_error() doesn't appear to be throwing any errors and when I use the MySQL Console to type out that query (to test it), it comes back saying that it's returned an empty set. This leads me to believe that this new query is not picking up the tables / columns that I am trying to get it to retrieve (unless there's some sort of compatibility issue at hand).

AlanG: Here is the function definition of get_results() -

Code: Select all

 
    function get_results($query = null, $output = OBJECT) {
        $this->func_call = "\$db->get_results(\"$query\", $output)";
 
        if ( $query )
            $this->query($query);
        else
            return null;
 
        if ( $output == OBJECT ) {
            // Return an integer-keyed array of row objects
            return $this->last_result;
        } elseif ( $output == OBJECT_K ) {
            // Return an array of row objects with keys from column 1
            // (Duplicates are discarded)
            foreach ( $this->last_result as $row ) {
                $key = array_shift( get_object_vars( $row ) );
                if ( !isset( $new_array[ $key ] ) )
                    $new_array[ $key ] = $row;
            }
            return $new_array;
        } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
            // Return an integer-keyed array of...
            if ( $this->last_result ) {
                $i = 0;
                foreach( (array) $this->last_result as $row ) {
                    if ( $output == ARRAY_N ) {
                        // ...integer-keyed row arrays
                        $new_array[$i] = array_values( get_object_vars( $row ) );
                    } else {
                        // ...column name-keyed row arrays
                        $new_array[$i] = get_object_vars( $row );
                    }
                    ++$i;
                }
                return $new_array;
            }
        }
    }
Here is the code to the query function, too:

Code: Select all

    function query($query) {
        if ( ! $this->ready )
            return false;
 
        // filter the query, if filters are available
        // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method
        if ( function_exists('apply_filters') )
            $query = apply_filters('query', $query);
 
        // initialise return
        $return_val = 0;
        $this->flush();
 
        // Log how the function was called
        $this->func_call = "\$db->query(\"$query\")";
 
        // Keep track of the last query for debug..
        $this->last_query = $query;
 
        // Perform the query via std mysql_query function..
        if ( defined('SAVEQUERIES') && SAVEQUERIES )
            $this->timer_start();
 
        $this->result = @mysql_query($query, $this->dbh);
        ++$this->num_queries;
 
        if ( defined('SAVEQUERIES') && SAVEQUERIES )
            $this->queries[] = array( $query, $this->timer_stop(), $this->get_caller() );
 
        // If there is an error then take note of it..
        if ( $this->last_error = mysql_error($this->dbh) ) {
            $this->print_error();
            return false;
        }
 
        if ( preg_match("/^\\s*(insert|delete|update|replace|alter) /i",$query) ) {
            $this->rows_affected = mysql_affected_rows($this->dbh);
            // Take note of the insert_id
            if ( preg_match("/^\\s*(insert|replace) /i",$query) ) {
                $this->insert_id = mysql_insert_id($this->dbh);
            }
            // Return number of rows affected
            $return_val = $this->rows_affected;
        } else {
            $i = 0;
            while ($i < @mysql_num_fields($this->result)) {
                $this->col_info[$i] = @mysql_fetch_field($this->result);
                $i++;
            }
            $num_rows = 0;
            while ( $row = @mysql_fetch_object($this->result) ) {
                $this->last_result[$num_rows] = $row;
                $num_rows++;
            }
 
            @mysql_free_result($this->result);
 
            // Log number of rows the query returned
            $this->num_rows = $num_rows;
 
            // Return number of rows selected
            $return_val = $this->num_rows;
        }
 
        return $return_val;
    }
Post Reply