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


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

Looking for NULL in arrays

Started byDerek Turner <frderek@suremail.je>
First post2015-10-25 16:05 +0000
Last post2015-10-27 22:23 +0100
Articles 15 — 6 participants

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


Contents

  Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-25 16:05 +0000
    Re: Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-25 16:11 +0000
      Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-25 19:07 +0100
    Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-25 19:06 +0100
      Re: Looking for NULL in arrays Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2015-10-25 21:01 +0100
        Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-25 21:58 +0100
    Re: Looking for NULL in arrays Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-25 16:33 -0400
      Re: Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-26 09:42 +0000
        Re: Looking for NULL in arrays Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-26 09:45 -0400
          Re: Looking for NULL in arrays Derek Turner <frderek@suremail.je> - 2015-10-26 13:57 +0000
            Re: Looking for NULL in arrays Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-10-26 17:41 +0100
            Re: Looking for NULL in arrays Jerry Stuckle <jstucklex@attglobal.net> - 2015-10-26 12:49 -0400
        Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-26 22:25 +0100
    Re: Looking for NULL in arrays Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-27 03:16 +0000
      Re: Looking for NULL in arrays Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-27 22:23 +0100

#15779 — Looking for NULL in arrays

FromDerek Turner <frderek@suremail.je>
Date2015-10-25 16:05 +0000
SubjectLooking for NULL in arrays
Message-ID<d94ctvFn9f4U1@mid.individual.net>
The problem is not that I don't know how to iterate through an array - 
the problem is that there are so many ways and I can't see the wood for 
the trees -which is the easiest method?

I have a simple array called $weeks. At the moment it contains either 
three string values or 5 could easily make it a consistent five if 
necessary.

$weeks[1] always contains a string. [2], [3] etc. either contain a string 
or NULL from mysql query.

I want to iterate through in key order until either [5] or a NULL value 
is found. What would do that, please? Is there a simple way without 
having to use an 'if' or similar test? I'm thinking something similar to

while ($row = $result->fetch_asoc());

that will detect a NULL value and stop. IYSWIM.

[toc] | [next] | [standalone]


#15780

FromDerek Turner <frderek@suremail.je>
Date2015-10-25 16:11 +0000
Message-ID<d94d9cFn9f4U2@mid.individual.net>
In reply to#15779
On Sun, 25 Oct 2015 16:05:19 +0000, Derek Turner wrote:

> $weeks[1] always contains a string. [2], [3] etc. either contain a
> string or NULL from mysql query.

sorry, $weeks[0], of course <blush> - thinko.

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


#15782

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-10-25 19:07 +0100
Message-ID<6587090.GmHYCo8pym@PointedEars.de>
In reply to#15780
Derek Turner wrote:

> On Sun, 25 Oct 2015 16:05:19 +0000, Derek Turner wrote:
>> $weeks[1] always contains a string. [2], [3] etc. either contain a
>> string or NULL from mysql query.
> 
> sorry, $weeks[0], of course <blush> - thinko.

Do you see the problem now?

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#15781

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-10-25 19:06 +0100
Message-ID<1854724.EiBtr6myrt@PointedEars.de>
In reply to#15779
Derek Turner wrote:

> The problem is not that I don't know how to iterate through an array -
> the problem is that there are so many ways and I can't see the wood for
> the trees -which is the easiest method?

foreach

> I have a simple array called $weeks. At the moment it contains either
> three string values or 5 could easily make it a consistent five if
> necessary.

Your sentences are incoherent.  You cannot just be posting as you are just 
thinking; you have to make an effort to put your thoughts in proper words.  
In doing that, you will learn to think clearly, maybe even arrive at the 
solution yourself.
 
> $weeks[1] always contains a string. [2], [3] etc. either contain a string
> or NULL from mysql query.
> 
> I want to iterate through in key order until either [5] or a NULL value
> is found. What would do that, please?

“foreach” over an array that contains the array keys in order.  RTFM.

> Is there a simple way without having to use an 'if' or similar test?

No.

> I'm thinking something similar to
> 
> while ($row = $result->fetch_asoc());
> 
> that will detect a NULL value and stop.

This will detect a false-value, *including* NULL, and then stop.

> IYSWIM.

Pardon?

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#15783

FromThomas Mlynarczyk <thomas@mlynarczyk-webdesign.de>
Date2015-10-25 21:01 +0100
Message-ID<n0jcf5$ieo$1@news.albasani.net>
In reply to#15781
On 25/10/15 19:06, Thomas 'PointedEars' Lahn wrote:
>> IYSWIM.
> Pardon?

