How can I shift the elements of an array to the right
sorting - Is it possible to use array_shift() in PHP and get the key? - Stack Overflow
java - Shift elements of an array - Stack Overflow
shift array elements to the right?
What is the use of array_shift in PHP?
$fruits = ['apple', 'banana', 'cherry'];
$first = array_shift($fruits);
print_r($fruits); // ['banana', 'cherry']
echo $first; // apple
It removes the first element from the array and returns it.How does array_shift work with associative arrays?
$user = ['name' => 'John', 'age' => 25];
$first = array_shift($user);
print_r($user); // ['age' => 25]
echo $first; // John
It removes the first key-value pair based on internal order.What is the difference between array_shift and array_pop?
$arr = [1, 2, 3];
array_shift($arr); // removes 1 from start
array_pop($arr); // removes last element
array_shift removes from the beginning, array_pop removes from the end.Videos
Im trying to add a string into an index of an existing array with inputted values and shift the old values to the right 1 place. for example an array I have created has 5 spaces in total and hold string values is [A, B, C] when I add Z to index 0 the output is [Z, A, A, A] instead of [Z, A, B, C]. I am confused on why it copies the A from index 0 to the other indices of the filled array. Here is the code for my function. curIndex is a integer instance variable that holds the current index of the array.
public void add (int index, String theData)
{
for(int i = 0; i < curIndex; i++)
{
strarray[i + 1] = strarray[i];
}
strarray[index] = theData;
}$result = array_splice( $yourArray, 0, 1 );
... should do the trick. See array_splice.
You could use each like:
$b = array(1=>2, 3=>4, 7=>3);
while(1) {
list($key,$value) = each($b);
if (empty($key))
break;
echo "$key $val\n";
}
Iterating the array with each will keep its last position.
"How many times does this number need to shift" in this case is actually just "how many odd numbers in total are before this number", because you are trying to move all the even numbers to the beginning without changing the relative positions of the even numbers. Each even number needs to "move over" however many odd numbers there are before it.
So you just need to keep a cumulative count:
int[] a = { 1, 3, 2, 5, 4, 7, 8, 6 };
List<Integer> shifts = new ArrayList<>();
int oddNumberCount = 0;
for (int i : a) {
if (i % 2 == 0) {
shifts.add(oddNumberCount);
} else {
oddNumberCount++;
}
}
I don't know how many even numbers there will be, so I used a list. If you require an array as the output you can convert it to a list simply like this (See Also):
int[] result = shifts.stream().mapToInt(x -> x).toArray();
If you do know how many even numbers there will be, you can of course just start with an array of that given size.
What are you lacking in your code is you don't track what's the current position of the even numbers correctly:
public static void main(String...arg){
int[] a={1,3,2,5, 4, 7, 8, 6,0}; //the array to be shifted
int indexEvenNumber = 0;//where should we put the even number if we found it.
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
int temp = a[i];
a[i] = a[indexEvenNumber];
a[indexEvenNumber] = temp;
indexEvenNumber++;
}
}
// print the shifted array to confirm results
Arrays.stream(a).forEach(System.out::println);
}
can anyone explain what to do?
for(int i =current_index; i <used; i++)
{
int temp = data[i+1];
data[i+1] = data[i];
data[i+2] = temp;
}this doesn't seem to be doing it