Jump to content

Getting all Values of the same associative key in Array


phoenicoperu

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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!)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.