Google. First result. Just as easy as finding the meanings of all the 
acronyms that *you* use all the time... if you see what I mean ;-)

-- 
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)

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


#15785

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-10-25 21:58 +0100
Message-ID<1757224.JshPPPhi2X@PointedEars.de>
In reply to#15783
Thomas Mlynarczyk wrote:

> On 25/10/15 19:06, Thomas 'PointedEars' Lahn wrote:
>>> IYSWIM.
>> Pardon?
> 
> Google. First result. Just as easy as finding the meanings of all the
> acronyms that *you* use all the time... if you see what I mean ;-)

I do, thanks.
 
-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#15784

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-10-25 16:33 -0400
Message-ID<n0je5n$jqn$1@dont-email.me>
In reply to#15779
On 10/25/2015 12:05 PM, Derek Turner wrote:
> The problem is not that I don't know how to iterate through an array - 
> the problem is that there are so many ways and I can't see the wood for 
> the trees -which is the easiest method?
> 
> I have a simple array called $weeks. At the moment it contains either 
> three string values or 5 could easily make it a consistent five if 
> necessary.
> 
> $weeks[1] always contains a string. [2], [3] etc. either contain a string 
> or NULL from mysql query.
> 
> I want to iterate through in key order until either [5] or a NULL value 
> is found. What would do that, please? Is there a simple way without 
> having to use an 'if' or similar test? I'm thinking something similar to
> 
> while ($row = $result->fetch_asoc());
> 
> that will detect a NULL value and stop. IYSWIM.
> 

I guess my first question is why are you fetching multiple rows into an
array?  There are valid reasons for it, but most of the time you just
fetch a row and process it, then fetch the next row.

The next question is why can it contain either three or five string
values?  Are they coming from different tables, for instance?   If so, I
think you have a logic problem.  If not, why do some queries return 3
values and some 5?  I'm not asking about null because that very well
could be valid in either case.

It just sounds to me like you're mixing different types in the same
array, which will cause problems like you are having.  My suggestion
would be to look at how your code is structured.

However, to answer your question: it depends on what you need, and how
you're going to process it.  You don't supply enough information.
foreach() works well if you're always going to start with the beginning
element of the array and is probably the easiest to use.  It also works
no matter what your indicies are (strings, non-contiguous integer
values, etc).  However, if you ever need to skip elements, it becomes
more difficult.  A for() loop with indicies can start anywhere in the
array, but pretty much limits you to contiguous integer values.

For determining the type and value (if any) of the element, look at
empty(), is_set(), is_array() and count().

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

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


#15786

FromDerek Turner <frderek@suremail.je>
Date2015-10-26 09:42 +0000
Message-ID<d96arrF7egqU1@mid.individual.net>
In reply to#15784
On Sun, 25 Oct 2015 16:33:08 -0400, Jerry Stuckle wrote:

> I guess my first question is why are you fetching multiple rows into an
> array?  There are valid reasons for it, but most of the time you just
> fetch a row and process it, then fetch the next row.
> 
> The next question is why can it contain either three or five string
> values?

OK, thanks Jerry.

Probably best to say exactly what I'm trying to do, what I've found so 
far and post the VERY 'work in progress' experimental code which is 
running strictly on my local server :)

I'm doing a site for my local University of the Third Age. There are four 
working pages here (but no DNS pointing to them as yet)

http://www.thozhiyur.org.uk/u3a/

If you view the Groups page you will see what information I have stored 
about each group.

The task is to construct a diary of meetings for the next three months 
from that information,  not a trivial task. Playing with strtotime I've 
discovered that it's terribly flaky an unpredictable, mostly because it 
interprets "fifth Sunday of February" (for example) as '5th Sunday after 
Feb 1st' rather than throwing an error as one might naively expect.

So I've found by experimenting that the most reliable way to get the 
results I want is to supply a timestamp to strtotime to work from for 
each month's results. It needs to be the last day of the previous month 
to give reliable results.

The plan is to populate a temporary table of meetings which can then be 
SELECTed from and ORDERed BY.

I will need to iterate through the 4 offsets (last day of previous month) 
and through each of the string ordinals (3 or 5, may contains NULL values)

So here's the code so far:

//First get meeting information and perapare a table to receive it

$sql = "DROP TABLE IF EXISTS meetings ";
$drop = $mysqli->query($sql);

$sql = "CREATE TABLE meetings(groupName VARCHAR (50), meeting DATETIME);";
$create = $mysqli->query($sql);

