...

View Full Version : Array not working



gilgalbiblewhee
07-30-2008, 06:54 PM
I don't think this is the right way of writing the array. It's not working:

$newSearchTheseArr = array(
for ($j=0; $j < count($newSearchTheseArr); $j++){
$query.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
if($j!=count($newSearchTheseArr)-1){
$query.=" +";
}else{
//removes the OR from the last line and replaces with the following
$query.= " > 3";
}
}
);
I get:

Parse error: syntax error, unexpected T_FOR, expecting ')' in ...\searchLinkFiles\linkTextareaSearchResultFunctioning.php on line 67

Fou-Lu
07-30-2008, 09:52 PM
I'll take a stab:

Guess I should clarify. The error you're receiving is because PHP doesn't know what the value of the below is in context to how to put it into an array. So, its telling you that it only knows how to treat it as a string, so you need to insert the data as a string. This is wrong for what you want to do, but keep reading and I'll show you what it is.



$newSearchTheseArr = array(
for ($j=0; $j < count($newSearchTheseArr); $j++){

This says that you're trying to create an array with the inner value of a loop referring to the current array. I can't see how this would work, count($newSearchTheseArr) would be undefinable since it doesn't know how to construct the fields correctly. I'm guessing you want to check if $newSearchTheseArr is an array, so do so like:


// Don't use is_array; $newSearchTheseArr should have been forward declared as one so this would be true.
$countSearchArr = count($newSearchTheseArr);
if ($countSearchArr > 0) // or if ($countSearchArr)
{
for ($j=0; $j < $countSearchArr; $j++){
$query.=" CASE WHEN text_data LIKE '&#37;" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
if($j!=$countSearchArr-1){
$query.=" +";
}else{
//removes the OR from the last line and replaces with the following
$query.= " > 3";
}
}
}

I don't think the query will be valid with the '+' or >3, but at least it should execute the PHP.

gilgalbiblewhee
07-30-2008, 09:53 PM
I'll take a stab:


$newSearchTheseArr = array(
for ($j=0; $j < count($newSearchTheseArr); $j++){

This says that you're trying to create an array with the inner value of a loop referring to the current array. I can't see how this would work, count($newSearchTheseArr) would be undefinable since it doesn't know how to construct the fields correctly. I'm guessing you want to check if $newSearchTheseArr is an array, so do so like:


$countSearchArr = count($newSearchTheseArr);
if ($countSearchArr > 0)
{
for ($j=0; $j < $countSearchArr; $j++){
$query.=" CASE WHEN text_data LIKE '%" .$newSearchTheseArr[$j]. "%' THEN 1 ELSE 0 END";
if($j!=$countSearchArr-1){
$query.=" +";
}else{
//removes the OR from the last line and replaces with the following
$query.= " > 3";
}
}
}

I don't think the query will be valid with the '+' or >3, but at least it should execute the PHP.

Ok. I need to step back.
How do you filter out the small words after exploding the string?

$searchThese = "the who and but Moses flock Jethro father priest Midian flock backside desert mountain Horeb angel appeared flame midst looked behold burned consumed Moses aside great sight burnt";
$searchTheseArray = explode(' ', $searchThese);
print_r($searchTheseArray);
$newSearchTheseArray = array_filter($searchTheseArray, 'small_word');

function small_word($word)
{
return strlen($word) > 4;
}

Fou-Lu
07-30-2008, 10:01 PM
That looks correct for you're filter. You provide it with an array and pretty much say 'if the strlen of the item is > 4, keep it, otherwise dump it'.
Are you having troubles with this block of code not working properly?

gilgalbiblewhee
07-30-2008, 10:01 PM
Ok well. It seems that there was one phrase missing:

print_r($newSearchTheseArray);
At the end:

$searchThese = "the who and but Moses flock Jethro father priest Midian flock backside desert mountain Horeb angel appeared flame midst looked behold burned consumed Moses aside great sight burnt";
$searchTheseArray = explode(' ', $searchThese);
print_r($searchTheseArray);
$newSearchTheseArray = array_filter($searchTheseArray, 'small_word');

function small_word($word)
{
return strlen($word) > 4;
}
print_r($newSearchTheseArray);
But I want it renumbered so that I can pass it through a For-loop.
This is what I get so far:

Array ( [4] => Moses [5] => flock [6] => Jethro [7] => father [8] => priest [9] => Midian [10] => flock [11] => backside [12] => desert [13] => mountain [14] => Horeb [15] => angel [16] => appeared [17] => flame [18] => midst [19] => looked [20] => behold [21] => burned [22] => consumed [23] => Moses [24] => aside [25] => great [26] => sight [27] => burnt )

Fou-Lu
07-30-2008, 10:06 PM
Oh, I see what you mean. You're indexes are keeping their positions. Gotcha. The easiest solution would be to remove the for loop and run with a foreach loop - you don't need to use the indexes anyway so a foreach would provide what you needed. Another solution would be to use array_walk to recreate you're keys - too bothersome IMO. I'm pretty sure that PHP has a built in function like array_[map/walk/filter] that resets the indexes, but off hand I'm not certain what it is :|
Use a foreach, much easier.


Thats right. And to add more stuff to the end (like the + and > 3), use some simple logic. Use something like:


$i = 0;
foreach ($arr AS $val)
{
if ($i++ > 0)
{
$str .= ' +';
}
}
$str = trim($str, ' +');
$str .= ' > 3';

for example. Foreach is the pain since it doesn't track count, but there is always a way around things :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum