0

想知道是否可以做任何改进来加速这个数组?

测试数组行数为 40,000 条记录。

循环完成需要很长时间。

样本值:

$test[] = array(
    'products_id' => $data['products_id'],
    'products_extra_fields_id' => $data['products_extra_fields_id'],
    'products_extra_fields_name' => $data['products_extra_fields_name'],
    'products_extra_fields_value' => $data['products_extra_fields_value']
    );



foreach productid
{

  $colname="column name";


    //this inside for loop making thing very slow
    foreach ($test as $key1 => $row1)
    {
        $w= $row1['products_id'];
        $x= $row1['products_extra_fields_id'];
        $y= $row1['products_extra_fields_name'];
        $z= $row1['products_extra_fields_value'];

        if( ($w==$products_id) && ($y== $colname) )
        {
            $wantedvalue=$z;
            break;
        }
    }

}
4

2 回答 2

0

我认为这不是您需要编辑的数组。如果要从数据库中检索这些数据,则必须加快 sql 查询速度。此外,您可以将索引添加到您的字段。这是您可以参考的链接。 http://www.catswhocode.com/blog/10-sql-tips-to-speed-up-your-database

于 2012-04-18T06:22:33.380 回答
0
  1. 您可以将数组 $test[] 构建为字典(用户 products_id . "_" . products_extra_fields_name 作为键)

    $test[$data['products_id'] ."_".$data['products_extra_fields_name']] = array('products_id' => $data['products_id'], 'products_extra_fields_id' => $data['products_extra_fields_id' ], 'products_extra_fields_name' => $data['products_extra_fields_name'], 'products_extra_fields_value' => $data['products_extra_fields_value'] );

一旦你这样做了,你可以直接索引数组,因为你已经有了列名和 products_id

于 2012-04-18T06:23:33.763 回答