如何从关联数组PHP中获取单个值

时间:2016-04-18 02:21:49

标签: php arrays

当我编码=>

if((isset($filename1))){
    print_r(array_values($filename1));
    $file_name=$filename1['file_name'];
    echo $file_name." this is file_name<br>";
    echo $filename1['file_name']."This isdirectly from the array <br>";
    }

我得到以下输出和错误=&gt;

Array ( [0] => Array ( [file_name] => uygun10.pgn [file_type] => application/x-chess-pgn [file_path] => D:/wamp/www/proje/uploads/ [full_path] => D:/wamp/www/proje/uploads/uygun10.pgn [raw_name] => uygun10 [orig_name] => uygun.pgn [client_name] => uygun.pgn [file_ext] => .pgn [file_size] => 0.48 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => ) )
A PHP Error was encountered

Severity: Notice

Message: Undefined index: file_name

Filename: controllers/Hesaplama.php

Line Number: 58

Backtrace:

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 58
Function: _error_handler

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 263
Function: pgn_oku

File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma

File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
this is file_name
A PHP Error was encountered

Severity: Notice

Message: Undefined index: file_name

Filename: controllers/Hesaplama.php

Line Number: 60

Backtrace:

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 60
Function: _error_handler

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 263
Function: pgn_oku

File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma

File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once

当我修改代码时,如下所示=&gt;

if((isset($filename1))){
print_r(array_values($filename1));
$file_name=$filename1[0]['file_name'];
echo $file_name." this is file_name<br>";
echo $filename1[0]['file_name']."this is directly from the array <br>";
}

我得到以下输出和错误=&gt;

Array ( [0] => Array ( [file_name] => uygun11.pgn [file_type] => application/x-chess-pgn [file_path] => D:/wamp/www/proje/uploads/ [full_path] => D:/wamp/www/proje/uploads/uygun11.pgn [raw_name] => uygun11 [orig_name] => uygun.pgn [client_name] => uygun.pgn [file_ext] => .pgn [file_size] => 0.48 [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => ) )
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: controllers/Hesaplama.php

Line Number: 58

Backtrace:

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 58
Function: _error_handler

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 263
Function: pgn_oku

File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma

File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
this is file_name
A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: controllers/Hesaplama.php

Line Number: 60

Backtrace:

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 60
Function: _error_handler

File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 263
Function: pgn_oku

File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma

File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once

我怎样才能找到显然位于数组$ filename1 ...中的单个项目? 谢谢大家

1 个答案:

答案 0 :(得分:0)

我只能使用以下代码=&gt;

恢复单个值
$filename2= $this->session->flashdata('item');
$filename1= $filename2['upload_data']['file_name'];

使用以下代码在另一个页面中创建会话=&gt;

else { 
            $data = array('upload_data' => $this->upload->data()); 
            $this->load->view('Upload_success', $data); 
         } 
            $this->session->set_flashdata('item', $data);
      }

感谢所有响应者......