0

I typically code in Python, Java, or C. I'm taking on a project in PHP and I'm reading up on arrays in PHP and I'm utterly baffled. If I am understanding correctly, the numerical indices in PHP don't necessarily correspond to position and are just keys like in a dict in Python. So, when you shuffle a PHP array, the order of the elements will change, but their keys will remain the same. So when calling array[9], you might actually be getting the first element of the array if the shuffle ordered the elements that way. This raises a bunch of questions:

  • Is a PHP array, then, always just some kind of ordered hash table? And what does that mean for overhead? In Python, lists function like a classic array data structure and dictionaries more along the lines of a hash structure. PHP seems to combine the two by assigning unique keys to every value AND keeping track of the order of those values. If I want to use an associative array structure for constant time lookup, am I in a far worse off position than I would be with a Python dictionary because of this ordering overhead? Are there PHP data structures that are ONLY arrays or ONLY hash tables?

  • What happens when you remove a value from a numbered PHP array? If I have an array, [1, 2, 3, 4, 5], I remove 4 from the array, and then try to access array[3], is it going to give me an error, since I
    removed the element with the key 3? Or does PHP do some kind of key
    adjusting in such a case?

  • If you change the ordering of an array (i.e., through a sort or a
    shuffle), is the only way to have the indices correspond to the
    position to copy the array to a new array using array_values().

1
  • 2
    In response to your last point: make sure to read through this list of PHP pitfalls, as well as the entire rest of that article, before you write any code. Commented Feb 27, 2013 at 15:40

1 Answer 1

2
  1. http://php.net/manual/en/class.splfixedarray.php
  2. This code:

    $arr = array(0,1,2,3,4);
    unset($arr[3]);
    echo $arr[3];  // undefined index warning, execution continues;
    echo isset($arr[3]) ? $arr[3] : '';
    print_r($arr);
    

    The print_r() outputs:

    Array
    (
        [0] => 0
        [1] => 1
        [2] => 2
        [4] => 4
     )
    
  3. This depends on the function you choose. Some maintain index association, some do not.

Protip:

  • Never expect two seemingly-similar PHP functions to behave anything like each other. It's the "English" of programming languages: full of crap stolen from other languages and loads of conventions that contradict each other, but everyone speaks it so hop on board the freedom train.

    'murca.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.