简体   繁体   中英

How to get a single value from an associative array PHP

When I code =>

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

I get the following out put and error=>

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

When I modify the code as follows =>

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

I get the following output and error =>

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

How can I possibly reach single items which are apparently in array $filename1...? Thank you all

I was only able to recover the single values with the below code=>

$filename2= $this->session->flashdata('item');
$filename1= $filename2['upload_data']['file_name'];

Where the session was created with the following code in another page=>

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

Thanks to all responders...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM