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. 4 3320
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
by: Peter Olcott |
last post by:
www.halting-problem.com
|
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...
|
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();...
|
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 />";
}
}
|
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...
|
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=...
|
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?
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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,...
|
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$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| | |