dongzhen4180 2012-05-24 18:04
浏览 23
已采纳

在循环对象的方法时是否有更好的方法来创建数组?

I'm using

foreach ($objs as $obj) {
    $data[] = $obj->getValue;
}

to collect values from a method which may return

array[4]{
    [0]=>
    string(2) "1234"
    [1]=>
    string(7) "5678"
    [2]=>
    string(7) "9ab"
    [3]=>
    string(10) "cdefg"
    ...
}

But I need that data in a multidimensional associative array format like...

array[2]{
[0] =>
    array[2]{
        ["alpha"]=>
        string(2) "1234"
        ["beta"]=>
        string(7) "5678"
    }
[1] =>
    array[2]{
        ["alpha"]=>
        string(7) "9ab"
        ["beta"]=>
        string(10) "cdefg"
}

I've been able to do this with two independent loops, one that loops the method data into a two dimensional array, and the second that loops through that array and manually changes the index values to associative values.

    $key_labels = array('alpha','beta');
    $row_num = $col_num = 0;
    $rows = array(
                array(),
                array()
            );

    /* Parse the query into a two-dimensional array */
    foreach ($objs as $obj) {
        $rows[$row_num][$col_num++] = $obj->nodeValue;

        if ($col_num == count($key_labels)) {   
            $col_num = 0;
            $row_num++;
        }
    }

    /* Change the array second dimension index values to associative values */
    foreach ($rows as $rows_idx => $row) {
        unset($rows[$rows_idx]);
        foreach ($row as $row_idx => $row_val) {
            $rows[$rows_idx][$key_labels[$row_idx]] = $row_val;
        }
    }

    var_dump($rows);

My question is... is there a way to accomplish this more directly than using two verbose (and seemingly clunky) loops? Or is this really the best way?

Note: It is expected that the object will always return a number of rows equal to some even factor of the $key_labels count.

  • 写回答

4条回答 默认 最新

  • dqv2743 2012-05-24 18:14
    关注

    untested, and needs php 5.3. But you can just replace the array_map and anonymous function with another foreach if you dont have 5.3

    $vals = array();
    foreach ($objs as $obj) {
        $vals[] = $obj->nodeValue;
    }
    
    $key_labels = array('alpha','beta');
    
    $result = array_map(array_chunk($vals, count($key_labels)), function($chunkOfVals) use ($key_labels) {
        return array_combine($key_labels, $chunkOfVals);
    });
    

    edit - heres the non 5.3 way. To be honest, I like this way better after writing it out. Much more clear.

    $result = array();
    foreach (array_chunk($vals, count($key_labels)) as $chunkOfVals) {
        $result[] = array_combine($key_labels, $chunkOfVals);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch