FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Explode the content of an array key





jmlworld
I have an array like this:

Code:
$attributes = array(
          'movies'        => 'terminator|Blade II|Fast and Furious',
          'genre'         => 'Action|Action Horro|Comedy'
      )



Now I want to explode the content of the movies key. Here is how I did:
Code:

    if ( $attributes['movies'] ) {

      $movie = explode("|", $attributes['movies'] );
    
      echo "<p>" . $movie . "</p>";
     
    }


But it returns this error:

Quote:
Warning: preg_split() expects parameter 2 to be string, array given...


Please help...
sonam
I don't know how you are getting preg_split error because I don't see this function in your script. But maybe PHP gives this error when you try to echo array. Your last line is wrong. When you explode $attributes['movies'] you are get an array of movies. In that case you need to use foreach, while or for function. Example:

Code:
foreach($movie as $val) {
   echo "<p>" . $val. "</p>";
}


Sonam
jmlworld
sonam wrote:
I don't know how you are getting preg_split error because I don't see this function in your script. But maybe PHP gives this error when you try to echo array. Your last line is wrong. When you explode $attributes['movies'] you are get an array of movies. In that case you need to use foreach, while or for function. Example:

Code:
foreach($movie as $val) {
   echo "<p>" . $val. "</p>";
}


Sonam


Sonam, adding using while or foreach where what I was missing.

I noticed where the preg_split error is coming from though.

Thanks, Sonam.
Related topics
PHP Tutorials
handling the $_POST array
Pagination
-> and =>
PHP: fopen('./directory/$variable'); Problem
Simple contnent rotator
[HELP] PHP (and some MyBB) Administration
PHP Array, explode, count , access array problem.
More traffic to You site or blog
How to code a simple blog using PHP
"PHP Vulnerability May Halt Millions of Servers"
escapeshellcmd seems to blank my user input
What is wrong with this ????
Opencart problem cart.php
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.