array_search, array_keys problem

This is a discussion on array_search, array_keys problem within the PHP Language forums, part of the PHP Programming Forums category; Hi, I am basing this upon my study of the array_search and array_keys functions in www.php.net and www....


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-16-2003
Phil Powell
 
Posts: n/a
Default array_search, array_keys problem

Hi, I am basing this upon my study of the array_search and array_keys
functions in www.php.net and www.zend.com and www.nyphp.org.

I have this array, $this->propertyArray, which I have passed into a
class as a mocked-up version of the same formatting as _GET and _POST
arrays inasmuch as the keys are variable names and vals are the
variable values. Here is the print_r printout of $this->propertyArray
for example:

Array (
[birth_month] => 10
[birth_day] => 01
[birth_year] => 1964
[0] => 4
)

I have a local array, $keyIndexArray, which I derived by doing this:
$keyIndexArray = array_keys($this->propertyArray);

this produces the following array which I can show using print_r:

Array (
[0] => birth_month
[1] => birth_day
[2] => birth_year
[3] => 0
)

I produce $keyIndexArray in order to know the ordinal position of each
key/val pair in propertyArray since I will be doing a particular
function upon each element in propertyArray depending SOLELY on its
position (the keys and vals can literally be anything at all!). So I
figured the easiest way to do it is to create a local enumerative
array so that I have a "static" value by which to pull from something
that tells me "Hey, this is a month", or "Hey, this is a year".

I can get what I want by doing this:

foreach ($this->propertyArray as $key => $val) {
switch (array_search($key, $keyIndexArray)) {
case '2': // It's a year, do year stuff
break;
case '0': // It's a month, do month stuff
....
}
}

However, for some reason, for the switch statement I am always getting
'3' every single time, verified by this print_r on key and val and
array_search:

key = birth_month and val = 10 and array_search for birth_month in
keyIndexArray = 3

Here are my online references:

http://us2.php.net/manual/en/function.array-search.php
http://us2.php.net/manual/en/function.array-keys.php
http://us2.php.net/manual/en/control...res.switch.php

Maybe I missed something in my simple plan, that is, parse through
$this->propertyArray and do month stuff on the first one, day stuff
on the second one, year stuff on the third one, etc.

Phil

PS: THIS IS *****NOT***** HOMEWORK!!!!!!!!!!! Someone had a baby fit
when I posted stuff like this earlier so this is a disclaimer.
Reply With Quote
  #2 (permalink)  
Old 10-16-2003
Jon Kraft
 
Posts: n/a
Default Re: array_search, array_keys problem

Phil Powell <soazine@erols.com> wrote:

> I have a local array, $keyIndexArray, which I derived by doing this:
> $keyIndexArray = array_keys($this->propertyArray);
>
> I produce $keyIndexArray in order to know the ordinal position of each
> key/val pair in propertyArray since I will be doing a particular
> function upon each element in propertyArray depending SOLELY on its
> position (the keys and vals can literally be anything at all!). So I
> figured the easiest way to do it is to create a local enumerative
> array so that I have a "static" value by which to pull from something
> that tells me "Hey, this is a month", or "Hey, this is a year".
>
> I can get what I want by doing this:
>
> foreach ($this->propertyArray as $key => $val) {
> switch (array_search($key, $keyIndexArray)) {
> case '2': // It's a year, do year stuff
> break;
> case '0': // It's a month, do month stuff
> ....
> }
> }


Hi Phil,

How about:

foreach ($this->propertyArray as $key => $val) {
switch ($key) {
case 'birth_year': // It's a year, do year stuff
break;
case 'birth_month': // It's a month, do month stuff
....
}
}

JOn
Reply With Quote
  #3 (permalink)  
Old 10-16-2003
Phil Powell
 
Posts: n/a
Default Re: array_search, array_keys problem

I'm sorry I can't do that, because it may not be 'birth_month', it
could be 'graduation_month' or 'graduation_moment_in_my_life' or
'whatever_I_felt_like_using_as_a_key_today'

