p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: process array function seems to return too much


Message #1 by spam@k... on Tue, 24 Sep 2002 01:48:49
Here's a function I use on database returns, usually to strip slashes. It 
seems to be doubling the number of entries. For instance, I just got out 
one row of a database, with 5 columns. Then I ran it through this. Then I 
did print_r on the result. I was left with a row with 10 columns, and the 
last 5 columns were blank. Anybody see where I've gone wrong? 



function processArray($dbArray, $function, $takeOutApostrophes="no"){
	$processedArray = array();

	for ($row = 0; $row < count($dbArray); $row++) {
		for ($element = 0; $element < count($dbArray[$row]); 
$element++) {
			$processedArray[$row][$element] = $function
($dbArray[$row][$element]);
			if ($takeOutApostrophes == "yes") {
				$processedArray[$row][$element] = 
str_replace("'", "`", $processedArray[$row][$element]);
			}
		}
	}

	return $processedArray; 
}
Message #2 by spam@k... on Tue, 24 Sep 2002 03:05:12
Okay, apparently the function is stumbling on the fact that I'm using 
mysql_fetch_array, which returns both a number and word for the key of the 
array. 

Below you can see what I start with and what I end up with:


Array
(
    [0] => Array
        (
            [0] => 2
            [quoteOnWhichCatPage] => 2
            [1] => 
            [quoteHeadline] => 
            [2] => <b>Mission:</b><br>Alternatives To Paving seeks to 
build community and ecological health by promoting pedestrian-oriented 
development. We engage the public in envisioning and enacting alternatives 
to an auto-dependant future.
            [quoteMainContent] => <b>Mission:</b><br>Alternatives To 
Paving seeks to build community and ecological health by promoting 
pedestrian-oriented development. We engage the public in envisioning and 
enacting alternatives to an auto-dependant future.
            [3] => 0
            [quoteArticleId] => 0
            [4] => 0
            [quoteCatPageLinkedTo] => 0
            [5] => 
            [quoteOtherUrl] => 
            [6] => 0
            [quoteOOA] => 0
            [7] => blank
            [quoteWhichType] => blank
            [8] => atpMissionStatement
            [quoteStyle] => atpMissionStatement
        )

)
<hr>Array
(
    [0] => Array
        (
            [0] => 2
            [1] => 
            [2] => <b>Mission:</b><br>Alternatives To Paving seeks to 
build community and ecological health by promoting pedestrian-oriented 
development. We engage the public in envisioning and enacting alternatives 
to an auto-dependant future.
            [3] => 0
            [4] => 0
            [5] => 
            [6] => 0
            [7] => blank
            [8] => atpMissionStatement
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
        )

)









Message #3 by "Nikolai Devereaux" <yomama@u...> on Tue, 24 Sep 2002 10:15:02 -0700
The problem is that you're iterating through numerical indexes in the array,
not string indexes.

Your for loop uses ($i = 0; $i < count($array); ++$i), which means if there's 5
string indexes ("quoteOnWhich...", "quoteHeadline", etc..) and 5 integer
indexes, you'll iterate the loop 10 times.

However, indexes 5, 6, 7, 8, and 9 don't exist in your array, but you attempt
to access them anyway.

(on a side note, if your error_reporting was set to E_ALL, you'd see probably a
notice level warning that you're accessing an undefined array index)

Since you're dealing with a string-indexed array, you should really use a
foreach() loop for your modifications:

foreach($dbArray as $rowIndex => $rowData)
{
   foreach($rowData as $colName => $colValue)
   {
      $dbArray[$rowIndex][$colName] = $function($colValue);
   }
}


In fact, I'd go so far as to say that _whenever_ you loop through an array, you
should use foreach() instead of for().

Take care,

Nik

Message #4 by spam@k... on Tue, 24 Sep 2002 15:31:25 -0500
That's fantastic to know, though it seems like, from where I'm at now, I can not do this easily. The
function, as you've redone it, returns a string index which can no longer be dealt with numerically. As I've perhaps over a hundred
calls to functions that expect a numeric index, it would take some serious effort to rewrite the whole thing. I mean, I would have
to rewrite everything or rewrite nothing. I can't rewrite the function to return a string index, if the function calling it is
expecting to be handed a numeric index back. But perhaps in the future, when I've time to rewrite the whole thing from scratch, I'll
make the changes you suggest. 



------------------------------------------------
On Tue, 24 Sep 2002 10:15:02 -0700, "Nikolai Devereaux" <yomama@u...> wrote:

> 
> The problem is that you're iterating through numerical indexes in the array,
> not string indexes.
> 
> Your for loop uses ($i = 0; $i < count($array); ++$i), which means if there's 5
> string indexes ("quoteOnWhich...", "quoteHeadline", etc..) and 5 integer
> indexes, you'll iterate the loop 10 times.
> 
> However, indexes 5, 6, 7, 8, and 9 don't exist in your array, but you attempt
> to access them anyway.
> 
> (on a side note, if your error_reporting was set to E_ALL, you'd see probably a
> notice level warning that you're accessing an undefined array index)
> 
> Since you're dealing with a string-indexed array, you should really use a
> foreach() loop for your modifications:
> 
> foreach($dbArray as $rowIndex => $rowData)
> {
>    foreach($rowData as $colName => $colValue)
>    {
>       $dbArray[$rowIndex][$colName] = $function($colValue);
>    }
> }
> 
> 
> In fact, I'd go so far as to say that _whenever_ you loop through an array, you
> should use foreach() instead of for().
> 
> Take care,
> 
> Nik
> 
> 
> 
Message #5 by "Nikolai Devereaux" <yomama@u...> on Tue, 24 Sep 2002 13:45:30 -0700
The key thing to understand is that I'm not returning any indexes that were not
passed in.  That's the key difference.

If you want your array to ONLY contain numerical indexes, then create your
array with numerical indexes only.  It doesn't make sense to create an array
with string indexes if you only call functions that use the integer indexes.

You should also realize that my version of your function doesn't alter the
indexes, either -- if you have all these functions which expect index 0 to be
the quoteHeadline, for example, it will *still* be quoteHeadline after the
function.

Foreach() doesn't care what the data types of array indexes are -- it will
iterate over numbers as well as strings, which is why it's so powerful a tool
for looping through array items.


nik


  Return to Index