kamkam 03-13-2009, 10:46 AM Hi;
I got two arrays which is first_array and second_array,
i want to keep the elements which in second_array and the first latter do not start with a,k,c,m,d,e,f,g.
so i just want to keep 'pppa','yyyyk','zzjjj' for the second_array,could anyone tell me how do that, please, thanks
$first_Array=('a','k','b','c','m','d','e','f','g')
$second_Array=('ahhh','kggg','bhjj','ctert','msd','dgx','efd','fffd','gdf','pppa','yyyyk','zzjjj')
Fou-Lu 03-13-2009, 11:10 AM Depends on if you want to do it the lazy way or not ;)
This way would require a global call:
function removeMatchFirst($item)
{
global $first_Array;
$bResult = false;
if(is_string($item))
{
$firstChar = substr($item, 0, 1);
if (!in_array($firstChar, $first_Array))
{
$bResult = true;
}
}
return $bResult;
}
$second_array = array_filter($scondArray, 'removeMatchFirst');
Another (probably better I guess) solution would be to use an array_walk. It takes some work to force an additional parameter in as a reference, but it can be done.
kamkam 03-13-2009, 11:21 AM sorry, where you call to the fuction removeMatchFirst with parrmata with item ??
Fou-Lu 03-13-2009, 11:23 AM $second_array = array_filter($scondArray, 'removeMatchFirst');
^
Called Here
kamkam 03-13-2009, 12:16 PM there are nothing i can print out ??? bit strange
$first_Array=array('a','k','b','c','m','d','e','f','g');
$secondArray=array('ahhh','kggg','bhjj','ctert','msd','dgx','efd','fffd','gdf','pppa','yyyyk','zzjjj ');
function removeMatchFirst($item)
{
global $first_Array;
$bResult = false;
if(is_string($item))
{
$firstChar = substr($item, 0, 1);
if (!in_array($firstChar, $first_Array))
{
$bResult = true;
}
}
return $bResult;
}
$second_array = array_filter($secondArray, 'removeMatchFirst');
print_r($second_Array);
Fou-Lu 03-13-2009, 12:20 PM there are nothing i can print out ??? bit strange
$first_Array=array('a','k','b','c','m','d','e','f','g');
$secondArray=array('ahhh','kggg','bhjj','ctert','msd','dgx','efd','fffd','gdf','pppa','yyyyk','zzjjj ');
function removeMatchFirst($item)
{
global $first_Array;
$bResult = false;
if(is_string($item))
{
$firstChar = substr($item, 0, 1);
if (!in_array($firstChar, $first_Array))
{
$bResult = true;
}
}
return $bResult;
}
$second_array = array_filter($secondArray, 'removeMatchFirst');
print_r($second_Array);
Do you mean that its not printing anything?
$second_array = array_filter($secondArray, 'removeMatchFirst');
print_r($second_Array);
$second_array should be either $second_array or $second_Array. There is a case difference in the 'a'.
kamkam 03-13-2009, 01:04 PM Thanks, but why it it does not work for non english letter, it give me not correct result.
Array ( [0] => 万 [1] => 与 [2] => 丑 [3] => 专 [4] => 业 [5] => 是万 [6] => 的与 [7] => 飞丑 [8] => 人专 [9] => 任业 )
$first_Array=array('万','与','丑','专','业');
$secondArray=array('万','与','丑','专','业','是万','的与','飞丑','人专','任业');
function removeMatchFirst($item)
{
global $first_Array;
$bResult = false;
if(is_string($item))
{
$firstChar = substr($item, 0, 1);
if (!in_array($firstChar, $first_Array))
{
$bResult = true;
}
}
return $bResult;
}
$second_Array = array_filter($secondArray, 'removeMatchFirst');
print_r($second_Array);
Fou-Lu 03-13-2009, 01:08 PM I'll fix it when I get home and can write some code.
The problem is likely that its the unicode charset thats killing it. I'm thinking substr fetches 0 and 1 on byte counts and not character counts. I find out how to fix it later.
kamkam 03-13-2009, 01:12 PM ok, thanks for your time.
Fou-Lu 03-13-2009, 03:55 PM Ok, try this:
function removeMatchFirst($item)
{
global $first_Array;
$bResult = true;
if (is_string($item))
{
$iInArr = count($first_Array);
for ($i = 0; $i < $iInArr && $bResult; ++$i)
{
if (mb_strpos($item, $first_Array[$i]) === 0)
{
$bResult = false;
}
}
}
return $bResult;
}
Hopefully someone else has more experience with the mbstring library to find an easier way to do this.
kamkam 03-14-2009, 12:24 AM Thanks a lot, it does work now, thanks for your time again.
|
|