I can't insert values from file into php associative array. Can anyone help? The code is below, look at 3rd array item, I'd like to insert key-values from file, but every time I get en error. How to do that? (include, echo, or function result - doesn't work inside array!). How to do that?
<?
$somedeclaredvaiable = '0 => "value1", 1 => "value2", 2 => value3", 3 => "value4", 4 => "value5", 7 => 4';
$a = array(
1 => array(
0 => "value1",
1 => "value2",
2 => "value3",
3 => "value4",
4 => "value5",
7 => 4
),
2 => array(
0 => "value1a",
1 => "value2a",
2 => "value3a",
3 => "value4a",
4 => "value5a",
7 => 4
),
3 => array(
//THE PROBLEM IS HERE, can't include file, or echo an variable!
include 'filewitharray.txt'; //doesn't work
echo $somedeclaredvariable ; //doesn't work
),
);
?>
This part of your code is syntactically incorrect :
3 => array(
//THE PROBLEM IS HERE, can't include file, or echo an variable!
include 'filewitharray.txt'; //doesn't work
echo $somedeclaredvariable ; //doesn't work
),
Assuming that filewitharray.txt
is a php file that would be returning an array (or something else), like this :
<?php
return array('foo' => 'bar');
Then, you could do :
$a[] = require 'filewitharray.txt';
It will add the variable returned by filewitharray.txt
into your table.
If filewitharray.txt
is not supposed to be PHP code and you want the content of the file to be added into the array (as a string), then you could do :
<?php
$a[] = file_get_contents('filewitharray.txt');
Try it now.
<?php
include 'filewitharray.txt';
$somedeclaredvaiable = '0 => "value1", 1 => "value2", 2 => value3", 3 => "value4", 4 => "value5", 7 => 4';
$a = array(
1 => array(
0 => "value1",
1 => "value2",
2 => "value3",
3 => "value4",
4 => "value5",
7 => 4
),
2 => array(
0 => "value1a",
1 => "value2a",
2 => "value3a",
3 => "value4a",
4 => "value5a",
7 => 4
),
3 => array(
),
);
?>
First, get the contents of the file beforehand:
$included = file_get_contents("file with array.txt");
Then in your array declaration:
...snip
3 => array(
$included, $somedeclaredvariable
)
);
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.