【问题标题】:php convert array in another format?php 将数组转换成另一种格式?
【发布时间】:2014-04-05 21:17:32
【问题描述】:

在 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.
        )

)

那么有人可以告诉我如何像上面那样制作我的数组吗?任何帮助和建议都将非常可观。谢谢

【问题讨论】:

    标签: php arrays arraylist multidimensional-array


    【解决方案1】:

    试试看

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

    【讨论】:

      【解决方案2】:

      试试

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

      查看演示here

      【讨论】:

        猜你喜欢
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-27
        相关资源
        最近更新 更多