How to insert an item into a particular php array position?

Asked

Viewed 944 times

3

I have the following array,

$status = array();
$status["Visualizar"] = "Visualizar";
$status["Editar"] = "Editar";
$status["Status"] = "Alterar Status";
$status["Boleto"] = "Gerar Boleto";

It turns out that depending on the current status going from 1 to 10 I must or must not show the Item $status["Status"], well with the if resolves, but it turns out that when I give an unset and then reinsert the item it goes to the end of the list, but I need it to be in the original position before billet

Example:

if ($codstatus == 3){
  $status["Status"] = "Alterar Status";
}else{
  unset($status["Status"]);
}

Has an elegant way of doing this or I’ll have to redo the entire array every time?

  • 2

    I don’t understand why you need the position if the key to your matrix is a text

  • It is a text because in a foreach I list this array in a combo for the user to choose, so should keep the position

  • I don’t consider this a legal code but it might help you: https://repl.it/Gudr/2 I suggest you use only integers to index your array, so you can set the position where you want to insert your text.

  • Sometimes we need to use by name in the keys to facilitate reading in other parts of the code, there is a very large system interaction with javascript so I need the keys with names and not numbers, which would be impossible to remember what each number represents. I’m studying the array_splice tip

  • 1

    There is no mystery, just check in javascript if the key exists

2 answers

2

Do it this way:

$status = [
    'Visualizar' => 'Visualizar',
    'Editar' => 'Editar',
    'Boleto' => 'Gerar Boleto'
];

// Define a chave => valor que será inserido no array
$pair = ['Status' => 'Alterar Status'];

// Procura no array pela chave e retorna o índice dela
$afterIndex = array_search('Boleto', array_keys($data));

// Cria um novo array, repartindo o antigo em duas partes e adicionando o novo par de chave => valor entre elas
$newStatus = array_merge(array_slice($status, 0, $afterIndex-1), $pair, array_slice($status, $afterIndex-1));

print_r($newStatus);

Exit:

Array
(
    [Visualizar] => Visualizar
    [Editar] => Editar
    [Status] => Alterar Status
    [Boleto] => Gerar Boleto
)

2

Why?

First, why you need it in a certain order since your array is associative?

Now, if you need it to be in the correct order, you can use the function array_splice, see example:

<?php

$array = ['a', 'b', 'd', 'e'];
array_splice($array, 2, 0, 'c');

var_dump($array); //['a','b','c','d','e']

See online example: https://repl.it/Gudr/0

  • The reason is that there is an interaction with javascript where the Keys name is used for some decisions, using numeric index is out of the question, I’m trying to use the array_splice but I don’t know if I can insert an array with name to literate in a certain position, see { $status = array(); $status["View"] = "View"; $status["Edit"] = "Edit"; //$status["Status"] = "Change Status"; $status["Boleto"] = "Generate Boleto"; array_splice($status, 1, 0, array("Status" => "Change Status"); var_dump($status); ;}

Browser other questions tagged

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