Welcome Guest, Not a member yet? Register   Sign In
Extract array variable
#1

Hi
I have array that i need to extract it in my file and use it
I extract array in file1.php and need to use file2.php also
But file2.php cannot find it
I can use extract array in files2.php but it is good if i can use in file1.php
Thanks

PHP Code:
extract(array('title' => 'hello')); 
Reply
#2

It's a PHP question:

https://www.php.net/manual/en/function.extract.php
Reply
#3

(02-28-2020, 03:38 AM)wdeda Wrote: It's a PHP question:

https://www.php.net/manual/en/function.extract.php
I know and know how do work extract function
Only i need extract array and use multi files
Reply
#4

Forget about extract - it is just a function. You have a variable, say $foo. How wouuld you make it accessible for different files? You need to store variable somewhere: in a Session for example, or in the Database. You can also use a Class as your Data Transfer Object, or just call global function defined in file1, that is included or required by file2.

Many options, but none of them are related to CI, but rather to general ability to code and your app architecture.
Reply
#5

(02-28-2020, 05:10 AM)zahhar Wrote: Forget about extract - it is just a function. You have a variable, say $foo. How wouuld you make it accessible for different files? You need to store variable somewhere: in a Session for example, or in the Database. You can also use a Class as your Data Transfer Object, or just call global function defined in file1, that is included or required by file2.

Many options, but none of them are related to CI, but rather to general ability to code and your app architecture.

Thank you zahhar
I hold data in flash session and get it in other page and extract it
I could extract only in page that i need
But i like use extracted variables in all pages

Thanks
Reply
#6

Well, if you already know how to use flashdata, you may reuse same concept and utilize Session class for data persistancy between requests: https://codeigniter4.github.io/userguide...sions.html

In file1.php
PHP Code:
$session = \Config\Services::session();
$session->set(array('title' => 'hello')); //no extract() needed 

In file2.php
PHP Code:
$session = \Config\Services::session();
$myTitle $session->get('title'); //now $myTitle has 'hello' value 
Reply
#7

(02-28-2020, 09:22 AM)zahhar Wrote: Well, if you already know how to use flashdata, you may reuse same concept and utilize Session class for data persistancy between requests: https://codeigniter4.github.io/userguide...sions.html

In file1.php
PHP Code:
$session = \Config\Services::session();
$session->set(array('title' => 'hello')); //no extract() needed 

In file2.php
PHP Code:
$session = \Config\Services::session();
$myTitle $session->get('title'); //now $myTitle has 'hello' value 
Thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB