php如何求同列元素之和,如何prePEND阵列与我的选择键和值(PHP)另一个数组中的每个元素?...

This code :

for ($p=0;$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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值