for some reason, the array i made only queries out the last value in the foreach statement like example below

	$message = array('hello'=>'there');
	$message = array('hello'=>'there');
	$message = array('hello'=>'there');
	$message = array('hello'=>'there');

foreach ($message as $key => $value) {
	echo $key." ".$value;
}

it only queries out hello there once. what am i doing wrong ???

    because your array only has one key value pair. $message = is just overwriting the previously defined $message.

      So what you apparently want is a 2-dimension array:

      $message = array(
         array('hello'=>'there'),
         array('hello'=>'there'),
         array('hello'=>'there'),
         array('hello'=>'there')
      );
      

        So let me just get this straight so I can have a better understanding of arrays. Are you saying that the varible $message is actually a key value ?? I thought it was just a varible that store multiple key values. so doing this

        $message = array('hello' => 'value');
        

        is the samething as doing this

        $hello = array('value')
        

        except its encapsulated inside of the varible $message ?????

          Now this the code im using

          	$message = array(array('hello'=>'there'));
          	$message = array(array('hello2'=>'there'));
          	$message = array(array('hell03'=>'there'));
          	$message = array(array('hello4'=>'there'));
          
          foreach ($message as $key => $value) {
          	echo $key." ".$value;
          }
          
          

          but this is the output

          0 : Array 0 Array

            .. but you need to do is to do something like NogDog suggested:

            $message = array(array('hello'=>'there'),
                array('hello'=>'there'),
            	array('hello'=>'there'),
            	array('hello'=>'there'));
            
            foreach ($message as $key => $value) {
                //echo $key." ".$value;//this will list out only first array dimension, 
            //but to get an actual values you need to scope the second dimension like this:
            		while(list($k,$v) = each($value)){
            			echo "$k $v<br />";
            		}
                } 
            

            Your way of creating array only rewrites array so you end up with one member in the end and it is in second dimension...
            Hence the 0 Array... That just means that you have another array stashed under the key 0... Are array clearer now?

              Yes I understand now BUT....

              what if I have a Class that contains a array... and the only way you can add a key and value to this array is through a function.

              How would i go about doing that ? I just cant leave the array parenthesis open.

                You always can create a two dimensional array this way as well:

                $message[] = array('hello'=>'there');
                $message[] = array('hello'=>'there');
                $message[] = array('hello'=>'there');
                $message[] = array('hello'=>'there');
                $message[] = array('hello'=>'there');
                

                This way you would get the same array as in the previous example...

                  Write a Reply...