By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,744 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,744 IT Pros & Developers. It's quick & easy.

array_search, array_keys problem

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Phil Powell <so*****@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
Jul 17 '05 #2

P: n/a
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 <jo*@jonux.co.uk> wrote in message news:<bm************@ID-175424.news.uni-berlin.de>...
Phil Powell <so*****@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

Jul 17 '05 #3

P: n/a
Phil Powell <so*****@erols.com> wrote:
Jon Kraft <jo*@jonux.co.uk> wrote:
Phil Powell <so*****@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
Jul 17 '05 #4

P: n/a
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 <jo*@jonux.co.uk> wrote in message news:<bm************@ID-175424.news.uni-berlin.de>...
Phil Powell <so*****@erols.com> wrote:
Jon Kraft <jo*@jonux.co.uk> wrote:
Phil Powell <so*****@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

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.