You are here: Re: Searching through an array « All PHP « IT news, forums, messages
Re: Searching through an array

Posted by -Lost on 08/22/06 18:39

> "-Lost" <spam_ninjaREMOVEME@REMOVEMEcomcast.net> wrote in message
> news:9rqdncF-r5QiQHfZnZ2dnUVZ_q6dnZ2d@comcast.com...
>> "John Rappold" <jrappold@jscoca-k12.org> wrote in message
>> news:IKmGg.20285$uV.15613@trnddc08...
>>
>>> $dirn = substr($file, 0, 6);
>>> $birn = substr($file, 7, 6);
>>> $fdate = substr($file, 14, 8);
>>> $period = substr($file, 23, 5);
>>> $cat = substr($file, 29, 3);
>>>
>>> $fields = array($dirn, $birn, $fdate, $period, $cat);
>>>
>>> $badchars = array("-", "_");
>>
>> How about:
>>
>> for($i = 0; $i < count($fields); $i++)
>> {
>> for($j = 0; $j < count($badchars); $j++)
>> {
>> if(strpos($fields[$i], $badchars[$j]) === false)
>> {
>> continue;
>> }
>> else
>> {
>> print $fields[$i] . ' contained ' . $badchars[$j] . '<br />' . "\n";
>> }
>> }
>> }
>>
>> It may not be the most elegant solution, and using regular expressions might make it
>> considerably easier, but hey... it works efficiently.
>>
>> -Lost

"John Rappold" <jrappold@jscoca-k12.org> wrote in message
news:jDFGg.11820$RQ5.358@trnddc03...
> -Lost,
>
> Thanks, that worked well.
>
> So that i can learn something and not have to ask a question in the same context, again,
> could you walk me through what these lines are doing:
>
> for($i = 0; $i < count($fields); $i++)
> {
> for($j = 0; $j < count($badchars); $j++)
>
>
> I understand that I have to traverse through both arrays, but I'm not sure what you did
> in the above lines.
>
> Later,
> jr

Firstly, (I gotta' say it, I see it all the time) do not top post. Makes reading posts
that much more difficult. Kind of like reading the post backwards.

ANYWAY...

count will tell me how many elements reside in the array $fields (and $badchars). So,
looping through 0 to that number (using < *less than* makes it stop at arraySize - 1)
gives us each element in your fields array.

Well... after each $field element we loop through the entire $badchar array.

Nested loops work from the inside out. Remember that!

So, each element in $field is then ran through *all* of the $badchar array. Each element
in $field gets checked against every element in $badchar. (I said that twice because I am
still not sure if I am explaining it sufficiently.)

A dummy example:

// pseudo-code

$a = (1, 2);
$b = 3, 4, 7;

check A1
does it equal 3
does it equal 4
does it equal 7
check A2
does it equal 3
does it equal 4
does it equal 7

See? Sorry if I "dumped" it down too much. My wife tells me that sometimes I explain
stuff like everyone thinks like me and then when I try to explain better I end up
explaining like everyone is 3.

Hope it helped.

-Lost

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация