問題描述
我正在嘗試刪除數組鍵名稱中的所有空格,即 str_replace(' ','',$value) (或者最壞的轉換場景將它們替換為下劃線 (_) )
I am trying to remove all spaces in array keys names i.e. str_replace(' ','',$value) (or worst cast scenario replace them with underscores (_) )
并且我正在嘗試在我的多維數組的最深層次(如下所示)執行此操作(因為其他層/層次沒有空格(感謝上帝!))
and I am trying to do this at the deepest level (shown below) of my multidimensional array (because other layers/levels don't have spaces (THANK GOD!))
[...]
[ownPagestoriesbystorytype] => Array
(
[type] => pagestoriesbystorytype
[object_id] => 12365478954
[metric] => page_stories_by_story_type
[end_time] => 1386057600
[period] => 86400
[ownValues] => Array
(
[type] => pagestoriesbystorytypemetrics
[fan] => 1913
[page post] => 153
[user post] => 24
)
)
[ownPagestorytellersbystorytype] => Array
(
[type] => pagestorytellersbystorytype
[object_id] => 12365478954
[metric] => page_storytellers_by_story_type
[end_time] => 1386057600
[period] => 86400
[ownValues] => Array
(
[type] => pagestorytellersbystorytypemetrics
[fan] => 1902
[page post] => 137
[user post] => 9
)
)
[...]
到目前為止,我的嘗試沒有結果:
So far my attempts have been fruitless :
[...]
if (is_array($value))
{
$keys = str_replace(' ','',array_keys($value));
$values = array_values($value);
$value = array_combine($keys,$values);
}
[...]
[...]
foreach ($value as $k => $v)
{
$b = str_replace(' ','',$k);
$value[$b] = $value[$k];
unset ($value[$k]);
}
[...]
上面的代碼不起作用,但是如果我把 print_r($value);在循環結束時,您可以清楚地看到空格正在被刪除,只是不知何故最終結果以空格結束(STILL).
The codes above do not work, however if I put print_r($value); at the end of the loop you can clearly see that spaces are being removed, just somehow the end result ends up being with spaces (STILL).
整個循環如下所示:
for ($i=0;$i<count($results);$i++)
{
for ($j=0;$j<count($results[$i]);$j++)
{
foreach($results[$i][$j] as $key => $value)
{
$typee = ['type' => strtolower(str_replace('_','',$results[$i][$j]['metric']))];
array_insert($results[$i][$j],$typee,0);
if (is_array($value))
{
$keys = str_replace(' ','',array_keys($value));
$values = array_values($value);
$value = array_combine($keys,$values);
$type = ['type' => strtolower(str_replace('_','',$results[$i][$j]['metric']))."metrics"];
array_insert($results[$i][$j]['value'],$type,0);
$results[$i][$j]['ownValues'] = $results[$i][$j][$key];
unset($results[$i][$j][$key]);
}
}
}
}
你可以在這里看到整個數組的樣子:
And you can see how the whole array looks like here:
如何使用我選擇的鍵和值(在 php 中)將數組添加到另一個數組的每個元素?
有什么建議嗎?:)
推薦答案
這將有助于:
function fixArrayKey(&$arr)
{
$arr = array_combine(
array_map(
function ($str) {
return str_replace(" ", "_", $str);
},
array_keys($arr)
),
array_values($arr)
);
foreach ($arr as $key => $val) {
if (is_array($val)) {
fixArrayKey($arr[$key]);
}
}
}
測試如下:
$data = array (
"key 1" => "abc",
"key 2" => array ("sub 1" => "abc", "sub 2" => "def"),
"key 3" => "ghi"
);
print_r($data);
fixArrayKey($data);
print_r($data);
輸入:
Array
(
[key 1] => abc
[key 2] => Array
(
[sub 1] => abc
[sub 2] => def
)
[key 3] => ghi
)
輸出:
Array
(
[key_1] => abc
[key_2] => Array
(
[sub_1] => abc
[sub_2] => def
)
[key_3] => ghi
)
這篇關于如何在php中刪除數組鍵名稱中的空格?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!