多维数组键引用

时间:2013-07-26 13:25:58

标签: php multidimensional-array

我有一个多维数组给我这个:

            [ShipmentServiceLevelCategory] => test
            [OrderTotal] => Array
                (
                    [Amount] => 11.11
                    [CurrencyCode] => GBP
                )

            [ShipServiceLevel] => Std UK Dom
            [MarketplaceId] => test
            [ShippingAddress] => Array
                (
                    [Phone] => 123213213213
                    [PostalCode] => TES T12
                    [Name] => Test
                    [CountryCode] => GB
                    [StateOrRegion] => Test
                    [AddressLine1] => Test
                    [City] => Test
                )

我想将此信息插入到我的数据库中,但我的列名与数组键不同,因此我尝试创建另一个包含键映射的数组。这是我到目前为止所尝试的:

$map = array('ShippingAddress['Phone']' => 'DEL_PHONE','ShippingAddress['PostalCode']'=> 'DEL_POSTCODE','ShippingAddress['Name']' => 'DEL_NAME');

然而,这给了我一个语法错误,有人可以指出我在哪里错了吗?

3 个答案:

答案 0 :(得分:0)

$map = array('ShippingAddress' => array (
'Phone' => 'DEL_PHONE',
'PostalCode' => 'DEL_POSTCODE',
'Name' => 'DEL_NAME' 
) );

答案 1 :(得分:0)

这是获取当前数组值并在不同数组中使用它们的正确语法

$new_array = array();

$new_array['DEL_PHONE'] = $current_array['ShippingAddress']['Phone'];

$new_array['DEL_POSTCODE'] = $current_array['ShippingAddress']['PostalCode'];

这应该会让你对如何继续

有一个好主意

答案 2 :(得分:0)

您可以制作如下数组:

define("DEL_PHONE", "col1");
define("DEL_NAME", "col2");
$attributes = array(
DEL_PHONE => $data['ShippingAddress']['Phone'],
DEL_NAME => $data['ShippingAddress']['PostalCode']
);