bossbowser bossbowser - 19 days ago 5
PHP Question

Store value in multidimensional array if value is equal to key

I'm trying to store the numbers associated with the music values below

Store 2797 in house
Store 2797 in rnb
Store 2797 in hiphop
Store 2829 in house
Store 2829 in trap
Store 2829 in hiphop


Into the multidimensional array which is such

Array (
[house] => Array ( )
[indie] => Array ( )
[trap] => Array ( )
[trance] => Array ( )
[partybangers] => Array ( )
[charthits] => Array ( )
)


Which I would like to result in the following

Array (
[house] => Array (2797,2829)
[indie] => Array ( )
[trap] => Array (2829)
[trance] => Array ( )
[partybangers] => Array ( )
[charthits] => Array ( )
)


Currently i'm attempting to array_push by

//Create Multidimensional Array to store each ID into dependent on music value matching $musictypestofetch
$outputmultidimensionalevent = array();
foreach ($musictypestofetch as $musicarray){
$tempmusicarray = array();
$outputmultidimensionalevent[$musicarray] = $tempmusicarray;
}
print_r($outputmultidimensionalevent); //Temporary print out
foreach ($grabbed as $grab)
{
$holdmusicvalues = array(); //Reset Array
$grabmusicvalues = mysqli_query($dbhandle_word, "SELECT term.name
FROM wp_2_term_relationships AS rel
LEFT JOIN wp_2_term_taxonomy AS tax
ON rel.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN wp_2_terms as term
ON tax.term_id = term.term_id
WHERE rel.object_id = '".$grab."'
AND tax.taxonomy = 'Music'
AND term.name != 'nightclub'
AND term.name != 'bigspender'
AND term.name != 'average'
AND term.name != 'cheap'
AND term.name != 'boosted'
AND term.name != 'recommended'
AND term.name != 'Mon'
AND term.name != 'Tue'
AND term.name != 'Wed'
AND term.name != 'Thu'
AND term.name != 'Fri'
AND term.name != 'Sat'
AND term.name != 'Sun'
AND term.name != 'central'
AND term.name != 'north'
AND term.name != 'east'
AND term.name != 'south'
AND term.name != 'west'
AND term.name != 'events'
AND term.name != 'gaylesbian'"); //Recommended can be removed once the pass over has been done to boosted in the back end of wordpress and all other functions
while ($musiceventvalues = mysqli_fetch_array($grabmusicvalues)){
array_push($holdmusicvalues,$musiceventvalues[0]);
}
foreach ($holdmusicvalues as $musictostore){
echo '<br/>Store ' . $grab . ' in ' . $musictostore;
array_push($outputmultidimensionalevent[$musictostore],$grab);
}

}


But so far no luck. Am I missing something massively obvious here, I'm starting to feel a bit stupid?

Thanks in advance for any help on something that should be very simple for me to work out!

Answer

I will provide a simple way to store and retrive your data inside that multidimensional array:

<?php
//Global definition of the array
$categories = array(
    "house" => array(),
    "indie" => array(),
    "trap" => array(),
    "trance" => array(),
    "partybangers" => array(),
);

function push_to_category($category,$value) {
    global $categories;
    array_push($categories[$category], $value);
}

push_to_category("house","2797");

//To retrive data you call $categories[$category][index]
//ex) $categories["house"][0] returns 2797

?>