473,387 Members | 1,516 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,387 software developers and data experts.

finding data in an array

Hi Folk

I have the following function:

function getdata($sql) {
$result = @mysql_query($sql) or die("Error: " . mysql_error()." sql was
".$sql);
$ret = array();
while($row = mysql_fetch_assoc($result)) {
$ret[] = $row;
}
mysql_free_result($result);
return $ret;
}

I love it, because all I need to do is to stick in some sql (e.g. Select *
from clients) and away she goes like this

$sql = "SELECT * FROM CLIENTS;"
$data = getdata($sql)
foreach($data as $ds) {
echo '<p>'. $ds["first_name"].' '.$ds["first_name"].'</p>';
}

Now, what I want to do is check the fields for all $ds arrays. That is, I
want to check one particular field for all the "rows" and check that it is
identical.

What is the most efficient way to do this? Is there a function that I can
use? Something like

function datafieldcheck($ma, $fld) {
//$ma = a multidimensional array from the getdata function
//$fld = the field to check
foreach($multiarray as $ds) {
$v = $ds[$fld];
if(!$first) {
if ($v != $vex) {
return false;
}
}
$vex = $v;
$first = 1;
}
return true;
}
Jan 14 '06 #1
3 1206
windandwaves wrote:
What is the most efficient way to do this? Is there a function that I
can use? Something like


AFAIK, there isn't an out-of-the-box function for this, so all I can offer
you is an alternative to what you have posted:

function datafieldcheck($ma, $fld) {
$current = current($ma);
if (isset($current[$fld])) {
$v = $current[$fld];
while (false !== ($current = next($ma))) {
if (!isset($current[$fld]) || strcmp($v, $current[$fld])) {
return false;
}
}
return true;
}
return false;
}
JW
Jan 14 '06 #2
On Sat, 14 Jan 2006 22:24:24 +1300, windandwaves wrote:
What is the most efficient way to do this?


To do it with the SQL. If you want to check one particular field
in your table, you would write your SQL as

$sql="select * from some_table where
one_particular_field='PARTICULAR_VALUE'";

The main rule of writing a database application is: whatever you can
do in the database, do in the database. That's what you have the database
for: to provide some intelligence for the data retrieval. Why bother using
database when you're using it as a flat file?

--
http://www.mgogala.com

Jan 15 '06 #3
Can't think of a way to do this that's signicantly better. The
following examines all the cells and return the columns where all rows
contain identical values:

$same = call_user_func_array('array_intersect_assoc', $rows);

Jan 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
1
by: Tristan | last post by:
Im trying to expand a search util by uing regular expression to allow common search criteria such as +-* and phrases "". My understanding of ereg(string pattern, string string, ) is that the...
6
by: biner | last post by:
Hello, I am using a program that has to read binary data from files coming from different machines. The file are always written with big endian. I am using the struct module to read the data and...
5
by: Manish | last post by:
This is the print_r() for a variable $categories. $categories :: Array ( =Array ( =Array (
22
by: jobo | last post by:
Hello, I'm very new to C so please forgive my ineptitude. If I am given a file with "jfewuuj3uefi8jkw128jdmnsdf\s;'d1904" I want to capture each occurence of an integer 0-9 into an array. ...
6
by: sajohn | last post by:
Hi A separte application is sending me a pointer to some binary data and I need to find the size of the binary data being passed to me. Does anyone know I can go about doing this? TIA
1
by: helraizer1 | last post by:
Hi all, I have a dynamic image that picks out data from a dynamically created .line file. showimage.php(5) <?php include("linesfile.php5"); $linesDataFile = new DataFile("data.line");
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.