Forum Moderators: coopster

Message Too Old, No Replies

indexed vs. associative arrays

         

Sarah Atkinson

2:38 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



I have a table in my DB that links two other tables together for 1-many association. The table contains a field for ID from one table and the ID from another.

In my PHP I many an Ass. array that has the two id's assosiated with each other. ($arr[12]=24)
I don't want the array to treat this as an indexed array. To make it an ass. array would I simply need to put the 12 in quotes?

lobo235

5:11 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



PHP will automatically change $arr['12'] to $arr[12] so you would get the same results either way. I can't see why it would make a difference anyway to have the array key be a string instead of an integer. What exactly are you trying to accomplish?

coopster

10:49 am on Jun 21, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you truly want to keep it a associative array index (and eliminate any type-juggling confusion) you could always append/prepend a static value to your indexes (keys) as you load the array. Some thing like 's' for string (PHP will then use an associative index)...
$number = 12; 
$array['s'.$number] = 24;

Sarah Atkinson

4:15 pm on Jun 21, 2005 (gmt 0)

10+ Year Member



what I will do is something like this

$nid=$database['id'];
if($nid==$myarray[X]{
do something
}

'X' being exsample varriable representing whatever number the i'm compairing it to and not PHP varriable code(havet figured out yet how/where it's is coming from only where it origionates. That's on my tomorrow's to-do list.

Sarah

(added)
What worries me aboult it is what if I add two arrays together. will it change their indexed numbers? and what if their order is important?

Right now I am kinda behind on my thinking of this due to some tecnical errors dealing with
(Suprise suprise) windows. I'm finaly back up and codeing by remoting into my laptop and just using it. unfortunaltly it is slower and hass less memory then windows.... I'm hoping next week I might feel a bit uncreative and ambitouse one day and decide I have the time and energy to waste on rebuilding my desktop.(That would be a good day or two down the drain)

coopster

10:11 am on Jun 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




What worries me aboult it is what if I add two arrays together. will it change their indexed numbers? and what if their order is important?

It may change their indexed numbers, it depends on how you "add" the two arrays together. It's important to understand how it works. Here is a snippet for you to play with and learn from, detailed explanations can be found in the PHP manual for the array_merge() [php.net] function ...

<pre> 
<?php
$array1 = array('zero', 'one', 'two');
$array2 = array('three', 'four', 'five');
$array = $array1 + $array2;
print_r($array);
$array = array_merge($array1, $array2);
print_r($array);
?>
</pre>
If the order is important, even appending or prepending a character as I described earlier will still give you the sorting ability you desire. In the example you gave, and in the explanation from lobo235, you will find that the indexed variable already exists, whether you append/prepend a string character or not. So, you check for the index/key existence and code accordingly.

Sarah Atkinson

4:19 pm on Jun 22, 2005 (gmt 0)

10+ Year Member



ok coopster,
I'm going with the letter added to the front although I am declaring them all 'a'.$. Now to put them into the $_SESSION I want to fillter them to make sure only the ones that start with a followed by a number are added.

foreach($_POST as $key => $val){
(statment that says if the first letter in $key is 'a' and the second is a number)
$_SESSION[$key]=$val;

Sarah Atkinson

4:35 pm on Jun 22, 2005 (gmt 0)

10+ Year Member




foreach($_POST as $key => $val){
(statment that says if the first letter in $key is 'a' and the second is a number)
$_SESSION[$key]=$val;

I'm thinking it would be somehting like
if(ereg('^a[0-9]+$',$key)