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: > ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
"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... |
|
|||
|
"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 |
|
|||
|
"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? |
|
|||
|
"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... |