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

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.
Jul 17 '05 #1
4 3321
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tamlyn Rhodes | last post by:
I have a gallery object which contains an array of image objects. I would like the image objects to be able to return their adjacent siblings in the array so that, for example,...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
2
by: yawnmoth | last post by:
Say I have the following script - test.php: <? if (count($_GET) != 0) { foreach(array_keys($_GET) as $var) { echo "$var<br />"; } }
2
by: Daz | last post by:
Hi everyone. I am having a problem with array_search(). My php script gets a list of books from the database, and then compares them with a list of books which have been obtained from a textarea in...
1
by: Johnnyboy01 | last post by:
Hi all... I am having problems with the array_search function. No matter what I do, I can't get it to find a value that I specify. Here is the code I am using to call it: $value = 'month'; $key=...
2
hsriat
by: hsriat | last post by:
Is there any function in JS similar to array_search() of PHP which returns the key that contains the particular value, passed as argument. Or I need to make it myself?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.