PHP 关联数组 - 将逗号分隔值拆分为单独的数组行
PHP Associative Array - Split comma separated value into separate array rows
我有一个关联数组,“CAT”中的一些值有多个单词,用逗号分隔。
我想将它们拆分成单独的数组条目(每个逗号分隔的单词创建另一个数组条目),如下所示。
我知道我可以使用 explode()
用逗号分隔这些值,但不确定如何从中创建新的数组条目?
$categories = explode(',', $details[0]['CAT']);
当前数组结构:
Array
(
[0] => Array
(
[ID] => 50829
[CAT] => "furniture,home,garden"
)
[1] => Array
(
[ID] => 50832
[CAT] => "kids"
)
[2] => Array
(
[ID] => 50854
[CAT] => "toys"
)
)
想要的结果:
Array
(
[0] => Array
(
[ID] => 50829
[CAT] => "furniture"
)
[1] => Array
(
[ID] => 50829
[CAT] => "home"
)
[2] => Array
(
[ID] => 50829
[CAT] => "garden"
)
[3] => Array
(
[ID] => 50832
[CAT] => "kids"
)
[4] => Array
(
[ID] => 50854
[CAT] => "toys"
)
)
非常感谢您的帮助!
虽然您总是可以使用非常神秘的技巧,但通常很容易完全按照您描述的方式完成这项工作。您想要展开 CAT 索引并创建一个新数组,每个值都与原始索引相关联。所以,就这样做吧:
// Assume $old_array is your original array.
$new_array = array(); // Originally empty.
foreach($old_array as $e) { // For each element in the old array...
$cats = explode(',',$e['CAT']);
// $cats is every word exploded (one word if there is no comma).
foreach($cats as $cat) { // For each word in $cats...
$new_array[] = array('ID'=>$e['ID'], 'CAT'=>$cat);
// Add a pair to the new array that has the ID and the cat word.
}
}
完成后,$new_array 就是您要求的分解版本。
您可以简单地使用 foreach 重新创建一个新数组。对于“CSV 字符串”中的每个类别,您可以在输出数组中添加“ID”+“新 CAT”:
$array = [
['ID' => 50829, 'CAT' => 'furniture,home,garden'],
['ID' => 50832, 'CAT' => 'kids'],
['ID' => 50854, 'CAT' => 'toys'],
];
$out = []; // output array
foreach ($array as $item) { // for each entry of the array
$cats = explode(',', $item['CAT']); // split the categories
foreach ($cats as $cat) { // for each cat
$out[] = [ // create a new entry in output array
'ID' => $item['ID'], // with same ID
'CAT' => $cat, // the "sub" category
];
}
}
var_export(array_values($out));
array (
0 =>
array (
'ID' => 50829,
'CAT' => 'furniture',
),
1 =>
array (
'ID' => 50829,
'CAT' => 'home',
),
2 =>
array (
'ID' => 50829,
'CAT' => 'garden',
),
3 =>
array (
'ID' => 50832,
'CAT' => 'kids',
),
4 =>
array (
'ID' => 50854,
'CAT' => 'toys',
),
)
或者创建一个没有“CAT”的相同数组:
$out = [];
foreach ($array as $item) {
$cats = explode(',', $item['CAT']);
unset($item['CAT']); // unset the original "CAT"
foreach ($cats as $cat) {
$out[] = $item + ['CAT' => $cat]; // copy $item and add "CAT"
}
}
我有一个关联数组,“CAT”中的一些值有多个单词,用逗号分隔。
我想将它们拆分成单独的数组条目(每个逗号分隔的单词创建另一个数组条目),如下所示。
我知道我可以使用 explode()
用逗号分隔这些值,但不确定如何从中创建新的数组条目?
$categories = explode(',', $details[0]['CAT']);
当前数组结构:
Array
(
[0] => Array
(
[ID] => 50829
[CAT] => "furniture,home,garden"
)
[1] => Array
(
[ID] => 50832
[CAT] => "kids"
)
[2] => Array
(
[ID] => 50854
[CAT] => "toys"
)
)
想要的结果:
Array
(
[0] => Array
(
[ID] => 50829
[CAT] => "furniture"
)
[1] => Array
(
[ID] => 50829
[CAT] => "home"
)
[2] => Array
(
[ID] => 50829
[CAT] => "garden"
)
[3] => Array
(
[ID] => 50832
[CAT] => "kids"
)
[4] => Array
(
[ID] => 50854
[CAT] => "toys"
)
)
非常感谢您的帮助!
虽然您总是可以使用非常神秘的技巧,但通常很容易完全按照您描述的方式完成这项工作。您想要展开 CAT 索引并创建一个新数组,每个值都与原始索引相关联。所以,就这样做吧:
// Assume $old_array is your original array.
$new_array = array(); // Originally empty.
foreach($old_array as $e) { // For each element in the old array...
$cats = explode(',',$e['CAT']);
// $cats is every word exploded (one word if there is no comma).
foreach($cats as $cat) { // For each word in $cats...
$new_array[] = array('ID'=>$e['ID'], 'CAT'=>$cat);
// Add a pair to the new array that has the ID and the cat word.
}
}
完成后,$new_array 就是您要求的分解版本。
您可以简单地使用 foreach 重新创建一个新数组。对于“CSV 字符串”中的每个类别,您可以在输出数组中添加“ID”+“新 CAT”:
$array = [
['ID' => 50829, 'CAT' => 'furniture,home,garden'],
['ID' => 50832, 'CAT' => 'kids'],
['ID' => 50854, 'CAT' => 'toys'],
];
$out = []; // output array
foreach ($array as $item) { // for each entry of the array
$cats = explode(',', $item['CAT']); // split the categories
foreach ($cats as $cat) { // for each cat
$out[] = [ // create a new entry in output array
'ID' => $item['ID'], // with same ID
'CAT' => $cat, // the "sub" category
];
}
}
var_export(array_values($out));
array (
0 =>
array (
'ID' => 50829,
'CAT' => 'furniture',
),
1 =>
array (
'ID' => 50829,
'CAT' => 'home',
),
2 =>
array (
'ID' => 50829,
'CAT' => 'garden',
),
3 =>
array (
'ID' => 50832,
'CAT' => 'kids',
),
4 =>
array (
'ID' => 50854,
'CAT' => 'toys',
),
)
或者创建一个没有“CAT”的相同数组:
$out = [];
foreach ($array as $item) {
$cats = explode(',', $item['CAT']);
unset($item['CAT']); // unset the original "CAT"
foreach ($cats as $cat) {
$out[] = $item + ['CAT' => $cat]; // copy $item and add "CAT"
}
}