...

View Full Version : Help adding values to an array



tomharto
04-22-2011, 07:01 PM
I have a foreach loop (runs 3 times for jpg, gif and png images) doing a glob search for image files, and i want to store them all in an array, however at the minute the array looks like this


Array ( [0] => Array ( [0] => ../photos/1.jpg [1] => ../photos/10.jpg [2] => ../photos/11.jpg [3] => ../photos/12.jpg [4] => ../photos/13.jpg [5] => ../photos/14.jpg [6] => ../photos/15.jpg [7] => ../photos/16.jpg [8] => ../photos/17.jpg [9] => ../photos/18.jpg [10] => ../photos/19.jpg [11] => ../photos/2.jpg [12] => ../photos/20.jpg [13] => ../photos/21.jpg [14] => ../photos/22.jpg [15] => ../photos/23.jpg [16] => ../photos/24.jpg [17] => ../photos/25.jpg [18] => ../photos/26.jpg [19] => ../photos/3.jpg [20] => ../photos/4.jpg [21] => ../photos/5.jpg [22] => ../photos/6.jpg [23] => ../photos/7.jpg [24] => ../photos/8.jpg [25] => ../photos/9.jpg [26] => ../photos/jimmy-the-rev-sullivan.jpg [27] => ../photos/kit.jpg [28] => ../photos/mp3.jpg ) [1] => Array ( [0] => ../photos/angrynerd.png [1] => ../photos/confnerd.png [2] => ../photos/haha.png ) [2] => Array ( [0] => ../photos/Oliver17.gif ) )

It has 3 different arrays inside it. How do i stop it doing that and make them all add to one array? Heres my code for building the array


class createGallery {
var $result;
function getArray($dir) {

$search = array($dir.".jpg", $dir.".png", $dir.".gif");
foreach ($search as $file)
{
$this->result[] = glob($file);
}
echo '<br /><br /><br />';
}

Keleth
04-22-2011, 07:26 PM
Since glob returns an array, you're asking for it to give you back 3 arrays. Take a look at using array_merge (http://php.net/manual/en/function.array-merge.php) instead.



$this->result = array();
$search = array($dir.".jpg", $dir.".png", $dir.".gif");
foreach ($search as $file)
{
$this->result = array_merge($this->result, glob($file));
}
echo '<br /><br /><br />';

I'm not sure if that works 100%, but its a good start.

tomharto
04-22-2011, 07:28 PM
I got a fix but maybe array merge will be better so ill take a look at it :). This is what i got to work
class createGallery {
var $result;
function getArray($dir) {

$search = array($dir.".jpg", $dir.".png", $dir.".gif");
foreach ($search as $file)
{
$glob = glob($file);
foreach ($glob as $src)
{
$this->result[] = $src;
}
}
echo '<br /><br /><br />';
}

Keleth
04-22-2011, 07:31 PM
Yah, that'll work (though you don't need to define $glob... you can just put glob($file) into the foreach).

tomharto
04-22-2011, 07:41 PM
Okay, thanks :)

