Nielsdv
asked on
PHP : Weird results with explode and array_intersect
Hi,
I'm having this piece of code to parse a bit of text into an array and then compare it with
keys i define in another array. However if i compare it and i do not have another array value
after that value i'm checking on, the check will fail and i will get no result,
Example :
Array ( [5] => TEST ) // The intersect result
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last ) // It's found in this array.
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST ) // It's however, not found in this array.
I don't get it ! can anyone help me ?
The work around to add another value so the check will work is unfortunatly not possible in my case =).
The code i'm using is just a test case - it is not a final code- so save your markup tips.
I'm having this piece of code to parse a bit of text into an array and then compare it with
keys i define in another array. However if i compare it and i do not have another array value
after that value i'm checking on, the check will fail and i will get no result,
Example :
Array ( [5] => TEST ) // The intersect result
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last ) // It's found in this array.
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST ) // It's however, not found in this array.
I don't get it ! can anyone help me ?
The work around to add another value so the check will work is unfortunatly not possible in my case =).
The code i'm using is just a test case - it is not a final code- so save your markup tips.
$string = "
4/28 16:14:50.187 Tester whispers: 200 TEST not last
4/28 16:14:50.328 To Tester: 200 TEST
4/28 16:30:55.875 Joined Channel: |Hchannel:1699248|h[(null)]|h";
$i = 0;
$string = explode("\n",$string);
$function_array[] = 'TEST';
foreach ($string as $key => $val) {
$val = str_replace("[","",$val); // removes the brackets
$val = str_replace("]","",$val); // removes the brackets
$val= str_replace(" "," ",$val); // removes double spaces
$array[] = explode(" ",$val); // Saves to array, seperated word by word
print_r(array_intersect($array[$i],$function_array));
print_r($array[$i]);
echo "<br>";
$i++;
}
ASKER
$array is a two dimensional array =)
and the corected code to my guess
$i = 0;
$string = explode("\n",$string);
$function_array[] = 'TEST';
$array = array(); //always init vars :)
foreach ($string as $key => $val) {
//its good practice not to modify $key or $val
$vals = str_replace("[","",$val); // removes the brackets
$vals = str_replace("]","",$vals); // removes the brackets
$vals= str_replace(" "," ",$vals); // removes double spaces
$array[$i][] = explode(" ",$vals); // Saves to array, seperated word by word
print_r(array_intersect($array[$i][],$function_array));
print_r($array[$i][]);
echo "<br>";
$i++;
}
ASKER
Okay, i see you can't eddit comments on EE, anyway.
It's an two dimensional array say ;
Each loop it makes a new entry in the array, and i use i to define wich sub-array i want to calculate.
Giving that the code isn't really optimal, the array_intersect works fine, on every array !
Aslong it isn't the last word in the sub-array.
It's an two dimensional array say ;
Each loop it makes a new entry in the array, and i use i to define wich sub-array i want to calculate.
Giving that the code isn't really optimal, the array_intersect works fine, on every array !
Aslong it isn't the last word in the sub-array.
ASKER
The code your posted doesn't work because intersect can't read an undefined []
"Fatal error: Cannot use [] for reading in /home/nielsdv/domains/niel sdv.nl/pub lic_html/p roject/par ser.php on line 202"
Anyway- the point is that it DOES actually work and it's already a two dimensional array.
The only problem is - it doesnt work if let's say out of 6 values , the 6th value is the one you're looking for.
It does work if let's say out of 6 values the 5th one is the one you're looking for.
"Fatal error: Cannot use [] for reading in /home/nielsdv/domains/niel
Anyway- the point is that it DOES actually work and it's already a two dimensional array.
The only problem is - it doesnt work if let's say out of 6 values , the 6th value is the one you're looking for.
It does work if let's say out of 6 values the 5th one is the one you're looking for.
ASKER
$array[] -> new array entry -> explode is an array result.
so array in array
$array[0] => array[0]=> array[1] =>
so array in array
$array[0] => array[0]=> array[1] =>
yup, your right there, i missed the brackets (maybe i shouldnt post stuff half asleep :)) ) let me take another look at it
hi,
ive just run your code and it seems to work correctly, check the attached code (ive only added a few echos).
ive just run your code and it seems to work correctly, check the attached code (ive only added a few echos).
$string = "
4/28 16:14:50.187 Tester whispers: 200 TEST not last
4/28 16:14:50.328 To Tester: 200 TEST
4/28 16:30:55.875 Joined Channel: |Hchannel:1699248|h[(null)]|h";
$i = 0;
$string = explode("\n",$string);
$function_array[] = 'TEST';
foreach ($string as $key => $val) {
echo "curent line: " . $val . "<br>";
$val = str_replace("[","",$val); // removes the brackets
$val = str_replace("]","",$val); // removes the brackets
$val= str_replace(" "," ",$val); // removes double spaces
$array[] = explode(" ",$val); // Saves to array, seperated word by word
echo "Intersect: <br>";
print_r(array_intersect($array[$i],$function_array));
echo "<br>the array: <br>";
print_r($array[$i]);
echo "<hr>";
$i++;
}
maybe you miscounted the output lines? (be aware that theres an empty line at the start of the string, which is gonna give two empty arrays at start)
ASKER
The output of this file is giving a good example;
curent line: 4/28 16:14:50.187 Tester whispers: 200 TEST not last
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last ) curent line: 4/28 16:14:50.328 To Tester: 200 TEST
Intersect:
Array ( )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.328 [2] => To [3] => Tester: [4] => 200 [5] => TEST )
So as you can see the intersect is empty on the second one, while there IS test in the text.
That shouldn't be happening ?
curent line: 4/28 16:14:50.187 Tester whispers: 200 TEST not last
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last ) curent line: 4/28 16:14:50.328 To Tester: 200 TEST
Intersect:
Array ( )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.328 [2] => To [3] => Tester: [4] => 200 [5] => TEST )
So as you can see the intersect is empty on the second one, while there IS test in the text.
That shouldn't be happening ?
heres the (stripped)output that im getting when im running the script:
curent line: 4/28 16:14:50.187 Tester whispers: 200 TEST not last
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last )
curent line: 4/28 16:14:50.328 To Tester: 200 TEST
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.328 [2] => To [3] => Tester: [4] => 200 [5] => TEST )
as you can see im getting the correct output, can you tell me more about the environment in which you test the script?
curent line: 4/28 16:14:50.187 Tester whispers: 200 TEST not last
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.187 [2] => Tester [3] => whispers: [4] => 200 [5] => TEST [6] => not [7] => last )
curent line: 4/28 16:14:50.328 To Tester: 200 TEST
Intersect:
Array ( [5] => TEST )
the array:
Array ( [0] => 4/28 [1] => 16:14:50.328 [2] => To [3] => Tester: [4] => 200 [5] => TEST )
as you can see im getting the correct output, can you tell me more about the environment in which you test the script?
ASKER
Also, when i add asmuch as a spacebar in the source (so theres another value in the array) it DOES pick it up :S
ASKER
http://www.nielsdv.nl/project/parser.php <- here is my script ran from, its on PHP5 Environment
ASKER
okay i just called up my phpinfo , and i was quite suprised with the results :
http://www.nielsdv.nl/project/phpinfo.php
http://www.nielsdv.nl/project/phpinfo.php
ASKER
Okay i ran php info i got chinese :
!OTP tlPBI -/3/DDXTL10Tastoa/E""T/hm1 tastoa.t"
hm>ha>bd bcgon-oo:#fff oo:#000}bd,t,t,h,h fn-aiy assrf}pe{agn p;fn-aiy oopc;
:ik{oo:# 009 etdcrto:nn;bcgon-oo:#fff}a hvr{etdcrt o:udrie}tb e{odrclas: clas;
cne tx-lg:cne;
cne al agnlf:at;mri-ih:at;tx-lg:l f;
cne h{tx-lg:cne iprat
d h {bre:1xsld#000 otsz:7% etclain aeie}h fn-ie 5%}h fn-ie 2%}. tx-lg:lf;
e{ akrudclr ccf;fn-egt od oo:#000}. bcgon-oo:#99c otwih:bl;clr 000;
v{akrud clr ccc;clr 000;
v bcgon-oo:#ccc etain ih;clr 000;
m fot ih;bre:0x}h wdh 0p;bcgon-oo:#ccc odr p;hih:1x oo:#000}mt ae"OOS otn=NIDXNFLO,ORHV"/igbre=0 r=/rjc/hif.h?PP96F4D2-12A6 -0A0AF2 l=PPLg"//r
t>t ls=e>ul aet ls=
If i removed the www. from the link it worked fine.
I called the webhost provider - and he claimed they both worked for him.
I let some friends check it out, and also for them it worked okay.
Somehow this must be a local problem, however i am really unable to address it.
!OTP tlPBI -/3/DDXTL10Tastoa/E""T/hm1
hm>ha>bd bcgon-oo:#fff oo:#000}bd,t,t,h,h fn-aiy assrf}pe{agn p;fn-aiy oopc;
:ik{oo:# 009 etdcrto:nn;bcgon-oo:#fff}a
cne tx-lg:cne;
cne al agnlf:at;mri-ih:at;tx-lg:l
cne h{tx-lg:cne iprat
d h {bre:1xsld#000 otsz:7% etclain aeie}h fn-ie 5%}h fn-ie 2%}. tx-lg:lf;
e{ akrudclr ccf;fn-egt od oo:#000}. bcgon-oo:#99c otwih:bl;clr 000;
v{akrud clr ccc;clr 000;
v bcgon-oo:#ccc etain ih;clr 000;
m fot ih;bre:0x}h wdh 0p;bcgon-oo:#ccc odr p;hih:1x oo:#000}mt ae"OOS otn=NIDXNFLO,ORHV"/igbre=0
t>t ls=e>ul aet ls=
If i removed the www. from the link it worked fine.
I called the webhost provider - and he claimed they both worked for him.
I let some friends check it out, and also for them it worked okay.
Somehow this must be a local problem, however i am really unable to address it.
ASKER
The script still fails on my webhost , on everyone who i let it check it gave the outcome without the 1st TEST in the array.
hey nields, i see the php info page just fine, the only thing interesting there is the version of the php (latest from feb 2009)(5.2.9) my version is a bit older (5.2.4) and the script runs fine with me. I also looked in the change log for 5.2.9 and there was some work done on explode and array_intersects (altough dunno if they are the cause).
either way run some simple code like the attached and see whats the output
either way run some simple code like the attached and see whats the output
$firstArray = array("a1", "a2", "a3", "a4");
$secondArray = array("a3");
$thirdArray = array("b1", "b2", "a3");
print_r(array_intersect($firstArray, $secondArray));
echo "<br>";echo "<br>";
print_r(array_intersect($thirdArray, $secondArray));
ASKER
Output works how it should :
Array ( [2] => a3 )
Array ( [2] => a3 )
- maybe it has something to do with explode but i can't really see what
Array ( [2] => a3 )
Array ( [2] => a3 )
- maybe it has something to do with explode but i can't really see what
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That is the answer to my problem, it works correctly now.
Thanks allot! for your help :)
It's really appreciated.
Thanks allot! for your help :)
It's really appreciated.
ASKER
I've truly learned my lesson. Don't use notepad - not even when you're too lazy to open Eclipse
>_<
>_<
i am very puzzled about this line
print_r(array_intersect($a
array_intersect needs two or more arrays two work, $array[$i] is a string so you need to probably remove the $i from there. And the print_r will display the intersection of values from the two arrays (not keys).
another problem is with print_r($array[$i]); again do you want to see the $i th word from the current line (the $i being the current line count)(doesnt really make sense)
IF you want to save the lines in $array then the line needs to be
$array[$i][] = explode(" ",$val); // Saves to array, seperated word by word
and the future calls to this array needs to be $array[$i][] (for the intersect and the following print_r functions)
hope I got your intentions right and that this helps