1

我有这个数组:

Array (
    [0] => Rolex  (more)
    [1] => WG Submariner dial (only)
    [2] => 116619
    [3] => MINT
    [4] =>   
    [5] =>   
    [6] =>   
    [7] =>   
    [8] => WTB: Want To Buy
    [9] => Aug 7, 2013, 7:00 AM
    [10] => EUR 1,000 (in USD)
    [11] => EUR  *(estimate)
    [12] =>
)

我正在尝试将其转换为这样的关联数组:

Array (
    [brand] => Rolex  (more)
    [Series] => WG Submariner dial (only)
    [Model] => 116619
    [Condition] => MINT
    [Box] =>   
    [Papers] =>   
    [Year] =>   
    [Case Size] =>   
    [Status] => WTB: Want To Buy
    [Listed] => Aug 7, 2013, 7:00 AM
    [Asking Price] => EUR 1,000 (in USD)
    [Retail Price] => EUR  *(estimate)
    [Payment Info] =>
)

我正在考虑创建另一个数组,然后遍历该数组以分配名称键

除了这个还有什么建议吗?

4

2 回答 2

3

您可能想看看array_combine

您仍然需要两个数组,但您不必循环创建关联数组。

所以你会有

$indexedArray = array( 'Rolex (more)', 'WG Submariner dial (only)', '116619' /* etc */ );
$keys = array('Brand', 'Series', 'Model', /* etc */);

$associativeArray = array_combine($keys, $indexedArray);
于 2013-08-14T06:46:27.193 回答
0

这是我所做的:

$params = array('brand'=>'brand','series'=>'series','model'=>'model','condition'=>'condition','box'=>'box','papers'=>'papers','year'=>'year','case size'=>'case size','status'=>'status','listed'=>'listed','asking price'=>'asking price','retail price'=>'retail price','payment info'=>'payment info');

Array (
    [0] => Rolex  (more)
    [1] => WG Submariner dial (only)
    [2] => 116619
    [3] => MINT
    [4] =>   
    [5] =>   
    [6] =>   
    [7] =>   
    [8] => WTB: Want To Buy
    [9] => Aug 7, 2013, 7:00 AM
    [10] => EUR 1,000 (in USD)
    [11] => EUR  *(estimate)
    [12] =>
)

$final_arr = array_combine($params,$post_meta);
            print_r($final_arr);

Array ( [brand] => Rolex  (more) [series] => WG Submariner dial (only) [model] => 116619 [condition] => MINT [box] =>   [papers] =>   [year] =>   [case size] =>   [status] => WTB: Want To Buy [listed] => Aug 7, 2013, 7:00 AM [asking price] => EUR 1,000 (in USD) [retail price] => EUR  *(estimate) [payment info] =>   ) 
于 2013-08-14T07:05:10.190 回答