2

I have an array and want to create a new numeric array. This looks like this:

$created_old = explode("_", $result[$i]["created"]);
$created_new = array();
$created_new[0] = $created_old[2];
$created_new[1] = $created_old[0];
$created_new[2] = $created_old[1];
$created_new[3] = "";
$created_new[4] = rtrim(explode(":", $created_old[3])[2], ")");

//Get name from the database

$created_new[3] = $name;

$created = implode("_", $created_new);

This version works just fine, but the previous was missing one line, so the code would be this:

$created_old = explode("_", $result[$i]["created"]);
$created_new = array();
$created_new[0] = $created_old[2];
$created_new[1] = $created_old[0];
$created_new[2] = $created_old[1];
//$created_new[3] = ""; - I am missing
$created_new[4] = rtrim(explode(":", $created_old[3])[2], ")");

//Get name from the database

$created_new[3] = $name;

$created = implode("_", $created_new);

In the second code the string $created is in the wrong order. The index 4 and 3 are switched. If it would be an associative array I would understand this but as it is an numeric array I assume the indices to increase numerically and beeing ordered like this. As I have a working version I do not need help to fix this code but rather understand why the code behaves as it does...

Best regards JRsz

3
  • There is not any numeric array in php. All are associative. Commented Jun 16, 2016 at 8:44
  • It is not relevant for my question, therefore I did not post it: FYI ist is a string I read from the database which has a format like this: 123_123_123_abc:(abc:123) Commented Jun 16, 2016 at 8:45
  • $created_new = array_fill(0, 5, ''); initialization should fix your issue. Commented Jun 16, 2016 at 8:50

1 Answer 1

3

All PHP arrays are associative. There's no such thing as a "numeric array" expect in colloquial speech. A key can be either a string or a number, it doesn't matter. Keys are still ordered by their order of insertion and never implicitly ordered by their value. You would not be surprised by this behaviour I assume:

$arr['a'] = 1;
$arr['c'] = 3;
$arr['b'] = 2;
// ['a' => 1, 'c' => 3, 'b' => 2]

The exact same mechanics are at work in your "numeric array".

If you want to sort your keys, you need to do so explicitly using ksort.

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

6 Comments

True. You can use print_r($created_new) check the array content.
Ok, I guess I have to live with it. Though this is a bit odd I will bear it in mind for future work. Thank you :)
@JRsz Other languages have separate numerically indexed lists and key-value maps; PHP conflates both of them into one type of associative ordered key-value maps.
You don't have to live with it. You can use ksort to explicitly order your keys in the order you're expecting
That is true, but If I create it in a way I need it to be then I do not have this overhead. So in the sense of perfomance one only has to keep this in mind :D
|

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.