Re: simple array question?

This is a discussion on Re: simple array question? within the PHP Language forums, part of the PHP Programming Forums category; On 12-Aug-2003, "Randell D." <you.can.email.me.at.randelld@yahoo.com> wrote: > ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-13-2003
Tom Thackrey
 
Posts: n/a
Default Re: simple array question?


On 12-Aug-2003, "Randell D." <you.can.email.me.at.randelld@yahoo.com> wrote:

> I hope I can explain this clearly... I have an associate array - it
> contains
> a single element - for example
>
> $alpha=array("name"=>"randell");
>
> Now, I have an array that has a single (associative) element... thus
>
> foreach($alpha as $key=>$value)
> { print("<br>$key ==== $value"); }
>
> will print
>
> name ==== randell
>
> My question is - how can I place the associated name in to one variable,
> and
> the value in to another variable. Thus *after* I have run some funciton,
> I
> would like to have two variables, $fieldName="name" and
> $fieldValue="randell"
>
> I know I can do this inside a foreach loop but it seems wasteful of
> resources - surely there must be something like split or explode that I
> can
> use to split a single element in an array?
>
> anybody got any ideas?


http://www.php.net/manual/en/function.list.php


--
Tom Thackrey
www.creative-light.com
Reply With Quote
  #2 (permalink)  
Old 08-13-2003
Randell D.
 
Posts: n/a
Default Re: simple array question?


"Tom Thackrey" <tomnr@creative-light.com> wrote in message
news:Gig_a.233$vP5.30335033@newssvr21.news.prodigy .com...
>
> On 12-Aug-2003, "Randell D." <you.can.email.me.at.randelld@yahoo.com>

wrote:
>
> > I hope I can explain this clearly... I have an associate array - it
> > contains
> > a single element - for example
> >
> > $alpha=array("name"=>"randell");
> >
> > Now, I have an array that has a single (associative) element... thus
> >
> > foreach($alpha as $key=>$value)
> > { print("<br>$key ==== $value"); }
> >
> > will print
> >
> > name ==== randell
> >
> > My question is - how can I place the associated name in to one variable,
> > and
> > the value in to another variable. Thus *after* I have run some

funciton,
> > I
> > would like to have two variables, $fieldName="name" and
> > $fieldValue="randell"
> >
> > I know I can do this inside a foreach loop but it seems wasteful of
> > resources - surely there must be something like split or explode that I
> > can
> > use to split a single element in an array?
> >
> > anybody got any ideas?

>
> http://www.php.net/manual/en/function.list.php
>
>
> --
> Tom Thackrey
> www.creative-light.com



Nope - I had thought of that but to quote the php website:

"Note: list() only works on numerical arrays and assumes the numerical
indices start at 0. "

and I have an associative array

$alpha['name']="randell";

and want to get "name" in to one variable, and "randell" in to another.

any other ideas?

I currently have

foreach($alpha as $key=>$value)
{
$fieldName=$key;
$fieldValue=$value;
}

But would prefer some other solution as that doesn't look right...



Reply With Quote
  #3 (permalink)  
Old 08-13-2003
Default User
 
Posts: n/a
Default Re: simple array question?

"Randell D." wrote:

> and I have an associative array
>
> $alpha['name']="randell";
>
> and want to get "name" in to one variable, and "randell" in to another.
>
> any other ideas?
>
> I currently have
>
> foreach($alpha as $key=>$value)
> {
> $fieldName=$key;
> $fieldValue=$value;
> }
>
> But would prefer some other solution as that doesn't look right...




You'll have to be a little more clear about what you want to end up with
and how you plan to use the results.




Brian Rodenborn
Reply With Quote
  #4 (permalink)  
Old 08-13-2003
Randell D.
 
Posts: n/a
Default Re: simple array question?


"Default User" <first.last@company.com> wrote in message
news:3F3A7A4D.58D0915C@company.com...
> "Randell D." wrote:
>
> > and I have an associative array
> >
> > $alpha['name']="randell";
> >
> > and want to get "name" in to one variable, and "randell" in to another.
> >
> > any other ideas?
> >
> > I currently have
> >
> > foreach($alpha as $key=>$value)
> > {
> > $fieldName=$key;
> > $fieldValue=$value;
> > }
> >
> > But would prefer some other solution as that doesn't look right...

>
>
>
> You'll have to be a little more clear about what you want to end up with
> and how you plan to use the results.
>
>
>
>
> Brian Rodenborn


I can't think on how much more clear I could be... I have an array with a
single element, an example below:

$alpha['name']="randell";

and I want my end result to read this single element array and give me the
following

$x="name";
$y="randell";

where 'x' will be the associate name of the element, and y will be the value
of the element in the array.

Why do I want to do this? I'm using it because I have an multi-dimension
array, for example

$owner[1]['name']="randell";
$owner[1]['country']="canada";
$owner[1]['sex']="male";
$owner[2]['name']="john";
$owner[2]['country']="canada";
$owner[2]['sex']="male";
$owner[3]['name']="jone";
$owner[3]['country']="uk";
$owner[3]['sex']="female";

I will be displaying the above $owner array in an HTML SELECT
pulldown/box/menu and use the above values of $x and $y to set a default
HTML SELECT OPTION SELECTED. Thus, if all of the above was true, the
'selected' would be $owner[1]. For example

print<"select size=10 name=whatever>");
foreach($owner as $recordNumber=>$record)
{
$selectFlag="";
$selectString="";
if("$record[$x]"=="$y")
{ $selectFlag="selected"; }

foreach($record as $fieldName=>$fieldValue)
{ $selectString="$selectString $fieldValue"; }

print("<option value=$recordNumber $selectFlag>$selectString</option>");

}
print("</select>");


Since I'm creating a function that can be passed *any* of my multi-arrays, I
don't want to hard code anything that my specify what might be displayed, or
what would be used to find an OPTION as SELECTED. And in order for the
above to work, I need some way to assign $x and $y a value when read from
$alpha array. Currently, if you look at the top of this post, you'll find
that I'm doing it via a call to a foreach loop - but this is crazy since
$alpha array will always only contain a single element.

Any ideas?


Reply With Quote
  #5 (permalink)  
Old 08-14-2003
Randell D.
 
Posts: n/a
Default Re: simple array question?


"Default User" <first.last@company.com> wrote in message
news:3F3A7A4D.58D0915C@company.com...
> "Randell D." wrote:
>
> > and I have an associative array
> >
> > $alpha['name']="randell";
> >
> > and want to get "name" in to one variable, and "randell" in to another.
> >
> > any other ideas?
> >
> > I currently have
> >
> > foreach($alpha as $key=>$value)
> > {
> > $fieldName=$key;
> > $fieldValue=$value;
> > }
> >
> > But would prefer some other solution as that doesn't look right...

>
>
>
> You'll have to be a little more clear about what you want to end up with
> and how you plan to use the results.
>
>
>
>
> Brian Rodenborn




Never mind - I've decided to pass my function two seperate variables as
opposed to an array with a single element...

Thanks for the help anyways...


Reply With Quote
  #6 (permalink)  
Old 08-14-2003
Default User
 
Posts: n/a
Default Re: simple array question?

"Randell D." wrote:

> Never mind - I've decided to pass my function two seperate variables as
> opposed to an array with a single element...



Actually it sounds like an object fits the problem description better.




Brian Rodenborn
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 12:28 PM.


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