How to restructure the index of an array - php

Asked

Viewed 53 times

2

Good afternoon, everyone,

I am in need of the help of vcs. Here’s the thing: I have an array that was created from a CSV file of client contacts. In this file what interests me most are the cell phone numbers and in some do not include the number. Blz. Then I scanned the array in question and removed the ones with the cell field empty. Only that the Index was disorderly, for example:

0=> array
     0 => string ...
     1 => string ...
10 => array
     0 => string ...
     1 => string ...
15 => array
     0 => string ...
     1 => string ...

And I would like to be in the correct order type 1, 2, 3... I don’t know if I made myself clear, but it would be to reorder the index of the array so that it was sequential again.

Thank you

  • the answer helped you? If yes, try to schedule it to help other colleagues.

1 answer

1

Dani, here’s the thing. There’s a native PHP function that does exactly what you need.

It’s called array_values(); it returns all values of array input in a array numerically indexed.

a hypothetical example:

$arr[0] = '10';
$arr[2] = '20';
$arr[5] = '45';

$arrN = array_values($arr);
echo "<pre>";
var_dump($arrN);

Return:

array(3) {
  [0]=>
  string(2) "10"
  [1]=>
  string(2) "20"
  [2]=>
  string(2) "45"
}

I hope I’ve helped

  • Thanks Leandro, I could not test yet, but soon I will have time and test. Once I have news, put here. Thx

  • 1

    great, very good. Give feedback to other colleagues who have the same doubt be helped tbm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.