$sql = "SELECT `name`,`weekday`,`time`,`ordinal1`,`ordinal2`,`ordinal3` 
FROM `groups` WHERE `active` AND `time`";
$result = $mysqli->query($sql);

//Set up offsets for the ordinals - need to be last day of previous month
$month = date("m");
$year = date("Y");
$first = "$year-$month-01";
$first = strtotime($first) - 86400;
$month = $month + 1;
if ($month > 12) {
    $year = $year + 1;
    $month = "01";
}
$second = "$year-$month-01";
$second = strtotime($second) - 86400;

$month = $month + 1;
if ($month > 12) {
    $year = $year + 1;
    $month = "01";
}

$third = "$year-$month-01";
$third = strtotime($third) - 86400;

$month = $month + 1;
if ($month > 12) {
    $year = $year + 1;
    $month = "01";
}

$fourth = "$year-$month-01";
$fourth = strtotime($fourth) - 86400;

$offset = array($first, $second, $third, $fourth);

//now iterate through the groups and set up weeks of meetings

while ($row = $result->fetch_assoc()):
    if ($row['ordinal1'] == "every") {
        $weeks = array("first", "second", "third", "fourth", "fifth");
    } else {
        $weeks = array($row['ordinal1'], $row['ordinal2'], $row
['ordinal3']);
    }

    echo $row['name'] . " meets on " . $row['weekday'] . "<br><pre>";
    print_r($weeks);
    echo "</pre><br>";
endwhile;

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


#15787

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-10-26 09:45 -0400
Message-ID<n0lalu$l63$1@dont-email.me>
In reply to#15786
On 10/26/2015 5:42 AM, Derek Turner wrote:
> On Sun, 25 Oct 2015 16:33:08 -0400, Jerry Stuckle wrote:
> 
>> I guess my first question is why are you fetching multiple rows into an
>> array?  There are valid reasons for it, but most of the time you just
>> fetch a row and process it, then fetch the next row.
>>
>> The next question is why can it contain either three or five string
>> values?
> 
> OK, thanks Jerry.
> 
> Probably best to say exactly what I'm trying to do, what I've found so 
> far and post the VERY 'work in progress' experimental code which is 
> running strictly on my local server :)
> 
> I'm doing a site for my local University of the Third Age. There are four 
> working pages here (but no DNS pointing to them as yet)
> 
> http://www.thozhiyur.org.uk/u3a/
> 
> If you view the Groups page you will see what information I have stored 
> about each group.
> 
> The task is to construct a diary of meetings for the next three months 
> from that information,  not a trivial task. Playing with strtotime I've 
> discovered that it's terribly flaky an unpredictable, mostly because it 
> interprets "fifth Sunday of February" (for example) as '5th Sunday after 
> Feb 1st' rather than throwing an error as one might naively expect.
> 
> So I've found by experimenting that the most reliable way to get the 
> results I want is to supply a timestamp to strtotime to work from for 
> each month's results. It needs to be the last day of the previous month 
> to give reliable results.
> 
> The plan is to populate a temporary table of meetings which can then be 
> SELECTed from and ORDERed BY.
> 
> I will need to iterate through the 4 offsets (last day of previous month) 
> and through each of the string ordinals (3 or 5, may contains NULL values)
> 
> So here's the code so far:
> 
> //First get meeting information and perapare a table to receive it
> 
> $sql = "DROP TABLE IF EXISTS meetings ";
> $drop = $mysqli->query($sql);
> 
> $sql = "CREATE TABLE meetings(groupName VARCHAR (50), meeting DATETIME);";
> $create = $mysqli->query($sql);
> 
> $sql = "SELECT `name`,`weekday`,`time`,`ordinal1`,`ordinal2`,`ordinal3` 
> FROM `groups` WHERE `active` AND `time`";
> $result = $mysqli->query($sql);
> 
> //Set up offsets for the ordinals - need to be last day of previous month
> $month = date("m");
> $year = date("Y");
> $first = "$year-$month-01";
> $first = strtotime($first) - 86400;
> $month = $month + 1;
> if ($month > 12) {
>     $year = $year + 1;
>     $month = "01";
> }
> $second = "$year-$month-01";
> $second = strtotime($second) - 86400;
> 
> $month = $month + 1;
> if ($month > 12) {
>     $year = $year + 1;
>     $month = "01";
> }
> 
> $third = "$year-$month-01";
> $third = strtotime($third) - 86400;
> 
> $month = $month + 1;
> if ($month > 12) {
>     $year = $year + 1;
>     $month = "01";
> }
> 
> $fourth = "$year-$month-01";
> $fourth = strtotime($fourth) - 86400;
> 
> $offset = array($first, $second, $third, $fourth);
> 
> //now iterate through the groups and set up weeks of meetings
> 
> while ($row = $result->fetch_assoc()):
>     if ($row['ordinal1'] == "every") {
>         $weeks = array("first", "second", "third", "fourth", "fifth");
>     } else {
>         $weeks = array($row['ordinal1'], $row['ordinal2'], $row
> ['ordinal3']);
>     }
> 
>     echo $row['name'] . " meets on " . $row['weekday'] . "<br><pre>";
>     print_r($weeks);
>     echo "</pre><br>";
> endwhile;
> 

As I told you before, it's not the way I would set up the database.
Proper normalization would be to add an additional table containing the
group id and the meeting details.  Your current design does not follow
1NF and is part of what's causing your problems.

And yes, strtotime() is pretty smart for what it does, but it doesn't
always provide a good answer.  I've used something I found in the notes
for strtotime() quite a while ago which seems to help clear up my code:

strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009

Of course, when you go to '+4 week sun feb 2016' you need to check the
month returned to see whether it's the fifth sunday or first sunday of
next month (a problem I didn't have).

That and proper database design makes the task fairly trivial.

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

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


#15788

FromDerek Turner <frderek@suremail.je>
Date2015-10-26 13:57 +0000
Message-ID<d96ppmFbp1nU1@mid.individual.net>
In reply to#15787
On Mon, 26 Oct 2015 09:45:48 -0400, Jerry Stuckle wrote:

> As I told you before, it's not the way I would set up the database.
> Proper normalization would be to add an additional table containing the
> group id and the meeting details

Sorry, but I can't see how that would help at all - it would be exactly 
the same information and just as awkward to process. But it's certainly 
not too late to do that, if you can convince me that it would make 
interpreting those details easier.

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


#15789

FromErwin Moller <erwinmollerusenet@xs4all.nl>
Date2015-10-26 17:41 +0100
Message-ID<562e57ce$0$23822$e4fe514c@news.xs4all.nl>
In reply to#15788
On 10/26/2015 2:57 PM, Derek Turner wrote:
> On Mon, 26 Oct 2015 09:45:48 -0400, Jerry Stuckle wrote:
>
>> As I told you before, it's not the way I would set up the database.
>> Proper normalization would be to add an additional table containing the
>> group id and the meeting details
>
> Sorry, but I can't see how that would help at all - it would be exactly
> the same information and just as awkward to process. But it's certainly
> not too late to do that, if you can convince me that it would make
> interpreting those details easier.
>

I didn't see your tables anywhere in this post, only 'meetings'.
Maybe you could post them?

I fully agree with Jerry that many problems disappear if you set up your 
database the right way.

Normalization and clear/logical references (Foreign keys) usually does 
the trick. :-)
Also, try to use a primary key when you create a table. They never hurt, 
and makes it easier for you to reference a certain row later.
(Yes, you will probably need that later in your project)
I noticed you didn't use one on your 'meetings' table.

Please post the relevant tables, and I am sure we can help you.

Regards,
Erwin Moller

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

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


#15790

FromJerry Stuckle <jstucklex@attglobal.net>
Date2015-10-26 12:49 -0400
Message-ID<n0llf3$59s$1@dont-email.me>
In reply to#15788
On 10/26/2015 9:57 AM, Derek Turner wrote:
> On Mon, 26 Oct 2015 09:45:48 -0400, Jerry Stuckle wrote:
> 
>> As I told you before, it's not the way I would set up the database.
>> Proper normalization would be to add an additional table containing the
>> group id and the meeting details
> 
> Sorry, but I can't see how that would help at all - it would be exactly 
> the same information and just as awkward to process. But it's certainly 
> not too late to do that, if you can convince me that it would make 
> interpreting those details easier.
> 

First of all, you need to learn about database normalization.  There are
a number of good tutorials on the web about it.  It's much more than can
be taught in a usenet post.

When you're done, you will get one row per meeting - not two or three
(or none) meetings per row.  It will also make searching easier.

Yes, it's the same information - but much easier to process than what
you're trying to do now.

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

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


#15791

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-10-26 22:25 +0100
Message-ID<2317665.9IWv2tE1in@PointedEars.de>
In reply to#15786
Derek Turner wrote:

> The task is to construct a diary of meetings for the next three months
> from that information,  not a trivial task. Playing with strtotime I've
> discovered that it's terribly flaky an unpredictable, mostly because it
> interprets "fifth Sunday of February" (for example) as '5th Sunday after
> Feb 1st' rather than throwing an error as one might naively expect.

I predicted that to you here a month ago.
 
> So I've found by experimenting that the most reliable way to get the
> results I want is to supply a timestamp to strtotime to work from for
> each month's results.

I still think it would be better to store “fifth Sunday of February” as the 
tuple (2[=February], 0[=Sunday], 5[=fifth occurrence]) or something like 
that.

> It needs to be the last day of the previous month to give reliable
> results.

I do not think so.
 
> The plan is to populate a temporary table of meetings which can then be
> SELECTed from and ORDERed BY.

OK.
 
> I will need to iterate through the 4 offsets (last day of previous month)
> and through each of the string ordinals (3 or 5, may contains NULL values)

At this point you should realize that your approach is too complicated, 
therefore probably wrong.  And in this case it is definitely wrong.
 
> […]
> $first = strtotime($first) - 86400;

Not every day in every time zone has 86'400 seconds.

MySQL and PHP have *date* functions, and PHP has a *Date*Time class for a 
reason.

For example, you can *calculate* the date of the last Sunday in April 2015 
this way:

php -r '

$mayTheFirst = new DateTime("2015-05-01");
$lastDayInApril = $mayTheFirst->modify("-1 day");
$lastSundayInApril = $lastDayInApril->modify(
  sprintf("-%d days", $lastDayInApril->format("w")));

var_dump($lastSundayInApril);

'
class DateTime#1 (3) {
  public $date =>
  string(26) "2015-04-26 00:00:00.000000"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(13) "Europe/Berlin"
} 

(This simple, unoptimized, undertested, off-the-top-of-my-hat solution works 
for Sundays only: If the last day in the month is a Sunday, you do not 
subtract from the date [you subtract 0, the weekday number for Sunday], 
otherwise you subtract from the date the number of weekdays to the previous 
Sunday.  For example, if the last day of the month is a Monday, it has 
weekday number 1, which is exactly the number of days to the previous 
Sunday.)

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#15792

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-10-27 03:16 +0000
Message-ID<n0mqaa$pnk$3@dont-email.me>
In reply to#15779
On Sun, 25 Oct 2015 16:05:19 +0000, Derek Turner wrote:

> that will detect a NULL value and stop.

Works with the test cases shown, but otherwise untested so no guarantees 
of any specific behaviour if you feed it garbage.

<?php

$a = array(
array("fred","jim","peter","susan","mary",0),
array("fred","jim","susan","mary",0),
array("fred","susan","mary",0),
array("fred","mary",0),
array("fred",0),
array("fred","jim","susan","mary",0,0),
array("fred","susan","mary",0,0,0),
array("fred","mary",0,0,0,0),
array("fred",0,0,0,0,0)
);

foreach ($a as $arr) {
  $idx = 0;
  while ($idx < count($arr)-1 && $arr[$idx] != NULL) $idx += 1;
  $valid = array_slice($arr,0,$idx);
  print_r($arr);
  print_r($valid);
}

-- 
Denis McMahon, denismfmcmahon@gmail.com

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


#15799

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-10-27 22:23 +0100
Message-ID<5273400.d3j8xVGaf9@PointedEars.de>
In reply to#15792
Denis McMahon wrote:

> On Sun, 25 Oct 2015 16:05:19 +0000, Derek Turner wrote:
>> that will detect a NULL value and stop.
                      ^^^^^^^^^^ 
> Works with the test cases shown, but otherwise untested so no guarantees
> of any specific behaviour if you feed it garbage.
> 
> <?php
> 
> $a = array(
> array("fred","jim","peter","susan","mary",0),
> array("fred","jim","susan","mary",0),
> array("fred","susan","mary",0),
> array("fred","mary",0),
> array("fred",0),
> array("fred","jim","susan","mary",0,0),
> array("fred","susan","mary",0,0,0),
> array("fred","mary",0,0,0,0),
> array("fred",0,0,0,0,0)
> );

Flawed testcase: There is not a single occurrence of “null” or “NULL” there.
 
> foreach ($a as $arr) {
>   $idx = 0;
>   while ($idx < count($arr)-1 && $arr[$idx] != NULL) $idx += 1;

“!= null” is a comparison recommended against as it does type juggling.

In PHP, “null” (or “NULL”) is a special value.  It is *that value* that were 
to be detected.  And the requirement was to do this *without* “having to use 
an 'if' or similar test”, which your approach obviously does not meet.  (The 
reason for that is that it cannot be met.)

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


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


csiph-web