Php 从var_导出中剥离数值数组键
我要做的是去掉数组上所有的数字数组键。我的阵列输出如下:Php 从var_导出中剥离数值数组键,php,arrays,Php,Arrays,我要做的是去掉数组上所有的数字数组键。我的阵列输出如下: array ( 2 => array ( 1 => array ( 'infor' => 'Radiation therapy & chemo subhead', 'PPOWithNotif' => '', 'PPOWithOutNotif' => 'Radiation therapy & chemo PPO amount',
array (
2 =>
array (
1 =>
array (
'infor' => 'Radiation therapy & chemo subhead',
'PPOWithNotif' => '',
'PPOWithOutNotif' => 'Radiation therapy & chemo PPO amount',
'NonPPO' => 'Radiation therapy & chemo Non PPO amount',
),
),
3 =>
array (
1 =>
array (
'infor' => 'Allergy testing & treatment subhead',
'PPOWithNotif' => '',
'PPOWithOutNotif' => 'Allergy testing & treatment PPO amount',
'NonPPO' => 'Allergy testing & treatment Non PPO amount',
),
)
)
通过这样做,我可以根据需要洗牌数组值,而不必担心数值数组值
我尝试过使用echo preg\u replace(“/[0-9]+\=\>/I“,”,var\u export($data))代码>但它什么都不做。有什么建议吗?我的正则表达式有什么我没做的吗?有更好的解决方案吗?为什么不使用:
PHP还有一个函数,它将为您洗牌数组,然后使用foreach
循环或next
/方法,您可以按随机顺序将其拉出 您必须将var\u export
的第二个参数设置为true
,否则您的preg\u replace
调用没有返回值
参考:
返回
如果使用并设置为TRUE,var\u export()将返回变量
表示而不是输出它
更新:回顾这个问题,我有一个预感,一个简单的数组\u值($input)
就足够了。可能不是你想要的答案,但是如果你有一个一级数组,你可以使用下面的函数。它可能不漂亮,但对我来说效果很好
function arrayToText($array, $name = 'new_array') {
$out = '';
foreach($array as $item) {
$export = var_export($item, true);
$export = str_replace("array (\n", '', $export);
$export = substr($export, 0, -1);
$out .= "[\n";
$out .= $export;
$out .= "],\n";
}
return '$' . $name . ' = ' . "[\n" . substr($out, 0, -2) . "\n];";
}
echo arrayToText($array);
你能更清楚地知道你最终要做什么吗?比如,数组的前后,或者你想从中得到什么?因为如果将var_export()作为数组的字符串表示形式,它就不再是一个可以洗牌的数组了。我想从这个字符串中删除数字数组键……对……我明白了,但接下来要讨论洗牌数组值。如果您使用var_export将数组转换为字符串,它就不再是数组,您根本无法对其进行洗牌,因为它不再是数组,而是字符串。例如:当我认为目标只是避免var_export
添加显式数字索引时,array_值将重新对数组进行索引。
function arrayToText($array, $name = 'new_array') {
$out = '';
foreach($array as $item) {
$export = var_export($item, true);
$export = str_replace("array (\n", '', $export);
$export = substr($export, 0, -1);
$out .= "[\n";
$out .= $export;
$out .= "],\n";
}
return '$' . $name . ' = ' . "[\n" . substr($out, 0, -2) . "\n];";
}
echo arrayToText($array);