phoenicoperu Posted August 1, 2011 Share Posted August 1, 2011 I have a group of data on this Associative Array with different values and possible equal keys like: industry => adasdsad, industry => helloworld, other => almhfe321, environment => loremipsum I´m tryin to get the values in separate chunks, with the same key each. Any help on this would be really appreciated. Note: This trying to not use a multidemsional array, if possible. Quote Link to comment https://forums.phpfreaks.com/topic/243508-getting-all-values-of-the-same-associative-key-in-array/ Share on other sites More sharing options...
manix Posted August 1, 2011 Share Posted August 1, 2011 well you can just loop through the array and check the matching or w/e it is you're trying to do foreach($your_array as $key => $value){ check if $key matches and do stuff you wanna do } Quote Link to comment https://forums.phpfreaks.com/topic/243508-getting-all-values-of-the-same-associative-key-in-array/#findComment-1250346 Share on other sites More sharing options...
gizmola Posted August 1, 2011 Share Posted August 1, 2011 Your assumption is incorrect. A key in an array is unique. If you do this: $foo['mykey'] = 'Apple'; $foo['mykey'] = 'Grape'; echo $foo['mykey']; What do you expect the output to be? The only way you could associate multiple "things" with a key, is to use a nested array: $foo['mykey'] = array('apple', 'grape', 'banana'); var_dump($foo); $foo['mykey'] = array('fruit' => 'apple', 'entre' => 'pb&j', 'drink' => 'milk'); var_dump($foo); Arrays in php are very flexible and easy to work with once you understand the syntax of how to do certain things. If there's something you don't understand how to do, please ask. Quote Link to comment https://forums.phpfreaks.com/topic/243508-getting-all-values-of-the-same-associative-key-in-array/#findComment-1250409 Share on other sites More sharing options...
phoenicoperu Posted August 2, 2011 Author Share Posted August 2, 2011 Thank you very much for answering so fast. And yes i see your point now, sounds logic. im getting from a database, Category->various subcategories each, that i want to save in an Array, (will have to be multidimensional as you mention) But how can i add values that i'm getting of a database, to a multidimensional array through a loop like: //sql stuff myarray = new array(); while ($row=mysql_fetch_array($result)) { myarray[] = array ( "$row['category']" => "$row['subcategory']"); } i have this... but i need something like you said, how would i do it? mmmh im thinkin.... while ($row=mysql_fetch_array($result)) { myarray[] = array ( "$row['category']" => array ("$row['subcategory']") ); } ??? wrong direction?? i need this array to create a view with tables, each table with title as "category" and all its "subcategories" below of that category. tyvm Quote Link to comment https://forums.phpfreaks.com/topic/243508-getting-all-values-of-the-same-associative-key-in-array/#findComment-1250506 Share on other sites More sharing options...
gizmola Posted August 2, 2011 Share Posted August 2, 2011 You are in the right direction, but the question is: is this a nested hierarchy (tree)? That is tricky. What is the structure of a single row you're fetching and how does $row['category'] relate to $row['subcategory']? Quote Link to comment https://forums.phpfreaks.com/topic/243508-getting-all-values-of-the-same-associative-key-in-array/#findComment-1250531 Share on other sites More sharing options...
phoenicoperu Posted August 2, 2011 Author Share Posted August 2, 2011 it's fetched from 3 tables with a triple joined query and i was getting only 2 columns for each fetch cat->subcat that's why i was having repeated keys instead of using multidimensional arrays, but i fixed it using them and php wins again =) Took me a long night but i found out how to fix it, using multi arrays. Thank you very much for the tips! (Note: this is my favorite forum on all the web now!) Quote Link to comment https://forums.phpfreaks.com/topic/243508-getting-all-values-of-the-same-associative-key-in-array/#findComment-1250708 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.