Welcome to WebmasterWorld Guest from 54.145.207.36

Forum Moderators: coopster & jatar k

Setting array keys AND values from another array

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

10+ Year Member



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

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

5+ Year Member




$cooking = array();

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

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

10+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month