Jump to content

can u use an array without an index?


willyamPax

Recommended Posts

I think you may be meaning somthing like this:

<?php
$myArray = array();
$myArray[] = 'array item 0';
$myArray[] = 'Array item 1';
echo $myArray[0];
//echos array item 0
?>

If you wanted preform a specific action to each item:

<?php
$myArray = array('me', 'myself', 'and', 'I');
foreach ($myArray as $arrayItem) {
   echo "$arrayItem <br>";
}
//This would output me<br>myself<br>and<br>I<br> 
?>

<?php 


 $returnArray= array();
 $myArray = array('me', 'myself', 'and', 'I');

            for ($i=0;$i<4;$i++) {
               $returnArray[]=$myArray[$i];   //just like this....the returnArray has no index    
	echo $returnArray[].'<br />';
		}
       //sorry for not giving an example so here it is
	 ?>

meaning u wont put an index to your return array

coz my classmates keeps on insisting that its possible...thats why im confuse?? ???

To answer your question seriously no.

 

replacing your word "index" with php's term of key you can not define an array without having keys that have values under the scope of a single variable

 

 

PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

 

using the syntax of

<?php
$var = arrary();
$var[] = "fish";
$var[] = "cat';
?>

 

Is still technically assigning keys (using a concept similar to mysql's AUTO_INCREMENT parameters on primary keys)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.