oesxyl
04-22-2011, 08:44 PM
I got a fix but maybe array merge will be better so ill take a look at it :). This is what i got to work
class createGallery {
var $result;
function getArray($dir) {

$search = array($dir.".jpg", $dir.".png", $dir.".gif");
foreach ($search as $file)
{
$glob = glob($file);
foreach ($glob as $src)
{
$this->result[] = $src;
}
}
echo '<br /><br /><br />';
}
i don't think you don't need the foreach and also don't need to use $search array



class createGallery {
var $result;
function getArray($dir) {

$this->result = glob($dir . "*.[jpg|png|gif]");
}
}

best regards

tomharto
04-22-2011, 08:58 PM
Hmm, i tried that but it just outputs "Array"

oesxyl
04-22-2011, 09:36 PM
Hmm, i tried that but it just outputs "Array"
an empty array? maybe the problem is the extension spec, [jpg|..]. you are on windows?

try this:


$search = array($dir.".jpg", $dir.".png", $dir.".gif");
$path = implode(',', $search);
$this->result = glob($path);


best regards

tomharto
04-22-2011, 09:52 PM
That brings back an empty array too =/, and yeah im using windows.

oesxyl
04-22-2011, 10:01 PM
That brings back an empty array too =/, and yeah im using windows.
sorry is my fault, i copy the array from a previous post
must be:


$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");

the stars are important for matching the filename.
try this and see if work.

best regards

tomharto
04-22-2011, 10:14 PM
I tried that too before :P still nothing both times =[

oesxyl
04-22-2011, 10:33 PM
I tried that too before :P still nothing both times =[
this way? with implode and stars in pattern?


$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
$path = implode(',', $search);
$this->result = glob($path);


best regards

tomharto
04-22-2011, 10:36 PM
Yeah, ill copy that code in and test it again maybe i messed it up, ill edit once tested.

Nope, still no luck :(.

I dont know if this makes annnnny difference at all, but the photos are in a parent directory (../photos to be exact), and the page im writing the class/methods on is include on an index page called the methods
$gallery = new createGallery;
$gallery->getArray("../photos/");. Both of those pages are in a child folder.

oesxyl
04-22-2011, 10:48 PM
Yeah, ill copy that code in and test it again maybe i messed it up, ill edit once tested.

Nope, still no luck :(.

I dont know if this makes annnnny difference at all, but the photos are in a parent directory (../photos to be exact), and the page im writing the class/methods on is include on an index page called the methods
$gallery = new createGallery;
$gallery->getArray("../photos/");. Both of those pages are in a child folder.
relative path are tricky, try to use absolute path. Take as reference the document root. Also i'm not sure about this but i remember about some changes in expanding '..' in path in php 5.1.x, is better to avoid them.
try this and check if the value of the path is correct, no '//' or missing '/' between directorys, and to point to the correct place:


$dir = $_SERVER['DOCUMENT_ROOT'] . "photos/";
echo $dir;


best regards

tomharto
04-22-2011, 11:23 PM
That pointed to the full home/public_html path, in the index where i call the method i put


$gallery->getArray1("http://mysite.co.uk/functionsPHP/photos/");
$gallery->printArray();

and i have this as the method

function getArray1($dir) {

$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
$path = implode(',', $search);
$this->result = glob($path);

echo "<a href='".$dir."'>Test</a>";
}
function printArray() {

foreach ($this->result as $file)
{
echo $file."<br />";
}
}

and all the page displays is the test link, which goes to the correct folder

oesxyl
04-22-2011, 11:42 PM
That pointed to the full home/public_html path, in the index where i call the method i put


$gallery->getArray1("http://mysite.co.uk/functionsPHP/photos/");
$gallery->printArray();

and i have this as the method

function getArray1($dir) {

$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
$path = implode(',', $search);
$this->result = glob($path);

echo "<a href='".$dir."'>Test</a>";
}
function printArray() {

foreach ($this->result as $file)
{
echo $file."<br />";
}
}

and all the page displays is the test link, which goes to the correct folder
well, sometime i'm an idiot, :)
tested and it work but this way:


$search = array($dir."*.jpg", $dir."*.png", $dir."*.gif");
$path = '{' . implode(',', $search) . '}';
$this->result = glob($path,GLOB_BRACE);


i forget about expanding GLOB_BRACE and '{}', i used this many years ago, :)

best regards

tomharto
04-23-2011, 12:00 AM
That works :) thanks. Does BRACE mean if $path is an array, it will search each value in the array?

oesxyl
04-23-2011, 12:14 AM
That works :) thanks. Does BRACE mean if $path is an array, it will search each value in the array?
as far as i know, no, there is no connection between glob syntax and php, the argument of glob is a string, that's way i used implode and dot for contenation.

best regards

tomharto
04-23-2011, 12:35 AM
Ahh i see, thanks :)

oesxyl
04-23-2011, 12:43 AM
Ahh i see, thanks :)
you are welcome, :)
seems that i was wrong, the '*.[jpg|png|gif]' i suggest before is not allowed in glob.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum