PHP ARRAY, there are many times the same valu inside - php
I am trying to use a csv file in my php code. I tried this:
if (($handle = fopen("licences/myFile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000)) !== FALSE) {
$num = count($handle);
$row++;
for ($cc=0; $cc < $num; $cc++) {
$UserLine = explode(";",trim($data[$cc]));
$Users =$UserLine[$cc];
echo "</br>" . print_r($UserLine) . "</br>";
}
}
}
But the output is not what I expect. I get this when I do the print_r:
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
Array ( [0] => GL [1] => )
1
Array ( [0] => papb1 [1] => )
1
Array ( [0] => RES [1] => )
1
Array ( [0] => ecllit [1] => )
1
It's repeating the values again and again.
The content of "myFile.csv" is:
GL;
papb1;
RES;
ecllit;
And my variable $users as the value:
GLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllitGLpapb1RESecllit
Can someone help me to only have the values one time, if it's not in the array at least in $users. Because I have a lot of users to add in the csv, but I want this step to work before writing all the users.
Sorry if it's a basic question or if I haven't given enough informations or if this is not clear, this is my first post :/
Thanks for any future replies,
Sellion
Use this updated.
Your csv file needs to be something like this.
GL
papb1
RES
ecllit
Then use the updated code as below.
if (($handle = fopen("file.csv", "r")) !== FALSE)
{
$line = 1;
while (($data = fgetcsv($handle, 1000)) !== FALSE)
{
echo "Line # $line : " . $data[0] . "</br>";
$line++;
}
}
This will give you output like below :
Line # 1 : GL
Line # 2 : papb1
Line # 3 : RES
Line # 4 : ecllit
I hope that is something you are looking for.
Related
How to access the values of this API return
Could you please help me? I need to know how to access the values of this response from an API call using PHP. Array ( [queryIds] => Array ( [0] => lHLLapn7EH ) [timedOutRealtimeData] => [realtimeDataMissing] => [cacheFreshness] => FRESH [prunedResult] => [novaCost] => 0 [novaRequestDuration] => 19 [wasCached] => 1 [minSampleRate] => 1 [timeComputed] => 1558794556710 [novaRuntime] => 199 [hitChunkGroupByLimit] => [throttleTime] => 0 [data] => Array ( [series] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [seriesLabels] => Array ( [0] => Array ( [0] => 0 [1] => C5241; GB ) [1] => Array ( [0] => 0 [1] => C9999; GB ) ) [seriesMeta] => Array ( [0] => Array ( [eventGroupBys] => Array ( [0] => C5241 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C5241; GB ) [1] => Array ( [eventGroupBys] => Array ( [0] => C9999 [1] => GB ) [segmentIndex] => 0 [eventIndex] => 0 [eventGroupBy] => C9999; GB ) ) [seriesCollapsed] => Array ( [0] => Array ( [0] => Array ( [setId] => [value] => 843 ) ) [1] => Array ( [0] => Array ( [setId] => [value] => 22 ) ) ) [xValues] => Array ( [0] => 2019-05-25 ) ) [partialMergedAndNewUserInformation] => [transformationIds] => Array ( [0] => 2696 ) [backend] => novaV2 )
You can access them using Array ( [0] => C9999 [1] => GB ) -> $arr['data']['seriesMeta'][0]['eventGroupBys'] Array ( [setId] => [value] => 20) -> $arr['data']['seriesMeta'][0][0]
Merging array keys in multidimentional array does not give desired output
This is my generated array i have tried array_merge_recrusive but not successful. I am doing program it should generate this array Array ( [0] => Array ( [competancy] => Array ( [0] => 1 ) ) [1] => Array ( [assistment] => Array ( [0] => 0 ) ) [2] => Array ( [competancy] => Array ( [0] => 3 ) ) [3] => Array ( [competancy] => Array ( [0] => 4 ) ) [4] => Array ( [assistment] => Array ( [0] => 0 ) ) [5] => Array ( [competancy] => Array ( [0] => 6 ) ) required [6] => Array ( [competancy] => Array ( [0] => 7 ) ) [7] => Array ( [assistment] => Array ( [0] => 0 ) ) ) I want following output array I have tried lot of but not find this type of output Array ( [competancy] => Array ( [0] => 1 ) [assistment] => Array ( [0] => 0 ) [competancy] => Array ( [0] => 3 [0] => 4 ) [assistment] => Array ( [0] => 0 ) [competancy] => Array ( [0] => 6 [0] => 7 ) [assistment] => Array ( [0] => 0 ) )
Try to merge array with splat : print_r(array_merge(...$array));
Posting multidimensional array as a value in Zend_Http_Client in Zend Framework 2
I have to post a form element inventory like following structure [inventory] => Array ( [0] => Array ( [inventory_id] => Array ( [0] => 1 ) [inventory_name] => Array ( [0] => Bed 90*200 ) [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) [1] => Array ( [inventory_id] => Array ( [0] => 15 ) [inventory_name] => Array ( [0] => Bed 90*200 ) [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) [2] => Array ( [inventory_id] => Array ( [0] => 15 ) [inventory_name] => Array ( [0] => Bed 90*200 ) [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) ) When I tried to assign this array to inventory in $client->setParameterPost(), I received POST values like this [inventory] => Array ( [0] => Array ( [inventory_id] => Array ( [0] => 1 ) ) [1] => Array ( [inventory_name] => Array ( [0] => Bed 90*200 ) ) [2] => Array ( [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) [3] => Array ( [inventory_id] => Array ( [0] => 15 ) ) [4] => Array ( [inventory_name] => Array ( [0] => Bed 90*200 ) ) [5] => Array ( [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) [6] => Array ( [inventory_id] => Array ( [0] => 15 ) ) [7] => Array ( [inventory_name] => Array ( [0] => Bed 90*200 ) ) [8] => Array ( [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) ) ) I have verified my array structure that is fine. I also checked in setParameter method in Client.php (Zend library), no issues. Just receiving this post. How can I achieve this?
Zend_Http_Client ignores integer keys of multi dimensional parameters. So in your case, [inventory] => Array ( [0] => Array ( [inventory_id] => Array ( [0] => 1 ) [inventory_name] => Array ( [0] => Bed 90*200 ) [inventory_photo] => Array ( [0] => 1_bed_90x200.jpg ) ) will be translated to inventory[][inventory_id][] => 1 inventory[][inventory_name][] => Bed 90*200 inventory[][inventory_photo][] => 1_bed_90x200.jpg The solution to this problem is either to use a class extended from Zend_Http_Client and override its method _flattenParametersArray(). or convert the params arrays to strings yourself, such as: $client->setParameterPost('inventory[0][inventory_id][]', 1); $client->setParameterPost('inventory[0][inventory_name][]', 'Bed 90*200'); $client->setParameterPost('inventory[0][inventory_photo][]', '1_bed_90x200.jpg');
Recursively remove elements from nested array looking for a specific value
I have the following nested array: Array ( [0] => Array ( [0] => [1] => 51.212342,6.7834665 ) [1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 ) [2] => Array ( [0] => 28.8.2013 15:11:53 [1] => 0,0 ) [3] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 ) [4] => Array ( [0] => 28.8.2013 15:36:06 [1] => 0,0 ) [5] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 ) [6] => Array ( [0] => 28.8.2013 15:40:14 [1] => 0,0 ) [7] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 ) [8] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 ) [9] => Array ( [0] => 28.8.2013 16:11:20 [1] => 0,0 ) [10] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 ) [11] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 ) [12] => Array ( [0] => 28.8.2013 18:11:34 [1] => 0,0 ) [13] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 ) [14] => Array ( [0] => 28.8.2013 18:11:55 [1] => 0,0 ) [15] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 ) ... ); Here I need to remove all the elements that have "0,0" as their [$n][1] value. I tried this but some of the "0,0" are still there. Why? for ($i = 0; $i < sizeof($locations); $i++) { $key = array_search('0,0', $locations[$i]); if ($key !== false) { unset($locations[$i]); $locations = array_values($locations); } } Array ( [0] => Array ( [0] => [1] => 51.212342,6.7834665 ) [1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 ) [2] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 ) [3] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 ) [4] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 ) [5] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 ) [6] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 ) [7] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 ) [8] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 ) [9] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 ) [10] => Array ( [0] => 29.8.2013 10:07:56 [1] => 51.2123948,6.7834622 ) [11] => Array ( [0] => 29.8.2013 11:57:45 [1] => 51.21244537,6.78355515 ) [12] => Array ( [0] => 29.8.2013 11:58:27 [1] => 51.21238401,6.78352698 ) [13] => Array ( [0] => 29.8.2013 12:01:17 [1] => 51.2124044633333,6.78353637 ) [14] => Array ( [0] => 29.8.2013 12:11:18 [1] => 51.2124044633333,0.783536 ) [15] => Array ( [0] => 29.8.2013 12:12:39 [1] => 51.212416045,6.783523 ) ... );
One problem with your code is that sizeof($locations) changes every time you do a unset. So whenever you have two consecutive [$n][1] having "0,0", you are not able to detect that. Also your code is not looking at just[$n][1], its looking at all indexes in [$n][] Use the below code: $count = count($locations); for ($i = 0; $i < $count; $i++) { if ($locations[$i][1] == "0,0") { unset($locations[$index]); } } $locations = array_values($locations);
foreach ($locations as $index => $row) { if ($locations[$index][1] == "0,0") unset($locations[$index]); } $locations = array_values($locations);
php - matching elements in an array
I've been playing with this all day and haven't figured out a good way to do it... I have two arrays and am trying to create an array based on matching values. //$original Array ( [0] => Array ( [items] => Array ( [0] => Array ( [0] => PA [1] => DZ [2] => ER [3] => TY ) [1] => Array ( [0] => KV [1] => EN [2] => CR ) [2] => Array ( [0] => HU [1] => GO [2] => GA [3] => FI ) ) ) ) //$compare Array ( [0] => Array ( [items] => Array ( [0] => Array ( [0] => PA [1] => AN [2] => ER ) [1] => Array ( [0] => KV ) [2] => Array ( [0] => HU [1] => XV [2] => ZL [3] => FI ) ) ) ) And I'm trying to produce //$similar Array ( [0] => Array ( [items] => Array ( [0] => Array ( [0] => PA [2] => ER ) [1] => Array ( [0] => KV ) [2] => Array ( [0] => HU [3] => FI ) ) ) )
Use array_intersect. $similar = $compare; $similar[0]['items'] = array_intersect($compare[0]['items'], $original[0]['items']); codepad example