473,796 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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->propertyArra y, 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->propertyArra y
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($thi s->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->propertyArra y 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->propertyArra y 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 3348
Phil Powell <so*****@erols. com> wrote:
I have a local array, $keyIndexArray, which I derived by doing this:
$keyIndexArray = array_keys($thi s->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->propertyArra y 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->propertyArra y 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_mon th' or 'graduation_mom ent_in_my_life' or
'whatever_I_fel t_like_using_as _a_key_today'

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

Phil

Jon Kraft <jo*@jonux.co.u k> 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($thi s->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->propertyArra y 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->propertyArra y 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.u k> wrote:
Phil Powell <so*****@erols. com> wrote:
> I have a local array, $keyIndexArray, which I derived by doing this:
> $keyIndexArray = array_keys($thi s->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->propertyArra y 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->propertyArra y 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_mon th' or 'graduation_mom ent_in_my_life' or
'whatever_I_fel t_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->propertyArra y as $key => $val) {
$found = array_search($k ey, $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
$enumKeyValArra y = array();
foreach($this->propertyArra y as $key => $val)
array_push($enu mKeyValArray, array($key, $val));
for ($i = 0; $i < sizeof($enumKey ValArray); $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 DateGroupHTMLGe nerator($myPOST LikeArray); // DISPLAY
ALL
$dateGroup = new DateGroupHTMLGe nerator($myPOST LikeArray, 1, 0, 1); //
DON'T DISPLAY DAY DROPDOWN

Phil

Jon Kraft <jo*@jonux.co.u k> wrote in message news:<bm******* *****@ID-175424.news.uni-berlin.de>...
Phil Powell <so*****@erols. com> wrote:
Jon Kraft <jo*@jonux.co.u k> wrote:
Phil Powell <so*****@erols. com> wrote:

> I have a local array, $keyIndexArray, which I derived by doing this:
> $keyIndexArray = array_keys($thi s->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->propertyArra y 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->propertyArra y 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_mon th' or 'graduation_mom ent_in_my_life' or
'whatever_I_fel t_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->propertyArra y as $key => $val) {
$found = array_search($k ey, $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
1600
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, $gallery->images->nextImage() would return $gallery->images. The images each contain a reference to their parent (i.e. the gallery object) so I tried using array_keys(). This is a method of the image class: function nextImage() { $pos =...
11
3764
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 (ClassB* b; b->func(); ) the base-class function is called instead of the new function in the derived class. All other similar functions (virtual in the base class and overwritten in the the derived class) work fine, it's just this one function. ...
117
7273
by: Peter Olcott | last post by:
www.halting-problem.com
18
6178
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 strcat (mystract). Program below. However this appears not to have fixed the problem and I don't know why it shouldn't ? Any further help as to what else I am doing wrong will be appreciated regards
28
5224
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
2
2666
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
3417
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 a form, and formatted into single books, each one stored in a non-associative array. I have a function that compares the books in the $user_books array, to those in the array derived from the books that are in the database. If a book exists...
1
1731
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= array_search($value, $array); Yet, it won't work, I have done it using the value itself, itstead of a variable, and I have searched for several different things that I know are in the array. I also have a question aboud the array_search...
2
10333
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
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.