Re: assign associative array values to variables?

From: Date: Wed, 18 Mar 2009 01:01:50 +0000
Subject: Re: assign associative array values to variables?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-290198@lists.php.net to get a copy of this message
PJ wrote:
Chris wrote:
PJ wrote:
I have been tearing out my hair to figure out a way to place array values into $variables with not much luck. I can echo the array to the screen but I can not manipulate the results. I have searched wide and far all day on the web and I find nothing that points the way how to extract values from an associative array and assign them to a variable. I expected to find some elegant way to do it. Here's the code that outputs the values: if ( isset( $book_categories[$bookID] ) ) { foreach ( $book_categories[$bookID] AS $categoryID ) {
      if ( isset( $category[$categoryID] ) ) {
          echo($category[$categoryID]['category']);
          }
      }
}
Same as other variable assignment. if ( isset( $category[$categoryID] ) ) {
    $myvar = $category[$categoryID];
} echo $myvar . "<br/>"; Doing it with a multi-dimensional array is no different to a result set from a database or any other scenario.
I probably did not express myself clearly; I thought I did: "place array * *values ** into * *$variables **"
Try this $my_categories = array(); foreach (......) {
    if (isset($category[$categoryID])) {
        $my_categories[] = $category[$categoryID]['category'];
    }
} At the end, you can either: foreach ($my_categories as $category_name) { .. do something } or $category_list = implode(',', $my_categories); echo "The categories are ${category_list}\n"; -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.general (#290198) next »