0

I have and array and i need to push a value into the first index(0).

array:3 [▼
  4 => "test1"
  5 => "test2"
  6 => "test3"
]

I need the indexes to stay like this, so the array will become like below. Since the indexes are the IDS of the values.

array:3 [▼
  0 => "None selected"
  4 => "test1"
  5 => "test2"
  6 => "test3"
]

To populate array:

$accuGroups = UpselGroup::where('accu_group','1')->with('UpselProducts.products')->pluck('naam','id')->toArray();

What i tried:

$accuGroups = array_merge([0 => 'None selected'], $accuGroups);

outcome (not what i want):

array:4 [▼
  0 => "None selected"
  1 => "test1"
  2 => "test2"
  3 => "test3"
]

Any help is appreciated

thanks

6
  • array_values($array ) to reset the key Commented May 15, 2017 at 12:00
  • I do not wish to reset the key Commented May 15, 2017 at 12:02
  • $accuGroups[0]="None selected"; try this Commented May 15, 2017 at 12:03
  • array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value Commented May 15, 2017 at 12:04
  • Every time you manipulate an array the index will change. What you can do is create your count function to ignore certain indexes like the 0 that you wanted above. Commented May 15, 2017 at 12:06

6 Answers 6

2

array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value

so use like this :

$accuGroups[0]="None selected";
Sign up to request clarification or add additional context in comments.

2 Comments

This suits my needs. Thank you for this. I will approve the answer in 5 minutes.
glad to help you . if my answer is useful mark it with green tick its's useful for future user reference @JordyGroote
0

Try this

$none_selected = array(
        0 => 'None selected');

    $accuGroups = $none_selected + $accuGroups;

Comments

0

you can try this

<?php
    $queue = array("test1", "test2","test3", "test4");
    array_unshift($queue, "None selected");
    echo "<pre>";
    print_r($queue);
    ?>

Comments

0
$accuGroups[0]="Not Selected.";
$accuGroups[6]="Not Selected.";

The array will reset its index value with given Value;

Comments

0

Keep another value which you have to merge as array and add it to first array and you will get result.

$a=['4' => "test1", '5' => "test2",'6' => "test3"];
$b=['0'=>'Not Selected'];
$c=$b+$a;

in $c you will get result as per your requirement.

Comments

0

You can try this

<?php
    $array = array('4' => 'test1','5' => 'test2', '6' => 'test3');
    $add = array('0'=>'None selected');
    $final =  $add + $array;
    echo "<pre>";print_r($final);die;
?>

Comments

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.