This is a discussion on Simple PHP question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I want to be able to randomly select the following from an array: 1). An image 2). A piece of ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I want to be able to randomly select the following from an array:
1). An image 2). A piece of text (name of tge image) 3). A piece of text (description of the image) I want to be able to build a static array with the values hardcoded into the array, and then be able to randomly select an item from the array and retrieve the image, name and description. I am new to PHP, but have been programming C/C++ for over 10 years. In C++ it would look something like this: class ImageInfo { public: ImageInfo(const std::string& path, const std::string& name, const std::string& descr); ImageInfo(const ImageInfo&); ~ImageInfo(); std::string PathName() const ; std::string Name() const ; std::string Description() const ; private: std::string m_path, m_name, m_descr; } static std::vector<ImageInfo> theArray ; static const ImageInfo& getRandomImageInfo() { //generate a random number less than number of items in vector size_t index = random_index(theArray.size()); return theArray[index]; } I need to know how to translate this into PHP. I know PHP does not have the equivalent of the STL, so I will have to use use arrays instead. Any help will be much appreciated. |
|
|||
|
En las nuevas, Ronald Raygun escribió:
> (...) > I need to know how to translate this into PHP. I know PHP does not have the > equivalent of the STL, so I will have to use use arrays instead. Any help > will be much appreciated. (Best view in fixed width font) $images = array( [1] => array( 'path' => '/whatever', 'name' => 'TheName', 'descr' => 'TheDescr') ), [2] => array( 'path' => '/whatever', 'name' => 'TheName', 'descr' => 'TheDescr') ), [3] => array( 'path' => '/whatever', 'name' => 'TheName', 'descr' => 'TheDescr') ), .... [n] => array( 'path' => '/whatever', 'name' => 'TheName', 'descr' => 'TheDescr') ), ); To get name #3... echo $images[3]['name']; The list start in #1, but you can start by 0 or any number or even a text identifier. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|