我想从php中的数组输出特定值
下面是我的代码和数组是 $content
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$total = $content[$quantity]*$content[$price];
}
?>
<?php
print_r( $content );
?>
输出显示如下:
Array ( [currency] => INR
[shipping] => 0
[tax] => 0
[taxRate] => 0
[itemCount] => 3
[item_name_1] => Our Nest
[item_quantity_1] => 1
[item_price_1] => 1900
[item_options_1] => image: CF01108.jpg, productcode: 602793420
[item_name_2] => Our Nest
[item_quantity_2] => 1
[item_price_2] => 2100
[item_options_2] => image: CF01110.jpg, productcode: 123870196
[item_name_3] => Our Nest
[item_quantity_3] => 1
[item_price_3] => 1800
[item_options_3] => image: CF01106.jpg, productcode: 416267436 )
如何productcode
在php变量中获取值并回显它?
例:
602793420、123870196、416267436
您可以productcode
使用如下explode()
功能:
$product_code = explode("productcode: ", $option)[1];
这是参考:
因此,您的代码应如下所示:
<?php
$content = $_POST;
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$image='item_image_'.$i;
$option='item_options_'.$i;
$product_code = explode("productcode: ", $option)[1];
$total = $content[$quantity]*$content[$price];
}
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句