Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8976 > unrolled thread

comparing arrays

Started bybill <william@TechServSys.com>
First post2013-04-07 07:34 -0400
Last post2013-04-07 15:59 +0100
Articles 14 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  comparing arrays bill <william@TechServSys.com> - 2013-04-07 07:34 -0400
    Re: comparing arrays Jerry Stuckle <jstucklex@attglobal.net> - 2013-04-07 08:34 -0400
      Re: comparing arrays bill <william@TechServSys.com> - 2013-04-07 10:00 -0400
        Re: comparing arrays Frank Steinmetzger <frank.steinmetzger@stud.tu-ilmenau.de> - 2013-04-07 16:14 +0200
          Re: comparing arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-07 17:13 +0200
          Re: comparing arrays bill <william@TechServSys.com> - 2013-04-07 11:34 -0400
            Re: comparing arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-07 21:33 +0200
        Re: comparing arrays The Natural Philosopher <tnp@invalid.invalid> - 2013-04-07 16:01 +0100
          Re: comparing arrays Frank Steinmetzger <frank.steinmetzger@stud.tu-ilmenau.de> - 2013-04-07 20:22 +0200
        Re: comparing arrays Jerry Stuckle <jstucklex@attglobal.net> - 2013-04-07 14:18 -0400
    Re: comparing arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-07 15:37 +0200
      Re: comparing arrays bill <william@TechServSys.com> - 2013-04-07 09:59 -0400
        Re: comparing arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-04-07 17:14 +0200
    Re: comparing arrays The Natural Philosopher <tnp@invalid.invalid> - 2013-04-07 15:59 +0100

#8976 — comparing arrays

Frombill <william@TechServSys.com>
Date2013-04-07 07:34 -0400
Subjectcomparing arrays
Message-ID<kjrlk6$8es$1@speranza.aioe.org>
I need to see if a patient has appointments with a 
psychotherapist and a physician on the same day.

I request the schedule of each therapist and each physician as 
arrays of arrays:
time, patient_number, duration (note, the therapist/physician id 
is not in the array)
They are non-associative arrays (numeric keys)
I might have 1..n physicians and 1..n therapists
I can generate arrays of physicians and arrays of therapists

Option 1 is a nightmare of nested foreachs

I am sure there must be an array function that will permit this 
check.  AFAIK, the _intersect functions require the same date in 
all, when what I want is "any".

my current thought is to create arrays of just pt_number, merge 
the physician arrays, merge the therapist arrays, array_flip to 
move the values to keys and then array_intersect.

But, before I try that I welcome any suggestions.

bill

[toc] | [next] | [standalone]


#8979

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-04-07 08:34 -0400
Message-ID<kjrp0m$ote$1@dont-email.me>
In reply to#8976
On 4/7/2013 7:34 AM, bill wrote:
> I need to see if a patient has appointments with a psychotherapist and a
> physician on the same day.
>
> I request the schedule of each therapist and each physician as arrays of
> arrays:
> time, patient_number, duration (note, the therapist/physician id is not
> in the array)
> They are non-associative arrays (numeric keys)
> I might have 1..n physicians and 1..n therapists
> I can generate arrays of physicians and arrays of therapists
>
> Option 1 is a nightmare of nested foreachs
>
> I am sure there must be an array function that will permit this check.
> AFAIK, the _intersect functions require the same date in all, when what
> I want is "any".
>
> my current thought is to create arrays of just pt_number, merge the
> physician arrays, merge the therapist arrays, array_flip to move the
> values to keys and then array_intersect.
>
> But, before I try that I welcome any suggestions.
>
> bill

Does the information come from a SQL database?  If so, I'd do it in SQL.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8985

Frombill <william@TechServSys.com>
Date2013-04-07 10:00 -0400
Message-ID<kjru58$19d$1@speranza.aioe.org>
In reply to#8979
On 4/7/2013 8:34 AM, Jerry Stuckle wrote:
> On 4/7/2013 7:34 AM, bill wrote:
>> I need to see if a patient has appointments with a
>> psychotherapist and a
>> physician on the same day.
>>
>> I request the schedule of each therapist and each physician as
>> arrays of
>> arrays:
>> time, patient_number, duration (note, the therapist/physician
>> id is not
>> in the array)
>> They are non-associative arrays (numeric keys)
>> I might have 1..n physicians and 1..n therapists
>> I can generate arrays of physicians and arrays of therapists
>>
>> Option 1 is a nightmare of nested foreachs
>>
>> I am sure there must be an array function that will permit this
>> check.
>> AFAIK, the _intersect functions require the same date in all,
>> when what
>> I want is "any".
>>
>> my current thought is to create arrays of just pt_number, merge
>> the
>> physician arrays, merge the therapist arrays, array_flip to
>> move the
>> values to keys and then array_intersect.
>>
>> But, before I try that I welcome any suggestions.
>>
>> bill
>
> Does the information come from a SQL database?  If so, I'd do it
> in SQL.
>

Yes it does, 3 tables: users (therapist/physician), pt_info, 
schedule.

A little more of a hint please.

bill

[toc] | [prev] | [next] | [standalone]


#8987

FromFrank Steinmetzger <frank.steinmetzger@stud.tu-ilmenau.de>
Date2013-04-07 16:14 +0200
Message-ID<kjrv05$5lq$2@inn-newsserver.rz.tu-ilmenau.de>
In reply to#8985
bill wrote:

> On 4/7/2013 8:34 AM, Jerry Stuckle wrote:
>> On 4/7/2013 7:34 AM, bill wrote:
>>> I need to see if a patient has appointments with a psychotherapist
>>> and a physician on the same day.
>>>
>>> I request the schedule of each therapist and each physician as
>>> arrays of arrays:
>>> time, patient_number, duration (note, the therapist/physician
>>> id is not in the array)
>>> They are non-associative arrays (numeric keys)
>>> I might have 1..n physicians and 1..n therapists
>>> I can generate arrays of physicians and arrays of therapists
>>> […]
>>>
>> Does the information come from a SQL database?  If so, I'd do it
>> in SQL.
>>
> 
> Yes it does, 3 tables: users (therapist/physician), pt_info,
> schedule.
> 
> A little more of a hint please.

You select two different appointments that have the same date:

SELECT s1.patient_number AS pn, s1.time AS t1, s2.time AS t2,
	s1.duration AS d1, s2.duration AS d2
FROM schedule s1, schedule s2
WHERE s1.time=s2.time AND s1.time=<today's date>
	AND s1.patient_number=s2.patient_number
	AND s1.patient_number=<ID of patient of interest>
	AND s1.id!=s2.id
	AND s1.doctor_number!=s2.doctor_number

Of course, those times don't work that way, b/c they have date *and* time, 
and you want to deal with date only. I'm not into advanced SQL, perhaps there 
is an SQL function to only extract and use the date part.

You then would need to PHP-fiddle the time and duration together for only 
those appointments that are today in a nested for-loop.
-- 
Gruß | Greetings | Qapla’
Please do not share anything from, with or about me with any Facebook 
service.

Dyslexics of the world, untie!

[toc] | [prev] | [next] | [standalone]


#8994

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-07 17:13 +0200
Message-ID<7617460.I37VCjhOAZ@PointedEars.de>
In reply to#8987
Frank Steinmetzger wrote:

> bill wrote:
>> On 4/7/2013 8:34 AM, Jerry Stuckle wrote:
>>> On 4/7/2013 7:34 AM, bill wrote:
>>>> I need to see if a patient has appointments with a psychotherapist
>>>> and a physician on the same day.
>>>>
>>>> I request the schedule of each therapist and each physician as
>>>> arrays of arrays:
>>>> time, patient_number, duration (note, the therapist/physician
>>>> id is not in the array)
>>>> They are non-associative arrays (numeric keys)
>>>> I might have 1..n physicians and 1..n therapists
>>>> I can generate arrays of physicians and arrays of therapists
>>>> […]
>>>>
>>> Does the information come from a SQL database?  If so, I'd do it
>>> in SQL.
>> 
>> Yes it does, 3 tables: users (therapist/physician), pt_info,
>> schedule.
>> 
>> A little more of a hint please.
> 
> You select two different appointments that have the same date:
> 
> SELECT s1.patient_number AS pn, s1.time AS t1, s2.time AS t2,
> s1.duration AS d1, s2.duration AS d2
> FROM schedule s1, schedule s2
> WHERE s1.time=s2.time AND s1.time=<today's date>
> AND s1.patient_number=s2.patient_number
> AND s1.patient_number=<ID of patient of interest>
> AND s1.id!=s2.id
> AND s1.doctor_number!=s2.doctor_number

Please don't.  If each schedule table has as little as 100 records, there 
will be 10'000 result records to filter from.  Read on JOIN to avoid 
Cartesian products like this.

> Of course, those times don't work that way, b/c they have date *and* time,
> and you want to deal with date only. I'm not into advanced SQL,

Apparently you are not into SQL, or relational databases for that matter, at 
all. 

> perhaps there is an SQL function to only extract and use the date part.

QED.
 
> You then would need to PHP-fiddle the time and duration together for only
> those appointments that are today in a nested for-loop.

No.  Apparently you are also not into PHP.

Please refrain from suggesting “solutions” in areas where you do not have a 
(minimum) clue.  TIA.


PointedEars
-- 
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
  -- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

[toc] | [prev] | [next] | [standalone]


#8996

Frombill <william@TechServSys.com>
Date2013-04-07 11:34 -0400
Message-ID<kjs3lm$gmi$1@speranza.aioe.org>
In reply to#8987
On 4/7/2013 10:14 AM, Frank Steinmetzger wrote:
> bill wrote:
>
>> On 4/7/2013 8:34 AM, Jerry Stuckle wrote:
>>> On 4/7/2013 7:34 AM, bill wrote:
>>>> I need to see if a patient has appointments with a psychotherapist
>>>> and a physician on the same day.
>>>>
>>>> I request the schedule of each therapist and each physician as
>>>> arrays of arrays:
>>>> time, patient_number, duration (note, the therapist/physician
>>>> id is not in the array)
>>>> They are non-associative arrays (numeric keys)
>>>> I might have 1..n physicians and 1..n therapists
>>>> I can generate arrays of physicians and arrays of therapists
>>>> […]
>>>>
>>> Does the information come from a SQL database?  If so, I'd do it
>>> in SQL.
>>>
>>
>> Yes it does, 3 tables: users (therapist/physician), pt_info,
>> schedule.
>>
>> A little more of a hint please.
>
> You select two different appointments that have the same date:
>
> SELECT s1.patient_number AS pn, s1.time AS t1, s2.time AS t2,
> 	s1.duration AS d1, s2.duration AS d2
> FROM schedule s1, schedule s2
> WHERE s1.time=s2.time AND s1.time=<today's date>
> 	AND s1.patient_number=s2.patient_number
> 	AND s1.patient_number=<ID of patient of interest>
> 	AND s1.id!=s2.id
> 	AND s1.doctor_number!=s2.doctor_number
>
> Of course, those times don't work that way, b/c they have date *and* time,
> and you want to deal with date only. I'm not into advanced SQL, perhaps there
> is an SQL function to only extract and use the date part.

there is
>
> You then would need to PHP-fiddle the time and duration together for only
> those appointments that are today in a nested for-loop.
select s1.user as s1user,
  s2.user as s2user,
  s1.apptTime as s1apptTime,
  s2.apptTime as s2pptTime,
users.physician,
users.user_id
from schedule as s1 join schedule as s2 join users
on  (s1.patient_number = s2.patient_number
and users.user_id = s2.user)

where s1.patient_number = s2.patient_number
  and s1.appt_status = 'a'
  and s2.appt_status = 'a'
and s1.patient_number = '5214'
and s1.schedule_id != s2.schedule_id
and physician = '1'
and date(s1.apptTime) = date(s2.apptTime)
and date(s1.apptTime) = '2013-04-03'

did the job

Thanks for the great suggestion.

bill

[toc] | [prev] | [next] | [standalone]


#9002

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-07 21:33 +0200
Message-ID<4479633.DZxCAXMi2a@PointedEars.de>
In reply to#8996
bill wrote:

> On 4/7/2013 10:14 AM, Frank Steinmetzger wrote:
>> You select two different appointments that have the same date:
>>
>> SELECT s1.patient_number AS pn, s1.time AS t1, s2.time AS t2,
>> s1.duration AS d1, s2.duration AS d2
>> FROM schedule s1, schedule s2
>> WHERE s1.time=s2.time AND s1.time=<today's date>
>> AND s1.patient_number=s2.patient_number
>> AND s1.patient_number=<ID of patient of interest>
>> AND s1.id!=s2.id
>> AND s1.doctor_number!=s2.doctor_number
>>
>> Of course, those times don't work that way, b/c they have date *and*
>> time, and you want to deal with date only. I'm not into advanced SQL,
>> perhaps there is an SQL function to only extract and use the date part.
> 
> there is
>>
>> You then would need to PHP-fiddle the time and duration together for only
>> those appointments that are today in a nested for-loop.
> select s1.user as s1user,
>   s2.user as s2user,
>   s1.apptTime as s1apptTime,
>   s2.apptTime as s2pptTime,
> users.physician,
> users.user_id
> from schedule as s1 join schedule as s2 join users
> on  (s1.patient_number = s2.patient_number
> and users.user_id = s2.user)
> 
> where s1.patient_number = s2.patient_number
>   and s1.appt_status = 'a'
>   and s2.appt_status = 'a'
> and s1.patient_number = '5214'
> and s1.schedule_id != s2.schedule_id
> and physician = '1'
> and date(s1.apptTime) = date(s2.apptTime)
> and date(s1.apptTime) = '2013-04-03'
> 
> did the job

  SELECT s1.user AS s1user,
    s2.user AS s2user,
    s1.apptTime AS s1apptTime,
    s2.apptTime AS s2pptTime,
    users.physician,
    users.user_id
  FROM
   (schedule AS s1 INNER JOIN schedule AS s2
     ON (s1.patient_number = s2.patient_number
         AND s1.appt_status = s2.appt_status)) AS tmp
   INNER JOIN users
     ON (users.user_id = tmp.user)
  WHERE s1.appt_status = 'a'
    AND s1.patient_number = 5214
    AND s1.schedule_id != s2.schedule_id
    AND physician = 1
    AND DATE(s1.apptTime) = DATE(s2.apptTime)
    AND DATE(s1.apptTime) = '2013-04-03'

probably does it better.

- Write queries so that you can tell names, keywords and function
  calls apart.  It is customary to write keywords and function
  identifiers all-uppercase, and keep names either all-lowercase
  (e.g., in MySQL and SQLite) or CamelCase (e.g., in MS SQL).

- Quote all names to avoid them being considered keywords, causing
  a syntax error.  (Since the SQL dialect is unknown, no recommendation
  can be made how to quote.)

- Write explicitly what kind of JOIN it is.

- Add a condition for each single JOIN, so that the result set the next
  table needs to be joined with is smaller.

- If you JOIN tables on values they have in common, it makes
  no sense to filter for them again.

- JOIN on common values, filter (WHERE) on different ones, so that the
  result set that needs to be filtered is smaller.

- Your keys are probably numbers.  Do not write them as strings, then.

- You almost never want to write a query in PHP like this; see
  “SQL injection”.

Further optimizations (like USING) are possible with MySQL (on-topic in 
comp.databases.mysql).

> Thanks for the great suggestion.

You are welcome.


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [next] | [standalone]


#8990

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-04-07 16:01 +0100
Message-ID<kjs1o1$odl$2@news.albasani.net>
In reply to#8985
On 07/04/13 15:13, Frank Steinmetzger wrote:
> bill wrote:
>
>> On 4/7/2013 8:34 AM, Jerry Stuckle wrote:
>>> On 4/7/2013 7:34 AM, bill wrote:
>>>> I need to see if a patient has appointments with a psychotherapist
>>>> and a physician on the same day.
>>>>
>>>> I request the schedule of each therapist and each physician as
>>>> arrays of arrays:
>>>> time, patient_number, duration (note, the therapist/physician
>>>> id is not in the array)
>>>> They are non-associative arrays (numeric keys)
>>>> I might have 1..n physicians and 1..n therapists
>>>> I can generate arrays of physicians and arrays of therapists
>>>> […]
>>>>
>>> Does the information come from a SQL database?  If so, I'd do it
>>> in SQL.
>>>
>>
>> Yes it does, 3 tables: users (therapist/physician), pt_info,
>> schedule.
>>
>> A little more of a hint please.
>
> You select two different appointments that have the same date:
>
> SELECT s1.patient_number AS pn, s1.time AS t1, s2.time AS t2,
> 	s1.duration AS d1, s2.duration AS d2
> WHERE s1.time=s2.time AND s1.time=<today's date>
> 	AND s1.patient_number=s2.patient_number
> 	AND s1.patient_number=<ID of patient of interest>
> 	AND s1.id!=s2.id
> 	AND s1.doctor_number!=s2.doctor_number
>
> Of course, those times don't work that way, b/c they have date *and* time,
> and you want to deal with date only. I'm not into advanced SQL, perhaps there
> is an SQL function to only extract and use the date part.
>

There is. date(time) will extract the date alone

> You then would need to PHP-fiddle the time and duration together for only
> those appointments that are today in a nested for-loop.
>
why not elect only 'todays' date in the first place?


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#9001

FromFrank Steinmetzger <frank.steinmetzger@stud.tu-ilmenau.de>
Date2013-04-07 20:22 +0200
Message-ID<kjsdht$dqv$1@inn-newsserver.rz.tu-ilmenau.de>
In reply to#8990
The Natural Philosopher wrote:

>> You then would need to PHP-fiddle the time and duration together for only
>> those appointments that are today in a nested for-loop.
>>
> why not elect only 'todays' date in the first place?

That's what I meant (well, implicitly). With the “only” I was referring to 
the list of appointments of “only today”, which had been extracted with the 
date-extracting funtion I didn't know about.
-- 
Gruß | Greetings | Qapla’
Please do not share anything from, with or about me with any Facebook 
service.

The government has to make cuts, now 20 ministers have to share one brain.

[toc] | [prev] | [next] | [standalone]


#9000

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-04-07 14:18 -0400
Message-ID<kjsd5q$g29$2@dont-email.me>
In reply to#8985
On 4/7/2013 10:00 AM, bill wrote:
> On 4/7/2013 8:34 AM, Jerry Stuckle wrote:
>> On 4/7/2013 7:34 AM, bill wrote:
>>> I need to see if a patient has appointments with a
>>> psychotherapist and a
>>> physician on the same day.
>>>
>>> I request the schedule of each therapist and each physician as
>>> arrays of
>>> arrays:
>>> time, patient_number, duration (note, the therapist/physician
>>> id is not
>>> in the array)
>>> They are non-associative arrays (numeric keys)
>>> I might have 1..n physicians and 1..n therapists
>>> I can generate arrays of physicians and arrays of therapists
>>>
>>> Option 1 is a nightmare of nested foreachs
>>>
>>> I am sure there must be an array function that will permit this
>>> check.
>>> AFAIK, the _intersect functions require the same date in all,
>>> when what
>>> I want is "any".
>>>
>>> my current thought is to create arrays of just pt_number, merge
>>> the
>>> physician arrays, merge the therapist arrays, array_flip to
>>> move the
>>> values to keys and then array_intersect.
>>>
>>> But, before I try that I welcome any suggestions.
>>>
>>> bill
>>
>> Does the information come from a SQL database?  If so, I'd do it
>> in SQL.
>>
>
> Yes it does, 3 tables: users (therapist/physician), pt_info, schedule.
>
> A little more of a hint please.
>
> bill

Try a newsgroup related to the SQL database you're using for help on 
creating the correct SQL statement(s).

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#8983

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-07 15:37 +0200
Message-ID<5107815.Hu20eYApaJ@PointedEars.de>
In reply to#8976
bill wrote:

> I need to see if a patient has appointments with a
> psychotherapist and a physician on the same day.
> 
> I request the schedule of each therapist and each physician as
> arrays of arrays:
> time, patient_number, duration (note, the therapist/physician id
> is not in the array)
> They are non-associative arrays (numeric keys)
> I might have 1..n physicians and 1..n therapists
> I can generate arrays of physicians and arrays of therapists
> 
> Option 1 is a nightmare of nested foreachs
> 
> I am sure there must be an array function that will permit this
> check.

I am quite sure there isn't.

> AFAIK, the _intersect functions require the same date in
> all, when what I want is "any".
> 
> my current thought is to create arrays of just pt_number, merge
> the physician arrays, merge the therapist arrays, array_flip to
> move the values to keys and then array_intersect.

A merge will destroy the association between patient, date, and doctor.
 
> But, before I try that I welcome any suggestions.

You should probably do this in a relational database.   But if you 
absolutely must do it in PHP, you can determine what the keys of your data 
are, and in which order you need them: the patient, the date, or the doctor.  

Then you can build from the records a “multi-dimensional array” (for lack of 
a better word) from which becomes apparent where there are schedule 
collisions.  You can take advantage of the fact that PHP builds “multi-
dimensional arrays” automatically if you add another index expression.

For example, in a loop,

  $appointments[$patient_id][$date] = $doctor;

would store the information that the patient with ID $patient_id has an 
appointment with $doctor on $date.  Obviously it is very easy then to 
organize the data by different primary keys in different “multi-dimensional 
arrays” (which themselves may be elements of yet another array, to avoid 
extra variables).  If an element of an array with an index already exists, 
there is the collision.


HTH

PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [next] | [standalone]


#8984

Frombill <william@TechServSys.com>
Date2013-04-07 09:59 -0400
Message-ID<kjru44$10b$1@speranza.aioe.org>
In reply to#8983
On 4/7/2013 9:37 AM, Thomas 'PointedEars' Lahn wrote:
> bill wrote:
>
>> I need to see if a patient has appointments with a
>> psychotherapist and a physician on the same day.
>>
>> I request the schedule of each therapist and each physician as
>> arrays of arrays:
>> time, patient_number, duration (note, the therapist/physician id
>> is not in the array)
>> They are non-associative arrays (numeric keys)
>> I might have 1..n physicians and 1..n therapists
>> I can generate arrays of physicians and arrays of therapists
>>
>> Option 1 is a nightmare of nested foreachs
>>
>> I am sure there must be an array function that will permit this
>> check.
>
> I am quite sure there isn't.
>
>> AFAIK, the _intersect functions require the same date in
>> all, when what I want is "any".
>>
>> my current thought is to create arrays of just pt_number, merge
>> the physician arrays, merge the therapist arrays, array_flip to
>> move the values to keys and then array_intersect.
>
> A merge will destroy the association between patient, date, and doctor.
>
>> But, before I try that I welcome any suggestions.
>
> You should probably do this in a relational database.   But if you
> absolutely must do it in PHP, you can determine what the keys of your data
> are, and in which order you need them: the patient, the date, or the doctor.
>
> Then you can build from the records a “multi-dimensional array” (for lack of
> a better word) from which becomes apparent where there are schedule
> collisions.  You can take advantage of the fact that PHP builds “multi-
> dimensional arrays” automatically if you add another index expression.
>
> For example, in a loop,
>
>    $appointments[$patient_id][$date] = $doctor;
>
> would store the information that the patient with ID $patient_id has an
> appointment with $doctor on $date.  Obviously it is very easy then to
> organize the data by different primary keys in different “multi-dimensional
> arrays” (which themselves may be elements of yet another array, to avoid
> extra variables).  If an element of an array with an index already exists,
> there is the collision.
>
>
> HTH
It is actually slightly simpler as I only need to check for 
"today".  But is more complex in that I have two classes for 
"doctor:" therapist or physician.

By doing it in a db are you suggesting a temporary table, or 
using the tables I have ?

bill

[toc] | [prev] | [next] | [standalone]


#8995

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2013-04-07 17:14 +0200
Message-ID<19819803.o9xVrzyPd8@PointedEars.de>
In reply to#8984
bill wrote:

> On 4/7/2013 9:37 AM, Thomas 'PointedEars' Lahn wrote:
>> bill wrote:
>>> I need to see if a patient has appointments with a
>>> psychotherapist and a physician on the same day.
>>>
>>> I request the schedule of each therapist and each physician as
>>> arrays of arrays:
>>> time, patient_number, duration (note, the therapist/physician id
>>> is not in the array)
>>> They are non-associative arrays (numeric keys)
>>> I might have 1..n physicians and 1..n therapists
>>> I can generate arrays of physicians and arrays of therapists
>>> […]
>> 
>> You should probably do this in a relational database.   But if you
>> absolutely must do it in PHP, you can determine what the keys of your
>> data are, and in which order you need them: the patient, the date, or the
>> doctor.
>>
>> Then you can build from the records a “multi-dimensional array” (for lack
>> of a better word) from which becomes apparent where there are schedule
>> collisions.  You can take advantage of the fact that PHP builds “multi-
>> dimensional arrays” automatically if you add another index expression.
>>
>> For example, in a loop,
>>
>>    $appointments[$patient_id][$date] = $doctor;
>>
>> would store the information that the patient with ID $patient_id has an
>> appointment with $doctor on $date.  Obviously it is very easy then to
>> organize the data by different primary keys in different
>> “multi-dimensional arrays” (which themselves may be elements of yet
>> another array, to avoid
>> extra variables).  If an element of an array with an index already
>> exists, there is the collision.
> 
> It is actually slightly simpler as I only need to check for
> "today".  But is more complex in that I have two classes for
> "doctor:" therapist or physician.

Am I to understand that you need to consider only one patient, only one 
date, and cannot figure out how to filter your array of arrays to see that 
there are different kinds of doctors involved using these criteria?
 
> By doing it in a db are you suggesting a temporary table, or
> using the tables I have ?

The latter, sort of.  You would JOIN the tables you have and apply 
aggregation functions on the result (certainly not as Frank Steinmetzger 
suggested).  If you would need to do this often, it would be a good idea to 
store the result in a TEMPORARY TABLE, if your DBMS supports that, or in 
general in a so-called “flat table”.

People have also devised and implemented a rather elegant way of querying 
data without a database – Language-INtegrated Query (LINQ) – that you also 
might find useful. [1]

Please trim your quotes.


PointedEars
___________
[1] <http://stackoverflow.com/questions/5792388/is-there-something-in-php-
equivalent-to-linq-in-c>
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

[toc] | [prev] | [next] | [standalone]


#8989

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-04-07 15:59 +0100
Message-ID<kjs1kv$odl$1@news.albasani.net>
In reply to#8976
On 07/04/13 12:34, bill wrote:
> I need to see if a patient has appointments with a psychotherapist and a
> physician on the same day.
>
> I request the schedule of each therapist and each physician as arrays of
> arrays:
> time, patient_number, duration (note, the therapist/physician id is not
> in the array)
> They are non-associative arrays (numeric keys)
> I might have 1..n physicians and 1..n therapists
> I can generate arrays of physicians and arrays of therapists
>
> Option 1 is a nightmare of nested foreachs
>
> I am sure there must be an array function that will permit this check.
> AFAIK, the _intersect functions require the same date in all, when what
> I want is "any".
>
> my current thought is to create arrays of just pt_number, merge the
> physician arrays, merge the therapist arrays, array_flip to move the
> values to keys and then array_intersect.
>
> But, before I try that I welcome any suggestions.
>
> bill
You need a better SQL statement to select patients and dates where this 
condition exists. Don't do this in PHP, do it in Mysql.

Sadly my SQL is only good enough to know that this *can* be done, and 
simply. It would take me hours to get the SQL query right, but 
comp.databases.mysql is that way==>

Assuming you are in fact querying a database to get your arrays.

If not you bloody well should be :-)

-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web