douzhi2017 2013-07-08 19:57
浏览 34

如何在PHP中将数组元素转换为简单元素?

My question may sound somewhat clumsy but that is what I wanted to do. I'm having an array named $data obtained from SQL query. For your reference I'm putting here it's first two elements:

Array
(
    [0] => Array
        (
            [test_pack_id] => 9f27643023a83addd5eed41c4aade840
            [test_pack_name] => Bank Exams Complete Combo
            [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude + 2 Mega tests of all 3 subjects.

Total Tests in this Package : 26
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 399.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 1
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675
            [test_pack_created_date] => 1347127051
            [test_pack_updated_date] => 1349235701
            [test_pack_purchase_date] => 1351228594
        )

    [1] => Array
        (
            [test_pack_id] => e7e95de96987cc7c89c1f0183110fb38
            [test_pack_name] => Bank Reasoning
            [test_pack_desc] => This package contains 8 tests on Reasoning.
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => free
            [test_pack_price] => 0.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 4
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 1347127196
            [test_pack_updated_date] => 1347127387
            [test_pack_purchase_date] => 1363775709
        )
)

Now I've to insert one new key-value pair in this array. For it I'm firing one query onto the database. The query will be fired for each and every element of array $data. The code for it is as below:

foreach($data as $data1) {  

            $sql  = " SELECT COUNT(*) as package_count FROM ". TBL_USER_PACKAGES. " WHERE pack_id ='".$data1['test_pack_id']."' AND pack_assign_date  ";  
            $sql .= " BETWEEN UNIX_TIMESTAMP(CURDATE()) and UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 day)) ";
            $this->mDb->Query( $sql);
            $data1['package_sold_count'] = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE);
}

But when I access the result of this query, I'm getting it in array format like this :

Array(
[0] => Array
        (
            [test_pack_id] => 9f27643023a83addd5eed41c4aade840
            [test_pack_name] => Bank Exams Complete Combo
            [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude + 2 Mega tests of all 3 subjects.

Total Tests in this Package : 26
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => paid
            [test_pack_price] => 399.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 1
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675
            [test_pack_created_date] => 303
            [test_pack_updated_date] => 1349235701
            [test_pack_purchase_date] => 256
            [package_sold_count] => Array
                (
                    [package_count] => 4
                )
[1] => Array
        (
            [test_pack_id] => e7e95de96987cc7c89c1f0183110fb38
            [test_pack_name] => Bank Reasoning
            [test_pack_desc] => This package contains 8 tests on Reasoning.
            [test_pack_type_id] => 3
            [test_pack_image] => 
            [test_pack_validity_year] => 0
            [test_pack_validity_month] => 3
            [test_pack_validity_days] => 0
            [test_pack_plan] => free
            [test_pack_price] => 0.00
            [test_pack_no_tests] => 0
            [test_pack_publish] => yes
            [test_pack_sold] => 4
            [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_updated_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
            [test_pack_created_date] => 303
            [test_pack_updated_date] => 1347127387
            [test_pack_purchase_date] => 110
            [package_sold_count] => Array
                (
                    [package_count] => 2
                )
)

Actually I wanted the array as a new key-value pair instead of a new associative array as follows :

    Array(
    [0] => Array
            (
                [test_pack_id] => 9f27643023a83addd5eed41c4aade840
                [test_pack_name] => Bank Exams Complete Combo
                [test_pack_desc] => This Package contains 24 tests of Reasoning, English and Quantitative Aptitude + 2 Mega tests of all 3 subjects.

    Total Tests in this Package : 26
                [test_pack_type_id] => 3
                [test_pack_image] => 
                [test_pack_validity_year] => 0
                [test_pack_validity_month] => 3
                [test_pack_validity_days] => 0
                [test_pack_plan] => paid
                [test_pack_price] => 399.00
                [test_pack_no_tests] => 0
                [test_pack_publish] => yes
                [test_pack_sold] => 1
                [test_pack_created_staff_id] => ff8d4a5ea6bf11dce105aa2fa7b959b8
                [test_pack_updated_staff_id] => 81c4e3607c7e56bbf5461ef150437675
                [test_pack_created_date] => 303
                [test_pack_updated_date] => 1349235701
                [test_pack_purchase_date] => 256
                [package_sold_count] => 4

)

Can anyone help me in achieving this? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongnachuang6635 2013-07-08 20:00
    关注

    Something as simple as the following should work fine, to do what you want:

    <?php
        foreach ($array as &$subArray) {
            $subArray["package_sold_count"] = $subArray["package_sold_count"]["package_count"];
        }
        unset($subArray);
    ?>
    

    or simply set it differently when you assign it:

    foreach($data as $data1) {  
    
                $sql  = " SELECT COUNT(*) as package_count FROM ". TBL_USER_PACKAGES. " WHERE pack_id ='".$data1['test_pack_id']."' AND pack_assign_date  ";  
                $sql .= " BETWEEN UNIX_TIMESTAMP(CURDATE()) and UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL 1 day)) ";
                $this->mDb->Query( $sql);
    
                //First assign a variable
                $dbFetch = $this->mDb->FetchArray(MYSQL_FETCH_SINGLE); 
    
                //Then refer to it
                $data1['package_sold_count'] = $dbFetch["package_count"]; 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行