使用substr将PHP添加到数组
我有点奇怪的问题。 我通过邮局发送了数据:使用substr将PHP添加到数组,php,arrays,Php,Arrays,我有点奇怪的问题。 我通过邮局发送了数据: Array ( [test_amount_9] => 9 [sell_cost_9] => 102.41 [cost_quote_9] => 2253.02 [test_amount_10] => 10 [sell_cost_10] => 13.68 [cost_quote_10] => 451.44 [q] => /portal/orders/creat
Array
(
[test_amount_9] => 9
[sell_cost_9] => 102.41
[cost_quote_9] => 2253.02
[test_amount_10] => 10
[sell_cost_10] => 13.68
[cost_quote_10] => 451.44
[q] => /portal/orders/create-commerical-offer
)
然后我有一个foreach,它循环遍历每个项并使用substr测试前4个字符,如果是,则将其添加到数组中
$items = array();
foreach($input as $item => $key) {
// if first 4 chars are sell, remove sell_cost_ and save the ID
if(substr($item, 0, 4) == 'sell') {
$id = str_replace(substr($item, 0, 10), '', $item);
$cost = $key;
$items[$id] = array(
'id' => $id,
'sell_cost' => $cost
);
// if first 4 chars are cost, add to array
} elseif(substr($item, 0, 4) == 'cost') {
$id = str_replace(substr($item, 0, 11), '', $item);
$items[$id]['price_per_case'] = $key;
// add amount to array
} elseif(substr($item, 0, 4) == 'test') {
$id = str_replace(substr($item, 0, 12), '', $item);
$items[$id]['amount'] = 'sdadassda';
//$items[$id]['amount'] = $key;
//$items['shouldbe'][] = array($item => $key);
} else {
$items['unknown'][] = array($item => $key);
}
}
前两个substr有效,我得到了这个结果
Array
(
[9] => Array
(
[id] => 9
[sell_cost] => 102.41
[price_per_case] => 2253.02
)
[10] => Array
(
[id] => 10
[sell_cost] => 13.68
[price_per_case] => 451.44
)
[unknown] => Array
(
[0] => Array
(
[q] => /portal/orders/create-commerical-offer
)
)
)
我不知道这笔钱要花在哪里。我尝试过更改名称(因此使用前缀test),但数据似乎消失了。
谢谢。当您计算成本时,$items数组将被覆盖(并且您在重置cost代码中的数组之前处理金额) 这将起作用(我把数量放在末尾而不是开头) 编辑:这是您在“成本”中重置数组的代码(添加金额后,因此此处删除金额):
如果将数组项的键转换为数组,如$itemArr=explode(“”,$itemKey),则会有更好的方法来处理它们。 例如,id将是$id=$itemArr[count($itemArr)-1],而不是像这样进行比较:substr($item,0,4)=='cost',您可以这样做:$itemArr[0]=='cost',而不必知道$item的长度限制
$items = array();
foreach ($input as $item => $key) {
$itemArr = explode('_', $item);
$id = $itemArr[count($itemArr)-1];
switch ($itemArr[0]) {
case 'sell':
$items[$id] = array(
'id' => $id,
'sell_cost' => $key,
);
break;
case 'cost':
$items[$id] = array(
'id' => $id,
'price_per_case' => $key,
);
break;
case 'test':
$items[$id] = array(
'id' => $id,
'amount' => $key,
);
break;
default:
$items['unknown'] = array(
$item => $key,
);
break;
}
}
要意识到,如果您有多个未知项目,您将只拥有最后一个。我不知道问题出在哪里。在ELSE子句中有'q',它执行它应该执行的操作?数组缺少金额,当前应为$items[$id]['amount']='sdadassda';另外,“q”是由Laravel添加的。但是您没有任何输入数据会在ifelse子句中包含amount?没有一个数组键以“test”开头?“test\u amount\u 9”和“test\u amount\u 10”都可以。它们不会添加到数组中,也不会添加到“未知”数组中。太简单了。我在每一步中都会抛出一条isset语句,以确保它不会被覆盖。这永远是最简单的解决办法,嗯?谢谢
$items[$id] = array(
'id' => $id,
'sell_cost' => $cost
);
It's very straight $items array is overwritten, to avoid this Just set $items as multidimensional ARRAY, it won't overwrite.
$input =Array
(
'test_amount_9' => 9,
'sell_cost_9' => 102.41,
'cost_quote_9' => 2253.02,
'test_amount_10' => 10,
'sell_cost_10' => 13.68,
'cost_quote_10' => 451.44,
'q' => '/portal/orders/create-commerical-offer'
);
$items = array();
foreach($input as $item => $key) {
// if first 4 chars are sell, remove sell_cost_ and save the ID
if(substr($item, 0, 4) == 'sell') {
$id = str_replace(substr($item, 0, 10), '', $item);
$cost = $key;
$items[][$id] = array(
'id' => $id,
'sell_cost' => $cost
);
// if first 4 chars are cost, add to array
} elseif(substr($item, 0, 4) == 'cost') {
$id = str_replace(substr($item, 0, 11), '', $item);
$items[][$id]['price_per_case'] = $key;
// add amount to array
} elseif(substr($item, 0, 4) == 'test') {
$id = str_replace(substr($item, 0, 12), '', $item);
$items[][$id]['amount'] = 'sdadassda';
//$items[][$id]['amount'] = $key;
//$items[]['shouldbe'][] = array($item => $key);
} else {
$items[]['unknown'][] = array($item => $key);
}
}
**Output**
Array
(
[0] => Array
(
[9] => Array
(
[amount] => sdadassda
)
)
[1] => Array
(
[9] => Array
(
[id] => 9
[sell_cost] => 102.41
)
)
[2] => Array
(
[9] => Array
(
[price_per_case] => 2253.02
)
)
[3] => Array
(
[10] => Array
(
[amount] => sdadassda
)
)
[4] => Array
(
[10] => Array
(
[id] => 10
[sell_cost] => 13.68
)
)
[5] => Array
(
[10] => Array
(
[price_per_case] => 451.44
)
)
[6] => Array
(
[unknown] => Array
(
[0] => Array
(
[q] => /portal/orders/create-commerical-offer
)
)
)
)
Hope It Hepls!!
If you want to format it according to you just do a foreach()
$items = array();
foreach ($input as $item => $key) {
$itemArr = explode('_', $item);
$id = $itemArr[count($itemArr)-1];
switch ($itemArr[0]) {
case 'sell':
$items[$id] = array(
'id' => $id,
'sell_cost' => $key,
);
break;
case 'cost':
$items[$id] = array(
'id' => $id,
'price_per_case' => $key,
);
break;
case 'test':
$items[$id] = array(
'id' => $id,
'amount' => $key,
);
break;
default:
$items['unknown'] = array(
$item => $key,
);
break;
}
}