homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Setting array keys AND values from another array
neophyte




msg:3615990
 6:58 am on Apr 1, 2008 (gmt 0)

Hello All -

I've got a loop that checks various information in any number of multi-dimensional $_Session arrays. As the loop executes, I'd like to set the KEYS and VALUES of a NEW array to various values found in the Session arrays.

For example, my session looks like this: $_Session['Apples']['Name']['Color']. The value of 'Name' could be Pippen, Washington, Granny Smith. The Value of 'Color' could be 'Green, Yellow, or Red.

So, as I do my loop, I'd like to set the KEYS of this new array to the apple names in the session ($_Session['Apples']['Name']) and the associated value of the new array to the apple colors in the session ($_Session['Apples']['Name']['Color']) kinda like this:

$cooking = array();

$cooking .= $_Session['Apples']['Name'] => $_Session['Apples']['Name']['Color'];

I thought this would be pretty straight forward, but I'm always getting a "unexpected T_DOUBLE_ARROW" parse error. Can someone see what I'm doing wrong?

Thanks in advance!
Neophyte

 

Steerpike




msg:3616012
 7:59 am on Apr 1, 2008 (gmt 0)


$cooking = array();

$cooking['Apples'] = $_Session['Apples'];
$cooking['Apples']['Name'] = $_Session['Apples']['Name'];
$cooking['Apples']['Name']['Colour'] = $_Session['Apples']['Name']['Colour'];

neophyte




msg:3616081
 11:01 am on Apr 1, 2008 (gmt 0)

Steerpike -

Thanks for that. What I gave was a REALLY POOR example... my apologies.

I have found a way to do what I want to do using array_combine() but am wondering if you or anyone else knows of a better (more efficient) way to accomplish this.

REAL EXAMPLE code below:

$_SESSION['Client'] = array
(
'First' => array
(
'name' => 'first',
'value' => 'John'
),
'Last' => array
(
'name' => 'last',
'value' => 'Jones'
),
'Phone' => array
(
'name' => 'phone',
'value' => '000-000-0000'
),
'Fax' => array
(
'name' => 'fax',
'value' => '111-111-1111'
)
);

foreach($_SESSION['Client'] as $k => $v)
{
$keys[] = $k;
$values[] = $v['value'];
}

$a = array_combine($keys, $values);

print_r($a);

So, yes, using array_combine does work, but it just seems, um, so "clunky". Isn't there another function that would populate a blank array with associative keys/values in one shot?

Neophyte

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved