473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to compare file data array() with normal array

Am spliting file string into array by using fgets(),and explode()
On other hand am taking string from user,and again spliting it into array
now I have 2 array
when I compare 2 arrays by using array_intersect()(even by using 2 foreach(as a last try))...it is returning null array(Array()) even if they have common elements
WHY?
is any special settings/parameter/syntax require to compare file data with normal array?
Sep 29 '10 #1
12 1859
code green
1,726 Expert 1GB
Let us see a portion of both arrays
Sep 29 '10 #2
Shenno
59
do u need to find common elements in 2 arrays ?

u can loop through the bigger one.. "use count() to find which one is bigger"

then use in_array() function and if condition inside the loop to find if each element in big array is already exist in other array..

inside if cindition u can add this common elements in third array if u want like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(in_array($big_array[$i] , $small_array))
  3. {
  4. $common_array[] = $big_array[$i];
  5. }
  6.  
  7.  

hope this helps.
Oct 2 '10 #3
kovik
1,044 Expert 1GB
@Shenno: ... You just rebuilt array_intersect().
Oct 4 '10 #4
Shenno
59
he says array_intersect give 'em null array.

this may let him analyse why he get null array easly..
Oct 4 '10 #5
kovik
1,044 Expert 1GB
Outputting the arrays to ensure that there actually ARE matching elements will help him analyze why he is getting an empty array, not running the same code in a different format.
Oct 4 '10 #6
Shenno
59
FOR ME, my method makes me feel comfortable while seeing every thing is running step by step.
Oct 4 '10 #7
kovik
1,044 Expert 1GB
Which would make sense if your code showed the user everything, step by step.

Expand|Select|Wrap|Line Numbers
  1. foreach ($array1 as $array1_data) {
  2.   echo "Array 1 data, '$array1_data', "
  3.     . (in_array($array1_data, $array2) ? 'is' : '<b>is not</b>')
  4.     . " in Array 2.<br />\n";
  5. }
Oct 4 '10 #8
Shenno
59
xD

u have wrote a code specialized in showing which elements in common, outputing on screen.

main goal is common element array.
Oct 4 '10 #9
kovik
1,044 Expert 1GB
Right, which is the exact goal of array_intersect(). If the OP is not getting the correct results from array_intersect(), then they won't get the correct results from your remake, either.
Oct 4 '10 #10
Shenno
59
u know break points ?

mine makes me able to make something like that in my mind, which is not provided in array_intersect().
Oct 4 '10 #11
kovik
1,044 Expert 1GB
I don't even use breakpoints while programming or debugging. I doubt the OP does.
Oct 4 '10 #12
Shenno
59
and does too for question poster.
Oct 4 '10 #13

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

Similar topics

1
by: davehunt | last post by:
Hi folks, New C# programmer here. I am reading some CSV data from a file into an ArrayList. I want to get the data from the ArrayList into a 2-dimensional array. I see a few references to...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
1
by: becca0619 | last post by:
Hey, Ive been looking around to find out how to read data into a 2D array and I cant find a solution that will fit my assignment. Im not sure that I should even use an array actually... ok so...
3
by: Frank Rizzo | last post by:
I've had a windows service, that I've written and compiled with vs2003 and .net framework 1.1, running just fine on windows server 2003 for a long while. Recently the service was installed on...
1
by: wartech | last post by:
Hey everyone, My objective here is to be able to sort the array that i created from a structure. When I attempt to do this I get a message stating "Failed to compare two elements in the array". ...
5
by: runsun | last post by:
Thanks in advance. This program is written in C. It needs to read all characters from a file; then write them into a 3D array (yes, 3D!). The file is a .prn file (one of the Excel types), which...
0
by: BobbyS | last post by:
I've a xml file.I'm taking its data passing it to a method in string form and converting that string "xmlData" to a array of bytes by Convert.FromBase64String(xmlData) but I'm getting a exception...
1
by: Amzul | last post by:
hello all, i have a roblam that need some new view. i have a file that contain ip | country what i am tring to do is to know how many ips i have from each country. the file looks like...
3
by: wesley1970 | last post by:
<?php $a = array(1,2,3,4,5); $b = array(1,2,3,4,5,6,7,8); echo array_intersect($a, $b); ?> array_intersect should return the numbers of items intersect in 2 arrays. In this case, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.