我对PHP很好,但javascript对我不好,我的英语也不是很好,但我一步一步地解释一切最好和慢慢。 我使用javascript计算电信服务的费用。我知道在php中必须使用类似的逻辑然后我卡住了。
/prices is json from server as see below.
var prices = [{"brand":"Airtel (Waridtel) Ug","numbering":"25670","price":"40"},
{"brand":"UTL ug","numbering":"25671","price":"30"},
{"brand":"Airtel Ug","numbering":"25675","price":"40"},
{"brand":"MTN Ug","numbering":"25677","price":"20"},
{"brand":"MTN Ug","numbering":"25678","price":"20"},
{"brand":"Orange Ug","numbering":"25679","price":"30"},
{"brand":"Safaricom Ke","numbering":"25470","price":"30"},
{"brand":"Safaricom Ke","numbering":"25471","price":"30"}]
用户以逗号分隔字符串
提供各种电话号码 //contacts
cdest = '245772306640,256754306640,25471624525,2567825689754,2563989654285,4588623566', 256782658974;
我们使用品牌前缀来寻找品牌。 如果联系人前缀相当于json中的价格编号,那么这就是该品牌所拥有的conctact。我根据用户提供的联系人列表计算每个品牌的联系人数和总费用。
期望输出如下所示。 品牌(联系人数量)::总费用 示例MTN Ug(3):: 60,Airtel Ug(1),Safaricom Ke(1):: 30,
我也想在没有品牌的情况下放置任何没有品牌的数字,这个品牌被称为国际,没有价格分配,如下例所示 国际(2)::默认,
这里我非常混淆两个循环,我不明白我怎么可以循环,计数和回声但我很好转移PHP知识到目前为止我创建双循环,但后来我再次混淆。
//explode numbers;
dest = cdest.split(",");
// loop through brands and numbers.
//plan is to create create array like (brand1 =>countbrand1,brand2=>countbrand3, etc)
cd = Array();
for( var i=0; i<dest.length; i++ )
{
$x.each(prices, function(index, m) {
if(dest[i].indexOf(m.numbering)==0)
{
cd[m.brand]+=1;
} else
cd[International]+=1;
});
}
在php中,我现在只需在cd上循环并获得输出。我尝试使用js,但不知道如何在Javascript中回显数组的键。 javascript整天欺负我。如果你得到了很好的答案,请逐步展示。我非常感谢。
答案 0 :(得分:0)
在php数组中用于很多东西,但在javascript数组中没有键。对象呢。你在prices
变量中拥有的是一个对象数组。要循环对象,可以使用以下语法:
for (var key in p) {
// do something
// the key has the name `key` and to get the value, you
// can type p[key]
}
这是你的内循环。你得到了正确的外环。这是您使用名称dest
:
for( var i=0; i<dest.length; i++ ) {
var member = dest[i];
// do something with member
}
使用外部循环,我们将获取数组中的每个对象,并将其分配给名为member
的变量。 (当然,你可能会想到一个更好的名字)
所以你的最终代码必须看起来像这样:
for( var i=0; i<dest.length; i++ ) {
var memeber = dest[i];
for (var key in member) {
// do something with the keys and values - print them for example.
}
}
答案 1 :(得分:0)
您可以检查我从您的代码创建的这个小提琴。可以帮到你 http://jsfiddle.net/GSeZ9/
var prices = [{"brand":"Airtel (Waridtel) Ug","numbering":"25670","price":"40"},
{"brand":"UTL ug","numbering":"25671","price":"30"},
{"brand":"Airtel Ug","numbering":"25675","price":"40"},
{"brand":"MTN Ug","numbering":"25677","price":"20"},
{"brand":"MTN Ug","numbering":"25678","price":"20"},
{"brand":"Orange Ug","numbering":"25679","price":"30"},
{"brand":"Safaricom Ke","numbering":"25470","price":"30"},
{"brand":"Safaricom Ke","numbering":"25471","price":"30"}];
var cdest = '245772306640,256754306640,25471624525,2567825689754,2563989654285,4588623566, 256782658974';
var dest = cdest.split(",");
// loop through brands and numbers.
//plan is to create create array like (brand1 =>countbrand1,brand2=>countbrand3, etc)
var cd = {};
for( var i=0; i<dest.length; i++ )
{
$.each(prices, function(index, m) {
// if(dest[i].toString().indexOf(m.numbering.toString()) != -1 )
//alert(dest[i].indexOf(m.numbering));
if(dest[i].toString().indexOf(m.numbering.toString()) == 0)
{
if( cd[m.brand] == undefined)
cd[m.brand] = 1;
else
cd[m.brand]+=1;
} //else
//cd[International]+=1;
});
}
for (var key in cd) {
alert(key + " = " + cd[key]);
}