如果value等于key,则将值存储在多维数组中

时间:2014-10-08 11:39:41

标签: php arrays multidimensional-array

我正在尝试存储与音乐值相关的数字

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

进入多维数组

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

我希望得到以下内容

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

目前我正在尝试通过

进行array_push
//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);
        }

    }

但到目前为止还没有运气。我错过了一些非常明显的东西,我开始觉得有点愚蠢吗?

提前感谢任何对我来说非常简单的事情的帮助!

2 个答案:

答案 0 :(得分:2)

我将提供一种在多维数组中存储和检索数据的简单方法:

<?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

?>

答案 1 :(得分:1)

我喜欢FoxNos's answer,但不喜欢全局,因为全局在另一个上下文中可能不是全局的($categories可能在另一个函数或类中定义。)

所以这就是我要做的:

$categories = array(.........);

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

push_to_category($categories, "house","2797");

由于第一个arg是引用的,因此您不需要全局,因此您可以在任何地方使用它。小改进。

如果你在另一个职能或班级里面,想要push_to_category很多,你甚至可以这样做:

class UserController {
  function categoriesAction() {

    $categories = array(.........);

    $push_to_category = function($category, $value) use (&$categories) {
      array_push($categories[$category], $value);
    }

    $push_to_category("house","2797");

  }
}

使得本地函数(closure)使用/操作局部变量($categories)。