2

hie i have this array which is showing in 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 
    )

now i want to retrieve the value of volume which is 5.95kg

i tried using the foreach loop but i am not being able to get the value which is by the way i am using codeigniter by which from controller i m calling the function which is

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


foreach ($list as $row)
{
 echo $row['volume'];
}
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42

2 Answers2

2

No need for foreach loop. Just echo your volume index.

CHeck below code.

$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);

Output

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
)
RJParikh
  • 4,096
  • 1
  • 19
  • 36
0

hey you can use following code

    <?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;
        }

    }


    ?>
ram singh
  • 117
  • 5