Page 1 of 1

Add to array but keep integrity of original array...

Posted: Sat Sep 04, 2004 10:44 pm
by hawleyjr
I have two different states arrays. One with all possible US territories and one only with US states.

I would like to combine the second array (US territories) with the first array (States only). I know I can do this using [php_man]array_merge[/php_man]() or $array1+$array2. However, it is important to keep array key integrity and I would like to use a reference to the first one vs just merging two arrays. (* using $array1+$array2 will keep key integrity)

Here is what I have so far:

Code: Select all

<?php
define('STATE_CW',0);
define('STATE_AL',1);
define('STATE_AK',2);
define('STATE_AZ',3);
define('STATE_AR',4);
define('STATE_CA',5);
define('STATE_CO',6);
define('STATE_CT',7);
define('STATE_DE',8);
define('STATE_DC',9);
define('STATE_FL',10);
define('STATE_GA',11);
define('STATE_GU',12);
define('STATE_HI',13);
define('STATE_ID',14);
define('STATE_IL',15);
define('STATE_IN',16);
define('STATE_IA',17);
define('STATE_KS',18);
define('STATE_KY',19);
define('STATE_LA',20);
define('STATE_ME',21);
define('STATE_MD',22);
define('STATE_MA',23);
define('STATE_MI',24);
define('STATE_MN',25);
define('STATE_MS',26);
define('STATE_MO',27);
define('STATE_MT',28);
define('STATE_NE',29);
define('STATE_NV',30);
define('STATE_NH',31);
define('STATE_NJ',32);
define('STATE_NM',33);
define('STATE_NY',34);
define('STATE_NC',35);
define('STATE_ND',36);
define('STATE_OH',37);
define('STATE_OK',38);
define('STATE_OR',39);
define('STATE_PA',40);
define('STATE_PR',41);
define('STATE_RI',42);
define('STATE_SC',43);
define('STATE_SD',44);
define('STATE_TN',45);
define('STATE_TX',46);
define('STATE_UT',47);
define('STATE_VT',48);
define('STATE_VA',49);
define('STATE_VI',50);
define('STATE_WA',51);
define('STATE_WV',52);
define('STATE_WI',53);
define('STATE_WY',54);


$A_STATE_ARRAY_NO_CW = array(
		STATE_AL=>'Alabama',
		STATE_AK=>'Alaska',
		STATE_AZ=>'Arizona',
		STATE_AR=>'Arkansas',
		STATE_CA=>'California',
		STATE_CO=>'Colorado',
		STATE_CT=>'Connecticut',
		STATE_DE=>'Delaware',
		STATE_DC=>'Dist. of Columbia',
		STATE_FL=>'Florida',
		STATE_GA=>'Georgia',
		STATE_HI=>'Hawaii',
		STATE_ID=>'Idaho',
		STATE_IL=>'Illinois',
		STATE_IN=>'Indiana',
		STATE_IA=>'Iowa',
		STATE_KS=>'Kansas',
		STATE_KY=>'Kentucky',
		STATE_LA=>'Louisiana',
		STATE_ME=>'Maine',
		STATE_MD=>'Maryland',
		STATE_MA=>'Massachusetts',
		STATE_MI=>'Michigan',
		STATE_MN=>'Minnesota',
		STATE_MS=>'Mississippi',
		STATE_MO=>'Missouri',
		STATE_MT=>'Montana',
		STATE_NE=>'Nebraska',
		STATE_NV=>'Nevada',
		STATE_NH=>'New Hampshire',
		STATE_NJ=>'New Jersey',
		STATE_NM=>'New Mexico',
		STATE_NY=>'New York',
		STATE_NC=>'North Carolina',
		STATE_ND=>'North Dakota',
		STATE_OH=>'Ohio',
		STATE_OK=>'Oklahoma',
		STATE_OR=>'Oregon',
		STATE_PA=>'Pennsylvania',
		STATE_RI=>'Rhode Island',
		STATE_SC=>'South Carolina',
		STATE_SD=>'South Dakota',
		STATE_TN=>'Tennessee',
		STATE_TX=>'Texas',
		STATE_UT=>'Utah',
		STATE_VT=>'Vermont',
		STATE_VA=>'Virginia',
		STATE_WA=>'Washington',
		STATE_WV=>'West Virginia',
		STATE_WI=>'Wisconsin',
		STATE_WY=>'Wyoming'
);

$A_STATE_ARRAY_CW = array(
		STATE_CW=>'Country wide',
		STATE_GU=>'Guam',
		STATE_PR=>'Puerto Rico',
		STATE_VI=>'Virgin Islands');


$A_STATE_ARRAY_CW += &$A_STATE_ARRAY_NO_CW;

echo '<HR><PRE>'; print_r($A_STATE_ARRAY_CW); echo '</PRE>';
?>
I get the following error when I reference the first array with the & operator.

Code: Select all

Parse error: parse error, unexpected '&amp;' in
If I remove the & the code works fine. But like I said earlier, I would like to reference the first array not just merge it to the second array.

Posted: Tue Sep 07, 2004 8:19 am
by scorphus
Do it element by element, for example:

Code: Select all

<?php
$numArray = array('one', 'two', 'three');
$fruArray = array('pear', 'lemon', 'apple');
for ($i = 0; $i < count($fruArray); $i++)
	$numArray[] = &$fruArray[$i];
print_r($numArray);
$fruArray[0] = 'orange';
print_r($numArray);
?>
which outputs:

Code: Select all

Array
(
    &#1111;0] =&gt; one
    &#1111;1] =&gt; two
    &#1111;2] =&gt; three
    &#1111;3] =&gt; pear
    &#1111;4] =&gt; lemon
    &#1111;5] =&gt; apple
)
Array
(
    &#1111;0] =&gt; one
    &#1111;1] =&gt; two
    &#1111;2] =&gt; three
    &#1111;3] =&gt; orange
    &#1111;4] =&gt; lemon
    &#1111;5] =&gt; apple
)
-- Scorphus

Posted: Tue Sep 07, 2004 8:35 am
by scorphus
Well, after reading it again I think I misunderstood your question. You might want to use [php_man]foreach[/php_man]() to keep integrity of original array. An example:

Code: Select all

<?php
$originalArray = array(3 => 'three',
	4 => 'four',
	6 => 'six',
	7 => 'seven');
$anotherArray = array(0 => 'zero',
	1 => 'one',
	2 => 'two',
	5 => 'five');
foreach ($anotherArray as $key => $val)
	$originalArray[$key] = &$anotherArray[$key];
print_r($originalArray);
$anotherArray[2] = 'TWO';
print_r($originalArray);
?>
which outputs:

Code: Select all

Array
(
    &#1111;3] =&gt; three
    &#1111;4] =&gt; four
    &#1111;6] =&gt; six
    &#1111;7] =&gt; seven
    &#1111;0] =&gt; zero
    &#1111;1] =&gt; one
    &#1111;2] =&gt; two
    &#1111;5] =&gt; five
)
Array
(
    &#1111;3] =&gt; three
    &#1111;4] =&gt; four
    &#1111;6] =&gt; six
    &#1111;7] =&gt; seven
    &#1111;0] =&gt; zero
    &#1111;1] =&gt; one
    &#1111;2] =&gt; TWO
    &#1111;5] =&gt; five
)
Hope it helps!

-- Scorphus