2

I'm building a program that parses a website and store links and images, i'm storing them in separate arrays, one array for the links, one for images, etc.. I want it to save on one multidimensional array.. how can I do it ? I have three functions that return an ID array, containing ID's from the topic i'm storing, another function that returns an array of titles from the topics i'm saving and another function that return an array with all image links.. so my "main" function is:

$array_id = getIds();
$array_titles = getTitles($array_id);
$array_images = getImages($array_id);

I want to store it in somewhay that when I call like:

$array[$id][$images]

I get all images on this topic ID, or if I call

$array[$id][$title]

I get the title on this topic ID, is this possible ? or is this the best way to do it ? I'm new to php, i would make a struct if I were using C, so I don't know if there is anything equivalent to it on php, thank you very much..

2
  • What is the structure of the titles and images arrays? How are they keyed to the correct IDs? Commented Jul 9, 2011 at 2:19
  • No, they are regular arrays, containing links only, they are not keyed to ids, at all, that's what i'm trying to do, key them to the ID's so I can use them just by referencing the ID's, I just don't know how to do it in php, can you help me ? Commented Jul 9, 2011 at 2:30

2 Answers 2

1

Rather than functions returning arrays of all titles and all images, you will need functions that return only the titles or images related to a single id. We don't see how you're defining your functions.

//Get all your ids
$array_id = getIds();

// Functions take an id and retrieve only its related objects
function getTitles($id) {}
function getImages($id) {}

//Iterate over the ids,  and retrieve the titles and images for each
foreach ($array_id as $id)
{
  // getTitles returns an array, keyed as 'titles' in the id array
  $id['titles'] = getTitles($id);

  // getImages returns an array, keyed as 'images' in the id array
  $id['images'] = getImages($id);
}

// Now you can retrieve them like so:
// A single title...
echo $array_id[0]['titles'][0];
// All titles
print_r($array_id[0]['titles'];

The structure of your resulting array looks something like this mock-up:

Array
(
    [0] => Array
        (
            [titles] => Array
                (
                    [0] => title 0a
                    [1] => title 0b
                    [2] => title 0c
                )    
            [images] => Array
                (
                    [0] => img0a.jpg
                    [1] => img0b.jpg
                )    
        )    
    [1] => Array
        (
            [titles] => Array
                (
                    [0] => title 1a
                    [1] => title 1b
                    [2] => title 1c
                )    
            [images] => Array
                (
                    [0] => img1a.jpg
                    [1] => img1b.jpg
                )    
        )    
    [2] => Array
        (
            [titles] => Array
                (
                    [0] => title 2a
                    [1] => title 2b
                    [2] => title 2c
                )    
            [images] => Array
                (
                    [0] => img2a.jpg
                    [1] => img2b.jpg
                )    
        )    
)

If it helps you, here's what I used to create the array mock-up with the php -a interactive interpreter:

php > $ids = array();
php > $ids[0] = array('titles'=>array('title 1', 'title 2', 'title 3'), 'images'=>array('img1.jpg', 'img2.jpg'));
php > $ids[1] = array('titles'=>array('title 1a', 'title 1b', 'title 1c'), 'images'=>array('img1a.jpg', 'img1b.jpg'));
php > $ids[2] = array('titles'=>array('title 2a', 'title 2b', 'title 2c'), 'images'=>array('img2a.jpg', 'img2b.jpg'));
php > $ids[0] = array('titles'=>array('title 0a', 'title 0b', 'title 0c'), 'images'=>array('img0a.jpg', 'img0b.jpg'));
php > print_r($ids);
Sign up to request clarification or add additional context in comments.

1 Comment

$id['titles'] = getImages($id); => $id['images'] = getImages($id); on line 11 :) Otherwise a good solution.
0

You may want to consider PHP class as an alternative, since struct is pretty much a class with default public data members, it may be something in this form:

 class arrayInfo {
     public var id;
     public var title;
     public var image;
}

You then can use them in array:

$cur_array = array();
$cur_array[0] = new arrayInfo();
$cur_array[1] = new arrayInfo();

..something like that. Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.