如何在php

时间:2015-12-16 09:11:35

标签: php arrays

我想从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 )

如何在php变量中获取productcode值并回显它?

示例:

602793420,123870196,416267436

2 个答案:

答案 0 :(得分:3)

您可以使用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];
    }
?>

答案 1 :(得分:0)

如果项目的选项将来会有更多的值,我宁愿建议这样做。

-- TABLE A
CREATE TABLE [dbo].[A]
    [EVS_MO_ID] [bigint] NOT NULL,
    [DRUG_ID] [varchar](50) NOT NULL,
    [ATC_CODE] [varchar](15) NULL,
    [DRUG_NAME] [varchar](1024) NULL,
    [PATIENT_ID] [varchar](50) NOT NULL,
    [PATIENT_LOCATION] [varchar](10) NULL,
    [MO_DATE] [datetime2](7) NOT NULL,
    [MO_START_DATE] [datetime2](7) NOT NULL,
    [MO_STOP_DATE] [datetime2](7) NULL,
    [ROUTE] [varchar](50) NULL,
    [MEDICATION_CONTAINER] [smallint] NULL,
    [PRESCRIBING_DOCTOR_NAME] [varchar](50) NULL,
    [PRESCRIBING_DOCTOR_SURNAME] [varchar](50) NULL,
    [MO_ACTIVE] [bit] NOT NULL,
 CONSTRAINT [PK_MedicationOrders] PRIMARY KEY CLUSTERED 
(
    [EVS_MO_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [dbo].[A]           
     VALUES
           (5411409,'97941689', 'B01AB06','NADROPARINE 0.8ML','7092425','ANBC', '2015-12-15 20:58:06.2030000',
           '2015-12-16 00:00:00.0000000', '', 'IV', 1, 'GEORGE','LAST', 1);
-- TABLE B
CREATE TABLE [dbo].[B](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ARTIKELNUMMER] [varchar](50) NOT NULL,
    [VLAG54] [bit] NULL,
    [VLAG55] [bit] NULL CONSTRAINT [DF_Table_1_VLAG50]  DEFAULT ((0)),
    [VLAG100] [bit] NULL CONSTRAINT [DF_ArtikelVlaggen_VLAG100]  DEFAULT ((0)),
 CONSTRAINT [PK_B] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


INSERT INTO [dbo].[B]
           ([ARTIKELNUMMER]
           ,[VLAG54]
           ,[VLAG55]
           ,[VLAG100])
     VALUES
           ('97941689', 1,0,1);

           -- TABLE C
CREATE TABLE [dbo].[C](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [OBX_DATETIME] [datetime2](7) NOT NULL,
    [PID] [int] NOT NULL,
    [DEPARTMENT] [varchar](8) NOT NULL,
    [OBX_ID] [int] NOT NULL,
    [OBX_VALUE] [decimal](5, 2) NOT NULL,
    [OBX_UNITS] [varchar](10) NULL,
    [REF_RANGE] [varchar](40) NULL,
    [FLAG] [varchar](2) NULL,
 CONSTRAINT [PK_C] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [dbo].[C]
           ([OBX_DATETIME]
           ,[PID]
           ,[DEPARTMENT]
           ,[OBX_ID]
           ,[OBX_VALUE]
           ,[OBX_UNITS]
           ,[REF_RANGE]
           ,[FLAG])
     VALUES
           ('2015-12-15 14:01:00.0000000',7092425, '8NAH', 20937, 27.00, 'mL/min', '> 60', 'L');

INSERT INTO [dbo].[C]
           ([OBX_DATETIME]
           ,[PID]
           ,[DEPARTMENT]
           ,[OBX_ID]
           ,[OBX_VALUE]
           ,[OBX_UNITS]
           ,[REF_RANGE]
           ,[FLAG])
     VALUES
           ('2015-12-15 06:30:00.0000000',7092425, '6ZPA', 20937, 28.00, 'mL/min', '> 60', 'L');

由于 阿米特