php转换数组的另一种格式?

时间:2014-03-04 11:06:51

标签: php arrays arraylist multidimensional-array

在php和mysql的帮助下,我从数据库中获取了一些数据。所以我的代码看起来像这样

mysql_connect(_DB_SERVER_,_DB_USER_,_DB_PASSWD_) or die(mysql_error());
mysql_select_db(_DB_NAME_) or die(mysql_error());

$sql_listing="SELECT "._DB_PREFIX_."product.id_product,"._DB_PREFIX_."product.id_category_default,description_short,"._DB_PREFIX_."product_lang.name FROM "._DB_PREFIX_."product_lang,"._DB_PREFIX_."product WHERE "._DB_PREFIX_."product_lang.id_lang=1 AND "._DB_PREFIX_."product.id_product="._DB_PREFIX_."product_lang.id_product";

$qry_listing=mysql_query($sql_listing);

现在,当我执行print_r($ qry_listing)来检查结果数组时,这会显示我的结果

Array
(
    [description_short] => New design. New features. Now in 8GB and 16GB. iPod nano rocks like never before.
    [name] => iPod Nano
)
Array
(
    [description_short] => iPod shuffle, the world’s most wearable music player, now clips on in more vibrant blue, green, pink, and red.
    [name] => iPod shuffle
)

但我希望我的结果应该是这样的

Array
(
    [iPod Nano] => Array
        (
            [description_short] => New design. New features. Now in 8GB and 16GB. iPod nano rocks like never before.
        )

    [iPod shuffle] => Array
        (

            [description_short] => iPod shuffle, the world’s most wearable music player, now clips on in more vibrant blue, green, pink, and red.
        )

)

所以有人可以告诉我如何像上面一样制作我的阵列吗?任何帮助和建议都会非常明显。感谢

2 个答案:

答案 0 :(得分:0)

尝试

$result =array();
foreach($array as $arr){
  $result[$arr['name']]['description_short'] = $arr['description_short'];
}

请参阅演示here

答案 1 :(得分:0)

尝试

foreach($qry_listing as $myArr) {
     $newArr[$myArr['name']]['description_short'] = $myArr['description_short'];
}
print_r($newArr);