RE: [PHP] assign associative array values to variables? SOLVED
On 18 March 2009 13:21, PJ advised: Thank you, once again, dg. Could not get it to work... whatever... But I did find an unexpected solution which made things very simple. This is the part for the categories section: if ( isset( $book_categories[$bookID] ) ) { foreach ( $book_categories[$bookID] AS $categoryID ) { if ( isset( $category[$categoryID] ) ) { *$catvar = array_values($category[$categoryID]); $cat = $catvar[1];* $catn = preg_replace(/[^a-zA-Z0-9]/, , $cat); echo a href='categories/, $catn, .php', $cat, /anbsp;nbsp; ; } } } I realise I'm a bit behind the curve on this, but this looks like a rather convoluted way of doing what you want to do. However, it's rather difficult to tell without *specifics* -- can you post the output from var_dump($book_categories) and var_dump($category), and a snippet of what you want the eventual output from that to look like, so we can actually work out the most efficient route from one to the other? (It might also be helpful to know where $book_categories and $category come from originally -- are they the results of a database query?) Cheers! Mike -- Mike Ford, Electronic Information Developer, C507, Leeds Metropolitan University, Civic Quarter Campus, Woodhouse Lane, LEEDS, LS1 3HE, United Kingdom Email: m.f...@leedsmet.ac.uk Tel: +44 113 812 4730 To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables? SOLVED
Ford, Mike wrote: On 18 March 2009 13:21, PJ advised: Thank you, once again, dg. Could not get it to work... whatever... But I did find an unexpected solution which made things very simple. This is the part for the categories section: if ( isset( $book_categories[$bookID] ) ) { foreach ( $book_categories[$bookID] AS $categoryID ) { if ( isset( $category[$categoryID] ) ) { *$catvar = array_values($category[$categoryID]); $cat = $catvar[1];* $catn = preg_replace(/[^a-zA-Z0-9]/, , $cat); echo a href='categories/, $catn, .php', $cat, /anbsp;nbsp; ; } } } I realise I'm a bit behind the curve on this, but this looks like a rather convoluted way of doing what you want to do. However, it's rather difficult to tell without *specifics* -- can you post the output from var_dump($book_categories) and var_dump($category), and a snippet of what you want the eventual output from that to look like, so we can actually work out the most efficient route from one to the other? (It might also be helpful to know where $book_categories and $category come from originally -- are they the results of a database query?)/email.htm The results are from a db query. What's the /email.htm ? var_dump($book_categories) outputs a series of arrays for all the books in the db - which is not what I want. var_dump($categories) outputs the same plus those that have a second author; 28 for book_categories, 34 for categories. Actually, I'm about to ask if someone would like to look at the code and see what could be done to improve it. I still have to do some sanitizing and formatting the code better for HTML output as well as adding LIMIT stuff to show only a few pages since eventually the db could hold upwards of a thousand entries. I'm a little hesitant to post the script as it is almost 200 lines; if you like, I can either post the whole page or upload the stuff to the web site as I will have to do it eventually. Here are the categories and book_categories queries: $book_categories = array(); $SQL = SELECT bookID, categories_id FROM book_categories; if ( ( $results = mysql_query($SQL, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $book_categories[$row['bookID']][] = $row['categories_id']; } } $category = array(); $SQL = SELECT id, category FROM categories; if ( ( $results = mysql_query($SQL, $db) ) !== false ) { while ( $row = mysql_fetch_assoc($results) ) { $category[$row['id']] = $row; } } The book_categories query returns all the categories for all the books. I haven't been able to limit it to just one book at a time; adding WHERE book_categories.bookID = book.id does not seem to work... is my syntax wrong? -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables? SOLVED
dg wrote: On Mar 17, 2009, at 6:47 PM, PJ wrote: The problem is that the $array returns another array (multidimensional and associative ?? ) ?php $array_branch_one = array(value_one = red, value_two = blue); $array_branch_two = array(value_three = green, value_four = black); $array_main = array(branch_one = $array_branch_one,branch_two = $array_branch_two); extract($array_main[branch_one]); extract($array_main[branch_two]); print $value_one, $value_two, $value_three, $value_four; ? Thank you, once again, dg. Could not get it to work... whatever... But I did find an unexpected solution which made things very simple. This is the part for the categories section: if ( isset( $book_categories[$bookID] ) ) { foreach ( $book_categories[$bookID] AS $categoryID ) { if ( isset( $category[$categoryID] ) ) { *$catvar = array_values($category[$categoryID]); $cat = $catvar[1];* $catn = preg_replace(/[^a-zA-Z0-9]/, , $cat); echo a href='categories/, $catn, .php', $cat, /anbsp;nbsp; ; } } } Because the array had keys that had various ids [number] and the value was [category], I could not extract the category. By reassigning the array I reset the key/value to [0] and [1]. That's in this, as above: $catvar = array_values($category[$categoryID]); $cat = $catvar[1]; The rest was a piece of cake. :-) 8-) -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables?
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']); } } } this will echo something like CivilizationGods And GoddessesHistorical PeriodsSociology Anthropology (I have not added breaks beween the categories as there is more manipulation needed on them) This works for as many categories as needed. Manipulating each value should not be a problem once it is in a string variable using switch and preg_replace() as each category needs to be stripped of spaces, commas and s. Perhaps extract() ?? http://www.php.net/manual/en/function.extract.php -- Thodoris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] assign associative array values to variables?
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']); } } } this will echo something like CivilizationGods And GoddessesHistorical PeriodsSociology Anthropology (I have not added breaks beween the categories as there is more manipulation needed on them) This works for as many categories as needed. Manipulating each value should not be a problem once it is in a string variable using switch and preg_replace() as each category needs to be stripped of spaces, commas and s. -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables?
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. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables?
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 ** the output of $myvar above is Array. That, I knew. And that does not extract. I can see what is in the array in several ways; the problem is to get the output into $var1, $var2, $var3 ... etc. How do I convert: $category[$categoryID]['category'] into up to 4 different variables that I can manipulate? My code above outputs text like this: CivilizationGods And GoddessesHistorical PeriodsSociology Anthropology -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables?
dg wrote: If I'm understanding your question correctly, this may help: ?php $array = array(value_one = red, value_two = blue); extract($array); print $value_one, $value_two; Thanks for the suggestion. I've seen that example and have tried it as well as about a dozen others. The problem is that the $array returns another array (multidimensional and associative ?? ) print_r($myarray) returns for example: Array ( [id] = 5 [category] = Some category) Array ( [id] = 33 [category] = Another category) So, you see, I can't do anything with that. I had hoped to be able to use something like count() or $row() or foreach or while but no go. foreach may have a possibility but it it rather convoluted and I'm trying to avoid it; hoping for something simpler, more logical and more elegant. ? On Mar 17, 2009, at 4:27 PM, 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']); } } } this will echo something like CivilizationGods And GoddessesHistorical PeriodsSociology Anthropology (I have not added breaks beween the categories as there is more manipulation needed on them) This works for as many categories as needed. Manipulating each value should not be a problem once it is in a string variable using switch and preg_replace() as each category needs to be stripped of spaces, commas and s. -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] assign associative array values to variables?
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/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php