为多维关联数组赋值

时间:2015-09-04 19:48:27

标签: php arrays multidimensional-array

有没有办法为关联数组赋值而不用数值数组创建另一个维度?

$ champion [$ row [' Champion']] [' New'] = $ row [' New'];

像这样的数组

this

'新'我怎么能访问这个值?

$champion=array();

                    $champions_table= $conn->prepare("SELECT Champion, New, Passive, Q, Q2, W, W2, E, E2, R, R2 FROM champions Where Patch_No = ? ORDER BY ID ASC");
                    $champions_table->bind_param('s', $Patch_No);
                    $champions_table->execute();
                    $champions_table_result = $champions_table->get_result();

while($row = $champions_table_result->fetch_assoc()){
                        //Getting General Changes
                        $champion[$row['Champion']]['New'] = $row['New'];
}

2 个答案:

答案 0 :(得分:0)

获取所有值。

foreach ( $champion as $key => $value) {
    echo "Champion : ".$key." Value: ".$value['New']." <br />\n";
}

如果你知道冠军名字

echo $champion['championname']['New'] <br />\n";

示例

echo $champion['Blitzcrank']['New'] <br />\n";

如果您希望稍微扩展一个3维数组,可以更轻松地查看它是如何分配给

while($row = $champions_table_result->fetch_assoc()){
        $value = $row['Champion'];
        $champion['Champion'][$value]= array('New' => $row['New']);
}

获取值

echo $champion['Champion']['Blitzcrank']['New'] <br />\n";    

答案 1 :(得分:0)

假设这是您的数据库的结果:

$row = array('Champion' => 'champ', 'New' => '1');

这应该有效:

$champion[$row['Champion']]['New'] = $row['New'];
echo $champion[$row['Champion']]['New'];

应输出1