PHP 的关联数组问题
Associative Array issue with PHP
我正在围绕存储在 API 中的数组的理论场景构建算法,这意味着我不允许编辑它,我正试图从所述数组中获取数据并将其转换使用 implode 和 explode 函数转换为关联数组。我取得了很好的进步,这是我目前的代码……
<?php
$arr = array(
"action: Added; quantity: 1; item_code: RNA1; product_name: Mens Organic T-shirt; colour: White; size: XL",
"action: Subtracted; quantity: 7; item_code: RNC1; product_name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",
"action: Added; quantity: 20; item_code: RNV1; product_name: Gift Voucher; style: Mens; value: £20",
);
// Split the array into strings.
$imploded1 = implode("; ", $arr);
//echo $imploded;
// Seperate the strings by the ";" character.
$exploded1 = explode("; ",$imploded1);
// This gives me the keys and pairs in the same index.
$imploded2 = implode("\n", $exploded1);
// print_r($formatimploded);
$result = array_column(array_map(function($v) {
return explode(":", $v);
}, explode(" ", $imploded2)), 1, 0);
print_r($result);
程序差不多可以运行了。我遇到的问题是键的值没有被添加到数组中,我不确定这是为什么。这是输出:
Array
(
[action] =>
[quantity] =>
[item_code] =>
[product_name] =>
[colour] =>
[size] =>
[style] =>
[value] =>
)
谁能给我解释一下?非常感谢任何帮助。
<?php
$arr = array(
"action: Added; quantity: 1; item_code: RNA1; product_name: Mens Organic T-shirt; colour: White; size: XL",
"action: Subtracted; quantity: 7; item_code: RNC1; product_name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",
"action: Added; quantity: 20; item_code: RNV1; product_name: Gift Voucher; style: Mens; value: £20",
);
//create new array to hold new values
$new_arr = [];
//loop through strings
foreach($arr as $v) {
//break strings into "chunks" (e.g "action: Added", "quantity: 1", etc)
$chunks = explode('; ', $v);
//create temporary array inside "string loop"
$tmp = [];
//loop through each chunk
foreach($chunks as $chunk) {
//split chunks into key => values (e.g "action", "added")
$key_value = explode(': ', $chunk);
//add split chunk to `$tmp` array, using `[0]` as key and `[1]` as value
$tmp[$key_value[0]] = $key_value[1];
}
//after "chunk loop", add `$tmp` to `$new_arr`
$new_arr[] = $tmp;
}
print_r($new_arr);
输出:
Array
(
[0] => Array
(
[action] => Added
[quantity] => 1
[item_code] => RNA1
[product_name] => Mens Organic T-shirt
[colour] => White
[size] => XL
)
[1] => Array
(
[action] => Subtracted
[quantity] => 7
[item_code] => RNC1
[product_name] => Kids Basic T-shirt
[colour] => Denim Blue
[size] => 3-4y
)
[2] => Array
(
[action] => Added
[quantity] => 20
[item_code] => RNV1
[product_name] => Gift Voucher
[style] => Mens
[value] => £20
)
)
如果您首先负责这些数据的实际存储方式,最好以更标准化的格式存储它,例如 JSON。您的数据将更容易处理。
以下是将其中一项存储为 JSON 的方法。请注意,这只是对您当前使用的 semi-colons 和 non-quoted 值的微小更改。
$json = '{"action": "Added", "quantity": "1", "item_code": "RNA1", "product_name": "Mens Organic T-shirt", "colour": "White", "size": "XL"}';
在 PHP 中,您可以简单地使用 json_decode() 将其转换为对象:
$item = json_decode($json);
您可以像这样轻松访问属性:
$item->quantity
$item->colour
$item->size
如果你真的想要一个数组,出于某种原因,你可以这样做:
$item = json_decode($json, true);
给出:
Array
(
[action] => Added
[quantity] => 1
[item_code] => RNA1
[product_name] => Mens Organic T-shirt
[colour] => White
[size] => XL
)
我正在围绕存储在 API 中的数组的理论场景构建算法,这意味着我不允许编辑它,我正试图从所述数组中获取数据并将其转换使用 implode 和 explode 函数转换为关联数组。我取得了很好的进步,这是我目前的代码……
<?php
$arr = array(
"action: Added; quantity: 1; item_code: RNA1; product_name: Mens Organic T-shirt; colour: White; size: XL",
"action: Subtracted; quantity: 7; item_code: RNC1; product_name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",
"action: Added; quantity: 20; item_code: RNV1; product_name: Gift Voucher; style: Mens; value: £20",
);
// Split the array into strings.
$imploded1 = implode("; ", $arr);
//echo $imploded;
// Seperate the strings by the ";" character.
$exploded1 = explode("; ",$imploded1);
// This gives me the keys and pairs in the same index.
$imploded2 = implode("\n", $exploded1);
// print_r($formatimploded);
$result = array_column(array_map(function($v) {
return explode(":", $v);
}, explode(" ", $imploded2)), 1, 0);
print_r($result);
程序差不多可以运行了。我遇到的问题是键的值没有被添加到数组中,我不确定这是为什么。这是输出:
Array
(
[action] =>
[quantity] =>
[item_code] =>
[product_name] =>
[colour] =>
[size] =>
[style] =>
[value] =>
)
谁能给我解释一下?非常感谢任何帮助。
<?php
$arr = array(
"action: Added; quantity: 1; item_code: RNA1; product_name: Mens Organic T-shirt; colour: White; size: XL",
"action: Subtracted; quantity: 7; item_code: RNC1; product_name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",
"action: Added; quantity: 20; item_code: RNV1; product_name: Gift Voucher; style: Mens; value: £20",
);
//create new array to hold new values
$new_arr = [];
//loop through strings
foreach($arr as $v) {
//break strings into "chunks" (e.g "action: Added", "quantity: 1", etc)
$chunks = explode('; ', $v);
//create temporary array inside "string loop"
$tmp = [];
//loop through each chunk
foreach($chunks as $chunk) {
//split chunks into key => values (e.g "action", "added")
$key_value = explode(': ', $chunk);
//add split chunk to `$tmp` array, using `[0]` as key and `[1]` as value
$tmp[$key_value[0]] = $key_value[1];
}
//after "chunk loop", add `$tmp` to `$new_arr`
$new_arr[] = $tmp;
}
print_r($new_arr);
输出:
Array
(
[0] => Array
(
[action] => Added
[quantity] => 1
[item_code] => RNA1
[product_name] => Mens Organic T-shirt
[colour] => White
[size] => XL
)
[1] => Array
(
[action] => Subtracted
[quantity] => 7
[item_code] => RNC1
[product_name] => Kids Basic T-shirt
[colour] => Denim Blue
[size] => 3-4y
)
[2] => Array
(
[action] => Added
[quantity] => 20
[item_code] => RNV1
[product_name] => Gift Voucher
[style] => Mens
[value] => £20
)
)
如果您首先负责这些数据的实际存储方式,最好以更标准化的格式存储它,例如 JSON。您的数据将更容易处理。
以下是将其中一项存储为 JSON 的方法。请注意,这只是对您当前使用的 semi-colons 和 non-quoted 值的微小更改。
$json = '{"action": "Added", "quantity": "1", "item_code": "RNA1", "product_name": "Mens Organic T-shirt", "colour": "White", "size": "XL"}';
在 PHP 中,您可以简单地使用 json_decode() 将其转换为对象:
$item = json_decode($json);
您可以像这样轻松访问属性:
$item->quantity
$item->colour
$item->size
如果你真的想要一个数组,出于某种原因,你可以这样做:
$item = json_decode($json, true);
给出:
Array
(
[action] => Added
[quantity] => 1
[item_code] => RNA1
[product_name] => Mens Organic T-shirt
[colour] => White
[size] => XL
)