Access array in PHP
How do I access the foll开发者_StackOverflow中文版owing array in PHP:
$Record = Array ( [0] => 1 [1] => 1 [2] => 1);
I have tried
echo $Record[0];
But no luck :(
To initialize an array (you don't need an associative array, if your keys are just the actual indices) use:
$record = array(1, 1, 1);
Then you can access the first element via:
$first = $record[0];
Try
$Record = array( 0 => 1, 1 => 1, 2 => 1);
or even
$Record = array(1,1,1);
and then
echo $Record[0];
Keep in mind that print_r shows you some form of representation of the array. so this code:
$record1 = array(1,1,1);
print_r($record1);
will output:
Array
(
[0] => 1
[1] => 1
[2] => 1
)
$Record = Array ( 1, 1, 1 );
your array has wrong syntax
just a guess, but you can try something like that:
$pattern = "|\[(\d+)\] => (\d+)|";
preg_replace_callback(
$pattern,
"add_to_array",
$text);
and write a function 'add_to_array' to add to your array, get the index by $matches[1] and the value by $matches[2]!
精彩评论