473,473 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Array Search Problem

384 Contributor
I'm trying to search an array for a string and if the string exists in the array it wont go into the if statement, I'm using the below code, It just wont work for me, any ideas?
Expand|Select|Wrap|Line Numbers
  1. if((!in_array($string,$Array)) && ($string != '')) {
  2.         print $string.'<br/>';
  3. }
Oct 14 '09 #1
11 1868
ShenPixel
5 New Member
This is what I do, would that be practical?

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ( in_array ( $string, $array) ){
  3. echo 'The ' .$string. ' is in the array';
  4. }
  5. else
  6. {
  7. echo 'The ' .$string. ' is not in the array';
  8. }
  9.  
  10.  
Oct 14 '09 #2
Dormilich
8,658 Recognized Expert Moderator Expert
@ziycon
translated into words:
if string is not in array and string is not empty then print string.

...exactly what has happened.

although I'd test for the string first (the second expression is not executed if the first expression evaluates to FALSE)
Expand|Select|Wrap|Line Numbers
  1. if ($string && in_array(...))
Oct 14 '09 #3
ziycon
384 Contributor
Basicly if the string doesn't exist in the array and the string isn't empty then it will print out but whats happening is even if the string is in the array it still prints out the string, which it shouldn't do!
Oct 14 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
can you give an example to reproduce, because that sounds unlikely?
Oct 14 '09 #5
ziycon
384 Contributor
$outputArrayTemp[$outputCountTemp] has just been assigned a string.

The strings would be links like 'http://www.google.ie' etc.

Expand|Select|Wrap|Line Numbers
  1. if((!in_array($outputArrayTemp[$outputCountTemp],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp] != '')) {
  2.     print $outputArrayTemp[$outputCountTemp].'<br/>';
  3.     $outputCountTemp++;
  4. }
Oct 14 '09 #6
ziycon
384 Contributor
Another bit of information, the links are printing out fine, the function about is basicly to stop any link being printed out twice. So say that http://www.google.ie is in $outputArrayTemp and say the link http://www.google.ie is found four times then its printing like:

http://www.google.ie
http://www.google.ie
http://www.google.ie
http://www.google.ie

When it shouldn't print out as it exists in $outputArrayTemp.
Oct 15 '09 #7
ziycon
384 Contributor
Ok, heres what I have so far, the full function. Hopefully this makes it easier to understand what I'm trying to do. Any help is much appreciated.
Expand|Select|Wrap|Line Numbers
  1. $linkCount = sizeof($matches[1]);
  2.  
  3. while($count < $linkCount) {
  4.     $string = $matches[1][$count];
  5.  
  6.     if($string[0] == '/') {
  7.         $outputArrayTemp[$outputCountTemp] = $domain.$string;
  8.         $outputCountTemp++;
  9.     }
  10.     else if(substr($string,0,4) == 'http') {
  11.         $outputArrayTemp[$outputCountTemp] = $string;
  12.         $outputCountTemp++;
  13.     }
  14.  
  15.     if((!in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp-1] != '')) {
  16.         $outputArray[$outputCount] = $outputArrayTemp[$outputCountTemp-1];
  17.         $outputCount++;
  18.     }
  19.     $count++;
  20. }
Oct 15 '09 #8
Dormilich
8,658 Recognized Expert Moderator Expert
did you define the start values of $outputCountTemp and $count anywhere?
Oct 15 '09 #9
ziycon
384 Contributor
There both defined as zero, think I have it working withe the below code:
Expand|Select|Wrap|Line Numbers
  1. $linkCount = sizeof($matches[1]);
  2.  
  3. while($count < $linkCount) {
  4.     $string = $matches[1][$count];
  5.  
  6.     if(($string[0] == '/') || (substr($string,0,4) == 'http')) {
  7.         if($string[0] == '/') {
  8.             $outputArrayTemp[$outputCountTemp] = $domain.$string;
  9.             $outputCountTemp++;
  10.         }
  11.         else if(substr($string,0,4) == 'http') {
  12.             $outputArrayTemp[$outputCountTemp] = $string;
  13.             $outputCountTemp++;
  14.         }
  15.  
  16.         if(!in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)) {
  17.             $outputArray[$outputCount] = $outputArrayTemp[$outputCountTemp-1];
  18.             $outputCount++;
  19.         }
  20.     }
  21.     $count++;
  22. }
Oct 15 '09 #10
Dormilich
8,658 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
this will always evaluate to false
Oct 15 '09 #11
ziycon
384 Contributor
@Dormilich
I know, I fixed it, was meant to be:
Expand|Select|Wrap|Line Numbers
  1. !in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)
That was the main problem with it not working as far as I can tell.
Oct 15 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
1
by: cyrvb | last post by:
Hello, I'm a very very newbie in C# I did start 2 days ago, I get Visual Stuido 2005 C# I try to understand how to manage the arrays I did write this
10
by: alanbe | last post by:
I am working on some scripts to help me automate the website creation process. I want to use a template for the whole website and call pages using something like ...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
10
by: Akhenaten | last post by:
I have an array created but I need to search it. The array is created from a mysql call. Code as follows: ****************** function respond_check($qid) { include "db.inc.php"; $postchk2 =...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
3
by: Robert Bevington | last post by:
Hi all, I ran into memory problems while tying to search and replace a very large text file. To solve this I break the file up into chunks and run the search and replace on each chunk. This...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.