Sign in to follow this  
Followers 0

php Split directory contents into array.

6 posts in this topic

Posted

So far I have this:



<?php

$image_dir = scandir("images");

$images = array_diff($image_dir, array('.', '..'));


var_dump($images);


?>

This loads the specified directory contents into an array which is a good start!

The array_diff function removes the . and .. from the array (linux server).

I'm then left with image1, image1_thumb, image2, image2_thumb, etc. in the array.

I want to split these into separate items, so that the image and the thumb associated with that image will each have it's own section in the array? I hope that makes sense.

Cheers,

Alex

Edit: urgh wrong section, should be in web design and programming.

Share this post


Link to post
Share on other sites

Posted

Does an image always have a thumb, and are they always next to each other in the array? If so you can use array_chunk($arr, 2).


$arr = array('image1', 'image1_thumb', 'image2', 'image2_thumb');

$arr = array_chunk($arr, 2);

array (size=2)

  0 => 

    array (size=2)

      0 => string 'image1' (length=6)

      1 => string 'image1_thumb' (length=12)

  1 => 

    array (size=2)

      0 => string 'image2' (length=6)

      1 => string 'image2_thumb' (length=12)

Share this post


Link to post
Share on other sites

Posted

That has worked brilliantly! Thank you so much :)

I'm newish to arrays so I wonder if you'd be able to help me with the next bit...

Here's how the array looks:



Array

(

	[0] => Array

		(

			[0] => image1.jpg

			[1] => image1_thumb.jpg

		)


	[1] => Array

		(

			[0] => image2.jpg

			[1] => image2_thumb.jpg

		)


	[2] => Array

		(

			[0] => image4.jpg

			[1] => image4_thumb.jpg

		)


//etc......

)

I'm would like to recursively tech the contents like so:

echo 'somehtmlgoeshere' image_name 'morehtml' thumb_name 'lastbitofhtml';

I'm not sure where to start :s

Cheers,

Alex

Share this post


Link to post
Share on other sites

Posted

I know I can do this:


echo $images[0][1];

and it will echo that specific area of the array but I want it to do the following:

echo $images[0][0]; echo $images[0][1];

echo $images[1][0]; echo $images[1][1];

etc....

until reaching the end of the array.

Clues?

Share this post


Link to post
Share on other sites

Posted

So this sorta works:



foreach ($images as $value1) {

	foreach ($value1 as $value2) {

		echo "$value2\n";

	}

}

It doesnt put the values in any order though, just prints the contents of the array one after the other.

I want the image and then the thumb printed on the same lines. Then a new line for the next one. Make sense?

Cheers.

Share this post


Link to post
Share on other sites

Posted

Done it!:



foreach ($images as $value) {

echo 'Image: '.$value[0].' Thumb: '.$value[1].'<br>';

    }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.