A
A
AstonMartin2016-03-20 07:38:05
PHP
AstonMartin, 2016-03-20 07:38:05

Why different indexes in PHP array?

Good afternoon!
I’m slowing down something out of the blue)
Parse on php (5.5.14) a text file with a chat log, from the data I make an array of the form:

$users[$user_number]['start'] = $message_timestamp;
$users[$user_number]['status'] = "onchat";

Below I also do this:
$users[$user_number]['messagescount']="аааа";
Where $user_number is obtained from the phone number like this:
function number_to_norm($number) {
    $number_new = str_replace(array(" ", "-", "+", "‑", " "), "", $number);
    $number = trim($number_new);
    $number = strval($number);

    return $number;
}

The problem is that for the first and second piece of code (adding array elements) php creates different array elements! Although the key is the same. Key value type too.
First time I see this. I read php.net/manual/ru/language.types.array.php but still didn't understand where the error is.
Tell me plz?
UPD.
To be clear:
Here is the full code https://dl.dropboxusercontent.com/u/757649/phperro...
This is the original test log file https://dl.dropboxusercontent.com/u/757649/phperro...
A this is a screenshot of how the array is obtained (from the NetBeans debugger) joxi.ru/p27EaXPt8qzjr7

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2016-03-20
@AstonMartin

The problem is that for the first and second piece of code (adding array elements) php creates different array elements!

The problem is that you don't show all the code, or at least that part of it, so that we can see different elements. And it is not customary to take a word here.
So far, here's what I've noticed:
function number_to_norm($number) {
    $number_new = str_replace(array(" ", "-", "+", "‑", " "), "", $number);
    $number = trim($number_new); // Это лишнее, вы уже дважды удалили пробелы выше
    $number = strval($number);   // $number и так уже строка
    return $number;
}

At a minimum, it can be rewritten like this:
function number_to_norm($number) {
    return str_replace(array(" ", "-", "+", "‑"), "", $number);
}

If you need to get only digits from a string, then it is safer to remove all non-digit characters:
function number_to_norm ($number) {
    return preg_replace('/\D/', '', $number);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question