The keys are totally dynamic, determined by a prior form producing.

Phil

Jon Kraft <jon@jonux.co.uk> wrote in message news:<bmmch0$okceb$1@ID-175424.news.uni-berlin.de>...
> Phil Powell <soazine@erols.com> wrote:
>
> > I have a local array, $keyIndexArray, which I derived by doing this:
> > $keyIndexArray = array_keys($this->propertyArray);
> >
> > I produce $keyIndexArray in order to know the ordinal position of each
> > key/val pair in propertyArray since I will be doing a particular
> > function upon each element in propertyArray depending SOLELY on its
> > position (the keys and vals can literally be anything at all!). So I
> > figured the easiest way to do it is to create a local enumerative
> > array so that I have a "static" value by which to pull from something
> > that tells me "Hey, this is a month", or "Hey, this is a year".
> >
> > I can get what I want by doing this:
> >
> > foreach ($this->propertyArray as $key => $val) {
> > switch (array_search($key, $keyIndexArray)) {
> > case '2': // It's a year, do year stuff
> > break;
> > case '0': // It's a month, do month stuff
> > ....
> > }
> > }

>
> Hi Phil,
>
> How about:
>
> foreach ($this->propertyArray as $key => $val) {
> switch ($key) {
> case 'birth_year': // It's a year, do year stuff
> break;
> case 'birth_month': // It's a month, do month stuff
> ....
> }
> }
>
> JOn

Reply With Quote
  #4 (permalink)  
Old 10-17-2003
Jon Kraft
 
Posts: n/a
Default Re: array_search, array_keys problem

Phil Powell <soazine@erols.com> wrote:
> Jon Kraft <jon@jonux.co.uk> wrote:
>> Phil Powell <soazine@erols.com> wrote:
>>
>> > I have a local array, $keyIndexArray, which I derived by doing this:
>> > $keyIndexArray = array_keys($this->propertyArray);
>> >
>> > I produce $keyIndexArray in order to know the ordinal position of each
>> > key/val pair in propertyArray since I will be doing a particular
>> > function upon each element in propertyArray depending SOLELY on its
>> > position (the keys and vals can literally be anything at all!). So I
>> > figured the easiest way to do it is to create a local enumerative
>> > array so that I have a "static" value by which to pull from something
>> > that tells me "Hey, this is a month", or "Hey, this is a year".
>> >
>> > I can get what I want by doing this:
>> >
>> > foreach ($this->propertyArray as $key => $val) {
>> > switch (array_search($key, $keyIndexArray)) {
>> > case '2': // It's a year, do year stuff
>> > break;
>> > case '0': // It's a month, do month stuff
>> > ....
>> > }
>> > }

>>
>>
>> foreach ($this->propertyArray as $key => $val) {
>> switch ($key) {
>> case 'birth_year': // It's a year, do year stuff
>> break;
>> case 'birth_month': // It's a month, do month stuff
>> ....
>> }
>> }

>
> I'm sorry I can't do that, because it may not be 'birth_month', it
> could be 'graduation_month' or 'graduation_moment_in_my_life' or
> 'whatever_I_felt_like_using_as_a_key_today'
>
> The keys are totally dynamic, determined by a prior form producing.


Okay, I see. I just don't understand how you then can determine what exactly
to do in your switch statement if the key at index '2' can be a year,
month, string, whatsoever?

Anyway, have you tried using integers (and I've put FALSE in there in well):

foreach ($this->propertyArray as $key => $val) {
$found = array_search($key, $keyIndexArray);
switch ($found) {
case FALSE: // key not found, do stuff
break;
case 2: // It's a year, do year stuff
break;
case 0: // It's a month, do month stuff
break;
....
}
}

JOn
Reply With Quote
  #5 (permalink)  
Old 10-17-2003
Phil Powell
 
Posts: n/a
Default Re: array_search, array_keys problem

Well the solution I came up with was very complex and bizarre, but
literaly the only one I could feasibly come up with and I'm sorry..

// CONVERT THE INPUT PARAMETER ARRAY INTO A 2-DIM ENUMERATIVE ARRAY TO
PRESERVE KEYS AND VALS AND HAVE ENUMERATION FOR SWITCH
$enumKeyValArray = array();
foreach($this->propertyArray as $key => $val)
array_push($enumKeyValArray, array($key, $val));


for ($i = 0; $i < sizeof($enumKeyValArray); $i++) {
$html = '';
switch ($i) {
case '2': // TEXT FIELD SLOT IN INPUT ARRAY PARAMETER
if ($this->hasYear) {


break;

case '3': // SIZE OF TEXT FIELD - NOTHING NEEDS TO BE DONE
// DO NOTHING
break;

case '0': // MONTH DROPDOWN
if ($this->hasMonth) {
// DO MONTH STUFF
}
break;

case '1': // DAY DROPDOWN
if ($this->hasDay) {
// DO DAY STUFF
}
break;

default: // THIS WILL BE EXPANDED FOR FUTURE IMPLEMENTATION OF
DATEGROUP, FOR NOW IF ANYTHING ELSE IS FOUND DO NOTHING
// DO NOTHING
break;
}


}


I would then instantiate the class using optional boolean parameters
to determine if I am to display a month dropdown/day dropdown/year
textfield or not.

$dateGroup = new DateGroupHTMLGenerator($myPOSTLikeArray); // DISPLAY
ALL
$dateGroup = new DateGroupHTMLGenerator($myPOSTLikeArray, 1, 0, 1); //
DON'T DISPLAY DAY DROPDOWN

Phil

Jon Kraft <jon@jonux.co.uk> wrote in message news:<bmo9nk$p3bt8$2@ID-175424.news.uni-berlin.de>...
> Phil Powell <soazine@erols.com> wrote:
> > Jon Kraft <jon@jonux.co.uk> wrote:
> >> Phil Powell <soazine@erols.com> wrote:
> >>
> >> > I have a local array, $keyIndexArray, which I derived by doing this:
> >> > $keyIndexArray = array_keys($this->propertyArray);
> >> >
> >> > I produce $keyIndexArray in order to know the ordinal position of each
> >> > key/val pair in propertyArray since I will be doing a particular
> >> > function upon each element in propertyArray depending SOLELY on its
> >> > position (the keys and vals can literally be anything at all!). So I
> >> > figured the easiest way to do it is to create a local enumerative
> >> > array so that I have a "static" value by which to pull from something
> >> > that tells me "Hey, this is a month", or "Hey, this is a year".
> >> >
> >> > I can get what I want by doing this:
> >> >
> >> > foreach ($this->propertyArray as $key => $val) {
> >> > switch (array_search($key, $keyIndexArray)) {
> >> > case '2': // It's a year, do year stuff
> >> > break;
> >> > case '0': // It's a month, do month stuff
> >> > ....
> >> > }
> >> > }
> >>
> >>
> >> foreach ($this->propertyArray as $key => $val) {
> >> switch ($key) {
> >> case 'birth_year': // It's a year, do year stuff
> >> break;
> >> case 'birth_month': // It's a month, do month stuff
> >> ....
> >> }
> >> }

> >
> > I'm sorry I can't do that, because it may not be 'birth_month', it
> > could be 'graduation_month' or 'graduation_moment_in_my_life' or
> > 'whatever_I_felt_like_using_as_a_key_today'
> >
> > The keys are totally dynamic, determined by a prior form producing.

>
> Okay, I see. I just don't understand how you then can determine what exactly
> to do in your switch statement if the key at index '2' can be a year,
> month, string, whatsoever?
>
> Anyway, have you tried using integers (and I've put FALSE in there in well):
>
> foreach ($this->propertyArray as $key => $val) {
> $found = array_search($key, $keyIndexArray);
> switch ($found) {
> case FALSE: // key not found, do stuff
> break;
> case 2: // It's a year, do year stuff
> break;
> case 0: // It's a month, do month stuff
> break;
> ....
> }
> }
>
> JOn

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:47 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0