# PHP Array



## dudeking (Feb 7, 2007)

How can I just echo the first item in an array? Not the whole array.

Thanks


----------



## MMJ (Oct 15, 2006)

Why couldn't you?

http://www.w3schools.com/php/php_arrays.asp

echo $names[1] or [2] or [3]


----------



## dudeking (Feb 7, 2007)

Things are always being added to the array.
I want to echo the newest one.

$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
echo $array1[5];

Would echo 6.php but then if I add 7.php to the array 6.php would still be echoed.
I want it to change to 7.php by its self.

This is what I came up with:


```
<?php
$stories = glob('stories/'); //Opens stories/
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php"); //The array
	arsort($array1); //Sorts them
	$filename = count($array1)('.php'); //Doesnt Work!! Is ment to output number of items in array with .php on the end.
	echo ($filename); //echos $filename
?>
```


----------



## knight_47 (Mar 15, 2006)

If your trying to count files in a folder with an extension of .php, you can try this:


```
$files = glob('*.php'); // glob() returns an array of filenames matching the extension

$num = count($files); // counts the php files

echo "there are $num files"; // displays how many there are
```


----------



## namenotfound (Apr 30, 2005)

Couldn't you do it without all the variables, making just one line of code?


```
echo "there are " . count(glob('*.php')) . " files";
```


----------



## dudeking (Feb 7, 2007)

I don't need it to list the files.
I need to include them.


```
<?php
$stories = glob('stories/');
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
	arsort($array1);
	$number = count($array1) - 1;
	$php = ".php";
	$filename = $number $php;
	include ($filename);
?>
```
It's $filename = $number $php; that I cant get working.
I need $filename to be $number with .php on the end.


----------



## namenotfound (Apr 30, 2005)

Try

```
$filename = $number . $php;
```
Or you could just do (without the need of a $php variable)

```
$filename = $number . ".php";
```
(1st example used here)


```
<?php
$stories = glob('stories/');
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
    arsort($array1);
    $number = count($array1) - 1;
    $php = ".php";
    $filename = $number .  $php;
    include ($filename);
?>
```
(2nd example used here)


```
<?php
$stories = glob('stories/');
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
    arsort($array1);
    $number = count($array1) - 1;
    $filename = $number . ".php";
    include ($filename);
?>
```


----------



## dudeking (Feb 7, 2007)

Thanks 
It was the . I was missing I was using a , I'll remember that for next time.

I have sorted it with this:-


```
<?php
$stories = glob('stories/');
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
	arsort($array1);
	$number = count($array1) - 1;
echo $array1[$number];
?>
```
I am now echoing the file name I want.
But I need to include the file not echo it.

If I change echo to include will it work?


----------



## dudeking (Feb 7, 2007)

```
<?php
$stories = glob('stories/');
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
	arsort($array1);
	$number = count($array1) - 1;
include ($array1[$number]);
?>
```
Right trying to include the file now.
I am getting messages about it not being able to find the file with that.
How can I add the directory to the include path?


----------



## dudeking (Feb 7, 2007)

```
<?php
$array1 = array("1.php","2.php","3.php","4.php","5.php","6.php");
	arsort($array1);
	$number = count($array1) - 1;
include ('stories/'.$array1[$number]);
?>
```
Wooo! It works.

Now for the next bit 

How can I make the array update it's self?
So the array is just all of the files in the directory with there file name as the value?
Then if I add a file to the directory it is put in the array by it's self


----------



## hi_itsme (Feb 17, 2007)

Just add the new entry to the end of your array using array_push().
If you use the above function to select by key (rather than value), you don't need to do anything else! Just select the last entry, and it will be the most recent.


----------

