如何prePEND阵列与我的选择键和值(PHP)另一个数组中的每个元素? [英] How to prepend array to each element of another array with my choice of key and value (in php)?
问题描述
这code:
为($ P = 0; $ P<计数($结果); $ P ++){
的foreach($结果[$ P]为$ K => $ V){
$ typeee = ['型'=>用strtolower(str_replace函数('_','',$结果[$ P] [$ k]的['指标']))集装箱]。
array_insert2($结果[$ P],0,$ typeee);
}
}
的print_r($结果);
给我这样的:
阵列(
[0] =>排列
(
[类型] = GT; pagestoriescontainer
[0] =>排列
(
[类型] = GT; pagestories
[OBJECT_ID] => 123456778
[指标] => page_stories
[END_TIME] => 1386057600
[期] => 86400
[值] => 2090
) [1] =>排列
(
[类型] = GT; pagestorytellers
[OBJECT_ID] => 123456778
[指标] => page_storytellers
[END_TIME] => 1386057600
[期] => 86400
[值] => 2041
)
[...][1] =>排列
( [0] =>排列
(
[类型] = GT; pagestories
[OBJECT_ID] => 199193463123456778
[指标] => page_stories
[END_TIME] => 1386057600
[期] => 86400
[值] => 0
) [1] =>排列
(
[类型] = GT; pagestorytellers
[OBJECT_ID] => 199193463123456778
[指标] => page_storytellers
[END_TIME] => 1386057600
[期] => 86400
[值] => 0
) [...]
但是,这code:
为($ P = 0; $ P<计数($结果); $ P ++){
的foreach($结果[$ P]为$ K => $ V){
$ typeee = ['型'=>用strtolower(str_replace函数('_','',$结果[$ P] [$ k]的['指标']))集装箱]。
array_insert2($结果[$ P],$ K,$ typeee);
}
}
不给我这样的:
阵列(
[0] =>排列
(
[类型] = GT; pagestoriescontainer
[0] =>排列
(
[类型] = GT; pagestories
[OBJECT_ID] => 123456778
[指标] => page_stories
[END_TIME] => 1386057600
[期] => 86400
[值] => 2090
)
[类型] = GT; pagestorytellerscontainer
[1] =>排列
(
[类型] = GT; pagestorytellers
[OBJECT_ID] => 123456778
[指标] => page_storytellers
[END_TIME] => 1386057600
[期] => 86400
[值] => 2041
)
[...][1] =>排列
(
[类型] = GT; pagestoriescontainer
[0] =>排列
(
[类型] = GT; pagestories
[OBJECT_ID] => 199193463123456778
[指标] => page_stories
[END_TIME] => 1386057600
[期] => 86400
[值] => 0
)
[类型] = GT; pagestorytellerscontainer
[1] =>排列
(
[类型] = GT; pagestorytellers
[OBJECT_ID] => 199193463123456778
[指标] => page_storytellers
[END_TIME] => 1386057600
[期] => 86400
[值] => 0
) [...]
为什么呢?我怎样才能得到我想要什么? :)
此外,
函数array_insert2(安培; $数组$地位,$ insert_array){
$ first_array = array_splice($数组,0,$位置);
$阵列= array_merge($ first_array,$ insert_array,$阵列);
}
array_insert2的第二个参数是位置。在第一个code你实际上是给它一个整数使它成为一个有效的位置($ P,与array_splice功能相结合)。
在第二片$ C $的c,提供到array_insert2位置是从$结果[$ P] foreach循环密钥($ K)。给出的键不能用array_splice功能正确使用。也许的供应,而不是$ K,给出$ K的$结果array_search结果[$ P]。
简答:
在第二片$ C $的c供给的位置不是一个整数,使得它不可用于所述array_splice功能
This code :
for ($p=0;$p<count($results);$p++){
foreach ($results[$p] as $k => $v){
$typeee = ['type' => strtolower(str_replace('_','',$results[$p][$k]['metric']))."container"];
array_insert2($results[$p],0,$typeee);
}
}
print_r($results);
gives me this:
Array (
[0] => Array
(
[type] => pagestoriescontainer
[0] => Array
(
[type] => pagestories
[object_id] => 123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 2090
)
[1] => Array
(
[type] => pagestorytellers
[object_id] => 123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 2041
)
[...]
[1] => Array
(
[0] => Array
(
[type] => pagestories
[object_id] => 199193463123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[1] => Array
(
[type] => pagestorytellers
[object_id] => 199193463123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[...]
But this code:
for ($p=0;$p<count($results);$p++){
foreach ($results[$p] as $k => $v){
$typeee = ['type' => strtolower(str_replace('_','',$results[$p][$k]['metric']))."container"];
array_insert2($results[$p],$k,$typeee);
}
}
DOES NOT give me this:
Array (
[0] => Array
(
[type] => pagestoriescontainer
[0] => Array
(
[type] => pagestories
[object_id] => 123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 2090
)
[type] => pagestorytellerscontainer
[1] => Array
(
[type] => pagestorytellers
[object_id] => 123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 2041
)
[...]
[1] => Array
(
[type] => pagestoriescontainer
[0] => Array
(
[type] => pagestories
[object_id] => 199193463123456778
[metric] => page_stories
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[type] => pagestorytellerscontainer
[1] => Array
(
[type] => pagestorytellers
[object_id] => 199193463123456778
[metric] => page_storytellers
[end_time] => 1386057600
[period] => 86400
[value] => 0
)
[...]
Why? How can I get what I want? :)
Also,
function array_insert2 (&$array, $position, $insert_array) {
$first_array = array_splice ($array, 0, $position);
$array = array_merge ($first_array, $insert_array, $array);
}
array_insert2's second param is the position. In the first code you are actually giving it an integer making it a valid position ( $p, combined with the array_splice function ). In the second piece of code, the position supplied to array_insert2 is the key ( $k ) from the foreach loop on $results[$p]. The key given cannot be used correctly with the array_splice function. Maybe instead of supplying $k, give the array_search result of $k in $results[$p].
short-answer: The position supplied in the second piece of code is not an integer, making it not usable for the array_splice function.
这篇关于如何prePEND阵列与我的选择键和值(PHP)另一个数组中的每个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!