How to remove the first value within an array?

Asked

Viewed 282 times

0

I have the following array:

array(3) {
  [0]=>
  string(22) "VALOR 1"
  [1]=>
  string(10) "VALOR 2"
  [2]=>
  string(14) "VALOR 3"
}

I need to show all the values of this array but delete the first one, which in this case is "VALUE 1". How to remove the first value from the array? The string can be anything.

I tried to use the array_shift() but it ends up disappearing with my array and leaving only the first string.

  • Do not understand, you want to remove the element or not? o array_shift() removes the first element.

  • @rray, I need to maintain an array but without the value [0]

  • You need to keep the key (0) empty, would that be?

  • @rray, that, key 0 with for example only " ".

  • Is the array always numerical? the zero key is always the first?

  • a 0 is always the first yes, I need to show all strings inside the array, except the first string

  • $arr = array('valor 1', 'valor 2', 'valor 3');
if(!empty($arr[0])) $arr[0] = ''; this server code or has some problem?

  • @Rray, this is it

  • It would not be enough just to set the first array as empty? $arr[0] = null;

Show 4 more comments

1 answer

3


You must use array_shift(), but be careful not to overwrite the array, because it returns the removed element, not the resulting array.

That is to say:

$arraycompleto = [1,2,3];

// Edit: se você precisa manter um valor qualquer na primeira posição 
// e a chave é sempre 0, isso vai resolver:
$arraycompleto[0] = '';
var_dump( $arraycompleto ); // ['',2,3]

// Se a primeira chave não é zero ou você precisa do valor original:
$elementoretirado = array_shift( $arraycompleto );
array_unshift( $arraycompleto, '' );

var_dump( $arraycompleto ); // ['',2,3]
var_dump( $elementoretirado ); // 1;
  • believe I didn’t explain it right but I need the original array with the three keys, but with the first empty or null string.

  • I think now it will solve

Browser other questions tagged

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