Php get variable as array in
// Before php 5.4
$array = array(1,2,3);
// since php 5.4 , short syntax
$array = [1,2,3];
// I recommend using the short syntax if you have php version >= 5.4
Used to creating arrays like this in Perl?
Looks like we need the range() function in PHP:
$array = array_merge (array( ‘All’ ), range ( ‘A’ , ‘Z’ ));
?>
You don’t need to array_merge if it’s just one range:
There is another kind of array (php>= 5.3.0) produced by
$array = new SplFixedArray(5);
Standard arrays, as documented here, are marvellously flexible and, due to the underlying hashtable, extremely fast for certain kinds of lookup operation.
Supposing a large string-keyed array
$arr=[‘string1’=>$data1, ‘string2’=>$data2 etc. ]
when getting the keyed data with
php does *not* have to search through the array comparing each key string to the given key (‘string1’) one by one, which could take a long time with a large array. Instead the hashtable means that php takes the given key string and computes from it the memory location of the keyed data, and then instantly retrieves the data. Marvellous! And so quick. And no need to know anything about hashtables as it’s all hidden away.
However, there is a lot of overhead in that. It uses lots of memory, as hashtables tend to (also nearly doubling on a 64bit server), and should be significantly slower for integer keyed arrays than old-fashioned (non-hashtable) integer-keyed arrays. For that see more on SplFixedArray :
Unlike a standard php (hashtabled) array, if you lookup by integer then the integer itself denotes the memory location of the data, no hashtable computation on the integer key needed. This is much quicker. It’s also quicker to build the array compared to the complex operations needed for hashtables. And it uses a lot less memory as there is no hashtable data structure. This is really an optimisation decision, but in some cases of large integer keyed arrays it may significantly reduce server memory and increase performance (including the avoiding of expensive memory deallocation of hashtable arrays at the exiting of the script).
When creating arrays , if we have an element with the same value as another element from the same array, we would expect PHP instead of creating new zval container to increase the refcount and point the duplicate symbol to the same zval. This is true except for value type integer.
Example:
$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => 23 ];
xdebug_debug_zval( ‘arr’ );
(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=0, is_ref=0)int 23
but :
$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => ’23’ ];
xdebug_debug_zval( ‘arr’ );
(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=1, is_ref=0)string ’23’ (length=2)
or :
$arr = [‘bebe’ => ‘Bob’, ‘age’ => [1,2], ‘too’ => [1,2] ];
xdebug_debug_zval( ‘arr’ );
(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
‘too’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
This function makes (assoc.) array creation much easier:
function arr (. $array )< return $array ; >
?>
It allows for short syntax like:
$arr = arr ( x : 1 , y : 2 , z : 3 );
?>
Instead of:
$arr = [ «x» => 1 , «y» => 2 , «z» => 3 ];
// or
$arr2 = array( «x» => 1 , «y» => 2 , «z» => 3 );
?>
Sadly PHP 8.2 doesn’t support this named arguments in the «array» function/language construct.
Php get variable as array in
// Before php 5.4
$array = array(1,2,3);
// since php 5.4 , short syntax
$array = [1,2,3];
// I recommend using the short syntax if you have php version >= 5.4
Used to creating arrays like this in Perl?
Looks like we need the range() function in PHP:
$array = array_merge (array( ‘All’ ), range ( ‘A’ , ‘Z’ ));
?>
You don’t need to array_merge if it’s just one range:
There is another kind of array (php>= 5.3.0) produced by
$array = new SplFixedArray(5);
Standard arrays, as documented here, are marvellously flexible and, due to the underlying hashtable, extremely fast for certain kinds of lookup operation.
Supposing a large string-keyed array
$arr=[‘string1’=>$data1, ‘string2’=>$data2 etc. ]
when getting the keyed data with
php does *not* have to search through the array comparing each key string to the given key (‘string1’) one by one, which could take a long time with a large array. Instead the hashtable means that php takes the given key string and computes from it the memory location of the keyed data, and then instantly retrieves the data. Marvellous! And so quick. And no need to know anything about hashtables as it’s all hidden away.
However, there is a lot of overhead in that. It uses lots of memory, as hashtables tend to (also nearly doubling on a 64bit server), and should be significantly slower for integer keyed arrays than old-fashioned (non-hashtable) integer-keyed arrays. For that see more on SplFixedArray :
Unlike a standard php (hashtabled) array, if you lookup by integer then the integer itself denotes the memory location of the data, no hashtable computation on the integer key needed. This is much quicker. It’s also quicker to build the array compared to the complex operations needed for hashtables. And it uses a lot less memory as there is no hashtable data structure. This is really an optimisation decision, but in some cases of large integer keyed arrays it may significantly reduce server memory and increase performance (including the avoiding of expensive memory deallocation of hashtable arrays at the exiting of the script).
When creating arrays , if we have an element with the same value as another element from the same array, we would expect PHP instead of creating new zval container to increase the refcount and point the duplicate symbol to the same zval. This is true except for value type integer.
Example:
$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => 23 ];
xdebug_debug_zval( ‘arr’ );
(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=0, is_ref=0)int 23
but :
$arr = [‘bebe’ => ‘Bob’, ‘age’ => 23, ‘too’ => ’23’ ];
xdebug_debug_zval( ‘arr’ );
(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=0, is_ref=0)int 23
‘too’ => (refcount=1, is_ref=0)string ’23’ (length=2)
or :
$arr = [‘bebe’ => ‘Bob’, ‘age’ => [1,2], ‘too’ => [1,2] ];
xdebug_debug_zval( ‘arr’ );
(refcount=2, is_ref=0)
array (size=3)
‘bebe’ => (refcount=1, is_ref=0)string ‘Bob’ (length=3)
‘age’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
‘too’ => (refcount=2, is_ref=0)
array (size=2)
0 => (refcount=0, is_ref=0)int 1
1 => (refcount=0, is_ref=0)int 2
This function makes (assoc.) array creation much easier:
function arr (. $array )< return $array ; >
?>
It allows for short syntax like:
$arr = arr ( x : 1 , y : 2 , z : 3 );
?>
Instead of:
$arr = [ «x» => 1 , «y» => 2 , «z» => 3 ];
// or
$arr2 = array( «x» => 1 , «y» => 2 , «z» => 3 );
?>
Sadly PHP 8.2 doesn’t support this named arguments in the «array» function/language construct.
PHP variable as array index
Hi all, I have a multi-dimensional array. Using a for function, I would like to display the information inside the array. This means using a variable $i for the first index of the array, which I think is causing the problem. I am getting the error message:
Undefined offset: 2 Note, that the number changes depending on the value of $i. The code I am using is:
But I get the above error when I echo that information inside the for loop. Is this because you can’t use variables as indexes for arrays?
- 3 Contributors
- 6 Replies
- 4K Views
- 1 Hour Discussion Span
- Latest Post 7 Years Ago Latest Post by James_43
Recommended Answers Collapse Answers
Looks like you have an additional level here. Show more of the array. Also choose NUM or ASSOC. I don.t think you need both.
how do I specifiy one or the other
$result = $sth->fetch(PDO::FETCH_ASSOC);
See jkon’s reply in your previous …
All 6 Replies
If this is referring to the code in your other thread, do note that indices start at zero and not one. So check your for loop. If that’s not it, provide some more code (including the loop) so we can see what happens with your variable $i.
Thanks. It is referring to the other thread I just posted. But actually I’ve just realised what I want to do hasn’t worked. My mistake in pulling the mySQL data (in my previous post) was I was using $statement->fetch(); instead of $statement->fetchAll(); Now I have an array: $results. Below is the first row of data so you can see what the array looks like:
Array ( [0] => Array ( [0] => Array ( [blog_id] => 1 [0] => 1 [title] => hello world [1] => hello world [post] => test [2] => test [date] => 2015-09-22 17:21:33 [3] => 2015-09-22 17:21:33 [modified] => 2015-09-22 17:31:33 [4] => 2015-09-22 17:31:33 [author_id] => 4 [5] => 4 )
I am then getting 2 error messages. One is Undefined index: title (which is incorrect since you can see above that there IS an index called title. Secondly, I get: Notice: Undefined offset: 1 , which I get for each item in the array. Any ideas?
Looks like you have an additional level here. Show more of the array. Also choose NUM or ASSOC. I don.t think you need both.
Array ( [0] => Array ( [0] => Array ( [blog_id] => 1 [0] => 1 [title] => hello world [1] => hello world [post] => Test [2] => Test [date] => 2015-09-22 17:21:33 [3] => 2015-09-22 17:21:33 [modified] => 2015-09-22 17:31:33 [4] => 2015-09-22 17:31:33 [author_id] => 4 [5] => 4 ) [1] => Array ( [blog_id] => 2 [0] => 2 [title] => hello world [1] => hello world [post] => Test [2] => Test [date] => 2015-09-22 17:21:33 [3] => 2015-09-22 17:21:33 [modified] => 2015-09-22 17:31:33 [4] => 2015-09-22 17:31:33 [author_id] => 4 [5] => 4 ) [2] => Array ( [blog_id] => 3 [0] => 3 [title] => hello world [1] => hello world [post] => Test [2] => Test [date] => 2015-09-22 17:21:33 [3] => 2015-09-22 17:21:33 [modified] => 2015-09-22 17:31:33 [4] => 2015-09-22 17:31:33 [author_id] => 4 [5] => 4 ) [3] => Array ( [blog_id] => 4 [0] => 4 [title] => hello world [1] => hello world [post] => Test [2] => Test [date] => 2015-09-22 17:21:33 [3] => 2015-09-22 17:21:33 [modified] => 2015-09-22 17:31:33 [4] => 2015-09-22 17:31:33 [author_id] => 4 [5] => 4 ) ) )
I had wondered about the numeric vs associative array. I’m guessing the mySQL query pulls both into the array, how do I specifiy one or the other?
$result = $sth->fetch(PDO::FETCH_ASSOC);