I have two arrays, one of them is a one dimensional array, a product of an explode by \n and then exploded by comma on a csv file, the other array is a mysql_fetch_array that contains information from the database, my goal is to compare those arrays and find out the difference between them or new records that exist in the csv array and not in the database, i have tried array_diff, array_diff_assoc, array_udiff , i tried comparing each line of the csv file to the database using a select statement but its taking forever to load, i tried almost all the solutions here on stackoverflow.com, but i can't seem to compare those arrays.
This is a snippet from my code :
$input = file_get_contents('test.csv');
$newarray = explode("\n", $input);
$sql = mysql_query("SELECT * FROM products")or(die(mysql_error()));
$basearray = mysql_fetch_array($sql);
foreach ($newarray as $key => $value) {
$each_line = explode(",", $value);
// code goes here
// i tried producing a new array here and compare it
// to $basearray later, but it didn't work.
}
Related
Example
I have CSV file that contains data of random number example data in CSV :
639123456789,73999999999,739222222222,839444444444,8639555555555....more
So, if I upload it, I want it to explode in a variable or in an array as long as I get the specific data. example of data I want to get is all 2 first line starting at 73 be extract so meaning all numbers that start with 73 only.
Example: 73999999999,739222222222
I have tried it by using only 1 number using split,substr and explode function but my problem is if the user input a CSV bulk data with no limit.
You can use substr() and loop through your array deleting the elements that do not match.
$str = '639123456789,73999999999,739222222222,839444444444,8639555555555';
$array = explode(',', $str); //Convert your string to an array.
$searchString = '73'; //Set the value your looking for.
foreach($array as $key=>$value){
if(substr($value, 0, 2) != $searchString){ //This will test first two characters.
unset($array[$key]);
}
}
$array = array_values($array);
print_r($array);
This will output:
Array
(
[0] => 73999999999
[1] => 739222222222
)
Updated
This will make a new array with only the numbers you want in it, leaving the original array untouched.
$str = '639123456789,73999999999,739222222222,839444444444,739222222222,839444444444,839444444444,73999999999';
$array = explode(',', $str);
$searchString = '73';
foreach($array as $key=>$value){
if(substr($value, 0, 2) == $searchString){
$results[] = $value;
}
}
print_r($results);
I got following situation.
I got a csv file and some data in a mysql database.
I want to load the csv file and compare it to a row in the mysql database.
The csv file got following types of data:
eet003 240
eet003 180
eet003 280
eet299 100
But in the database the data looks like this:
eet003
eet299
So both datas a stored in arrays.
$array1
$array2
And I want to compare the two arrays and save the result in a third array.
$diff = array_diff($array1, $array2)
But I onlny want to compare the first 6 chars of the data. So right now eet003 240 is different from eet003. But for me its the same. I only need eet003 e.g.
How could I archieve that? I now the substr function but array1 is created the follwing way:
$rows = array(); //aray for csv file
$rows = file('pinesite_eet.csv', FILE_IGNORE_NEW_LINES); //write csv in array
I don't know how du cut the string from the csv when it's loaded in the array.
The second array is written from a mysql_query.
Any suggestions on how I could do that?
I think something like this should work:
(Here I just use array_udiff() with a anonymous function where I only use the first part of the file data and compare it with the database data)
<?php
$result = array_udiff($fileData, $dbData, function($a, $b) {
if(strcasecmp(explode(" ", $a)[0], $b) == 0)
return 0;
return strcasecmp(explode(" ", $a)[0], $b);
});
?>
Might be easier to just modify the data. This removes the space and everything after in each element:
$rows = preg_replace('/ .*/', '', $rows);
Then array_diff() should work as expected.
Hi I have been struggling with population of an array if you can help it will be much appreciated.
So I have these two arrays $start_range[] and $end_range[] which both contain
respective values and both arrays are of the same size. For example: $start_range[0] = 1000 and $end_range[0]=[2000]. Now I want to fill a new array with the range between those numbers and keep the respectiveness of the values so as in the example $new_array[0] = range($start_range[0],$end_range[0]).
In the moment I am using this code here
for ($i=0; $i<sizeof($start_range); $i++) {
$new_array[] = range($start_range[$i], $end_range[$i]);
}
But my problem is that it generates arrays with the same data because it loops through the size of the array. As if the size of the array was 4 then it will generate 4 new exact same arrays. I can't break out of the loop as it generates arrays only from the first columns of the two arrays.
Any solution?
Would this do?
$newArray = array();
foreach ($startArray as $key => $value) {
$newArray[$key] = range($startArray[$key], $endArray[$key]);
}
I have a form field called $_POST['hidden-tags']
It has values entered in as follows
cars, vans, bikes, trains,
I on post im trying to split the values and save them into an array and then compare that array with another I have and only display the values that are different. I know the $arr1 has values as I have tested the data.
the code I have so far is
$arr1;
$arr2 = array();
foreach($_POST['hidden-tags'] as $value){
$arr2[] = explode(",",$value);
}
print_r($arr2);
// $tmp = array_diff_key($arr1, $arr2);
// echo $tmp;
parts of which I found here on stack
PHP explode array
As you can see I have the final bit commented out. This is so I can see the array values. If I echo the $arr2 all I see on screen is
Array()
even though I have entered cars, bikes, vans. I have not got as far as comparing the two array yet and displaying the $tmp variable
Is $_POST['hidden-tags'] a text input field? If so, you don't need to run a foreach on it to split it into an array. Just explode it and compare the generated array with the one you already have.
I don't think $_POST['hidden-tags'] is an array, but a string you want to split.
Also I think you don't want to save the result of the split action on your POST variable in the next index of $arr2, because split will return an array and this way you will have an array in an array.
$arr1;
$arr2 = array();
$arr2[] = explode(",",$_POST['hidden-tags']);
print_r($arr2);
$tmp = array_diff_key($arr1, $arr2);
echo $tmp;
I'm using this to to retrieve two columns in a database
while($comment[]=mysql_fetch_array($sql));
this returns an array with associative and number indices for each column.
This works great but I would also like to create a new array from the orginial$comment[] that is just a simple array of strings (only the first column). What are my options? Is there any way to accomplish this without looping through a second time?
Depending on how many columns you have, you could do something like:
$result = mysql_query($sql);
while (list($col1[], $col2[]) = mysql_fetch_row($result));
$col1 will be an array of just column 1's values, and $col2 will be similar for column 2's values.
$array = array();
for ($i = 0;$comment = mysql_fetch_array($sql);$i++){
$array[$i] = $comment['field_name'];
}