All Questions
Tagged with php-internals arrays
17
questions
8
votes
3
answers
433
views
Duplicate array keys (Notice: member variable "a" returned from __sleep() multiple times)
The title may seem a bit silly but I'm totally serious with this. Today at work I came across a weird PHP behaviour which I could not explain. Luckily this behaviour is fixed in PHP 7.4, so it seems ...
1
vote
2
answers
236
views
Micro optimization on array keys
I have an array of which I am using some items to construct more arrays, a rough example follows.
$rows = [
[1, 2, 3, 'a', 'b', 'c'],
[4, 5, 6, 'd', 'e', 'f'],
[4, 5, 6, 'g', 'h', 'i'],
];...
5
votes
0
answers
288
views
PHP, Compile foreach and opline
I'm trying to figure out how a foreach statement is interpreted by PHP. That led me to use gdb while executing a dummy foreach script.
I end up in zend_compile.c, in the zend_compile_foreach() ...
3
votes
3
answers
128
views
Which one of the following PHP array structure would be using less memory?
Which one of the following would be using less memory?
$myArray = array();
$myArray[1] = array(1,2,3,4,5,6,7,8,9,10);
$myArray[2] = array(1,2,3,4,5,6,7,8,9,10);
$myArray[3] = array(1,2,3,4,5,6,7,8,9,...
6
votes
1
answer
234
views
Does PHP optimize function arguments of array type, not explicitly passed by reference, when they are not modified?
Would the PHP engine optimize the second example to pass the $arr by reference?
function test1(array &$arr)
{
$arr[] = 123;
echo $arr[0];
}
function test2(array $arr)
{
echo $arr[0];...
22
votes
4
answers
4k
views
How does PHP memory actually work
I've always heard and searched for new php 'good writing practice', for example: It's better (for performance) to check if array key exists than search in array, but also it seems better for memory ...
2
votes
3
answers
805
views
What algorithm does array_sum use in PHP?
what algorithm does array sum uses to make it much faster than some looping through?
Is it prefix sum / suffix sum or something else?
5
votes
0
answers
837
views
Why does accessing a null value in PHP as an array not generate a warning? [duplicate]
I was hit by a bug where a returned DB result was expected to be an array but due to a glitch in the SQL query in one instance the DB wrapper correctly returned null. Unfortunately the code did not ...
6
votes
6
answers
4k
views
Increasing array elements while in foreach loop in php? [duplicate]
Consider the code below:
<?php
$arr = array();
$arr['b'] = 'book';
foreach($arr as $key=>$val) {
print "key=>$key\n";
if(!isset($arr['a']))
$arr['a'] = 'apple';
}
?>
It is ...
1
vote
2
answers
939
views
PHP uksort function using global variable fails after PHP upgrade to 5.3.3
I have a user defined sort function which uses a 'global' declaration in order to refer to a multi-dimensional array when deciding on the correct sort order. It used to work fine under PHP 5.1.6 but ...
14
votes
2
answers
3k
views
Why does foreach copy the array when we did not modify it in the loop? [duplicate]
In a blog post "PHP Internals: When does foreach copy", NikiC stated that in a code like this:
Snippet 1
$array = range(0, 100000);
foreach ($array as $key => $value) {
xdebug_debug_zval('...
2
votes
2
answers
246
views
What's the behavior of count when array has more than 2147483647 elements?
On a 32-bit system, an array can have as much as 4294967295 elements (as per Artefacto's post on another thread).
However, count returns the number of elements as an int, and on a 32-bit system, an ...
2
votes
1
answer
254
views
Why does a PHP array get modified when it's element is reference-assigned?
When ref-assigning an array's element, the contents of the array are modified:
$arr = array(100, 200);
var_dump($arr);
/* shows:
array(2) {
[0]=>
int(100) // ← ← ← int(100)
[1]=>
int(...
3
votes
1
answer
284
views
Make last array parameter optional in php class method (C)
I am creating a PHP extension in C to access the SPI interface. So far I have gotten pretty much everything working: php_spi on Github
However, I cannot seem to make the $options parameter in the ...
1
vote
2
answers
244
views
PHP array syntax/operator?
When writing the syntax for an associative array in PHP we do the following
$a = array('foo' => 'bar');
I am curious of the relationship of the => syntax, or possibly operator. Does this ...