在下面的输出中检索数组的值?

时间:2016-05-12 05:05:02

标签: php arrays codeigniter

我有这个数组显示在 print_r

Array ( [service_list] => 
    Array ( [0] => 
        Array ( [ratechart_id] => 15 
                [country_id] => 15 
                [sp_id] => 1 
                [.5kg] => 135 
                [1kg] => 225 
                [1.5kg] => 290 
                [2kg] => 360 
                [2.5kg] => 425 
                [3kg] => 480 
                [3.5kg] => 530 
                [4kg] => 585 
                [4.5kg] => 640 
                [5kg] => 690 
                [excess] => 100 
                [transit_time] => 4 
                [time_created] => 2016-05-09 18:24:14 
                [modified_date] => 0000-00-00 00:00:00 
            ) 
        ) 
        [volume] => 5.95kg 
    )

现在我要检索音量的值 5.95kg

我尝试使用 foreach循环,但是我无法获得通过我使用codeigniter的方式来控制我调用函数的方式

$data['service_list'] = $this->home_model->Search();
$data['volume'] = $this->input->post('volume');


foreach ($list as $row)
{
 echo $row['volume'];
}

2 个答案:

答案 0 :(得分:2)

不需要foreach循环。只需回显您的音量指数。

查看代码。

$array = Array ( 'service_list' => Array ( 0 => Array ( 'ratechart_id' => 15, 'country_id' => 15, 'sp_id' => 1, '.5kg' => 135, '1kg' => 225, '1.5kg' => 290, '2kg' => 360, '2.5kg' => 425, '3kg' => 480, ) ), 'volume' => '5.95kg' );

echo "volume: ". $array['volume'];
print_r($array);

<强>输出

Volume : 5.95kg

Array
(
    [service_list] => Array
        (
            [0] => Array
                (
                    [ratechart_id] => 15
                    [country_id] => 15
                    [sp_id] => 1
                    [.5kg] => 135
                    [1kg] => 225
                    [1.5kg] => 290
                    [2kg] => 360
                    [2.5kg] => 425
                    [3kg] => 480
                )

        )

    [volume] => 5.95kg
)

答案 1 :(得分:0)

嘿,你可以使用以下代码

    <?php


    $array = Array ('service_list' => Array ( 0 => Array ( 'ratechart_id' => 15, 'country_id' => 15, 'sp_id' => 1, '.5kg' => 135, '1kg' => 225, '1.5kg' => 290, '2kg' => 360, '2.5kg' => 425, '3kg' => 480, ) ), 'volume' => '5.95kg' );


    foreach ($array as $key => $list)
    {
        if ($key === 'volume')
        {

            echo $list;
            continue;
        }

    }


    ?>