Rearrange array

rearrange array python
rearrange array javascript
rearrange array alternately
rearrange array hackerearth
rearrange array php
how to rearrange array in alternating positive and negative number
rearrange array interviewbit solution java
rearrange array elements so as to form two number such that their sum is maximum

I am getting this array when form is submitted

array
  'item' => 
    array
      0 => string 'Salt'
      1 => string 'Pepper'
  'quantity' => 
    array (size=2)
      0 => string '2 spoon'
      1 => string '5'

and now want to rearrange above array, so it should look like

array
  '0' => 
    array
      'item' => string 'Salt'
      'quantity' => string '2 spoon'
  '1' => 
    array
      'item' => string 'Pepper'
      'quantity' => string '5'

I tried so many combinations but failed, will somebody help me how to rearrange this array. Any help will be more than appreciated.

Try this

$array = array(
    'item' =>
    array(
        0 => 'Salt',
        1 => 'Pepper'),
    'quantity' =>
    array(
        0 => '2 spoon',
        1 => '5')
);
$new_array = array();
foreach ($array['item'] as $key => $value) {
    $new_array[$key]["item"] = $value;
    $new_array[$key]["quantity"] = $array['quantity'][$key];
}
echo "<pre>";
var_dump($new_array);

Rearrange an array such that arr[i] = i, Rearrange an array such that arr[i] = i. Given an array of elements of length N, ranging from 0 to N – 1. All elements may not be present in the array  Recommended: Please solve it on PRACTICE first, before moving on to the solution. 1) Store all the numbers present in the array into a HashSet 2) Iterate through the length of the array, if the corresponding position element is present in the HashSet, then set A

Do it as below -

<?php
$arr=array(
  'item' => 
    array(
      0 => 'Salt' ,
      1 => 'Pepper'
       ),
  'quantity' => 
    array (
      0 =>'spoon',
      1 =>'5'
      )
);
$result=array();
$com=array_combine($arr['item'],$arr['quantity']);
foreach($com as $k=>$v)
{
   $result[]=array("item"=>$k,"quantity"=>$v);
}
print_r($result);
?>

OUTPUT

 Array ( 
          [0] => Array ( [item] => Salt [quantity] => spoon )
          [1] => Array ( [item] => Pepper [quantity] => 5 ) 
          ) 

Rearrange an array in maximum minimum form, Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max,  Rearrange Array: Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O(1) extra space. Example: Input : [1, 0] Return : [0, 1] Lets say N = size of the array. Then, following holds true : * All elements in the array are in the range [0, N-1] * N * N does not overflow for a signed integer

try this, I think this would help you,

$a = array(
        'item' =>
        array(
            0 => 'Salt',
            1 => 'Pepper'),
        'quantity' =>
        array(
            0 => '2 spoon',
            1 => '5')
    );

    $i = 0;
    foreach($a['item'] as $row){
        $b[$i]["item"] = $row;
        $b[$i]["quantity"] = $a['quantity'][$i];
        $i++;
    }
    print_r($b);

Output from print_r() will be

Array
(
    [0] => Array
        (
            [item] => Salt
            [quantity] => 2 spoon
        )

    [1] => Array
        (
            [item] => Pepper
            [quantity] => 5
        )

)

Rearrange Array, Rearrange Array: Rearrange a given array so that Arr[i] becomes Arr[Arr[i]] with O​(1) extra space. Example: Input : [1, 0] Return : [0, 1] Lets say N = size of the  Rearrange an array with alternate high and low elements 1. Sort the array in ascending order. 2. Take two index variables i and j to that points to two end-points of the array 3. Create an auxiliary array A[] & initialize an index k with 0

You can simply use array_walk like as

$result = [];
array_walk($arr['item'], function($v, $k)use(&$result, $arr) {
   $result[$k]['item'] = $arr['item'][$k];
   $result[$k]['quantity'] = $arr['quantity'][$k];
});
print_r($result);

Demo

Rearrange array alternately, Given an unsorted array of integers whose each element lies in range 0 to n-1 where n is the size of the array, rearrange array such that A[A[i]] is set to i for every  Rearrange an array in maximum minimum form | Set 1 Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max, fourth second min and so on.

Hoping that your array is stored in the $array variable and that item is leading. Meaning that there is always an item in the array and not always a quantity

<?php
foreach($array['item'] as $key => $item) {
   $newArray[$key]['item'] = $item;
   if (isset($array['quantity'][$key]) {
      $newArray[$key]['quantity'] = $array['quantity'][$key];
   } else {
      $newArray[$key]['quantity'] = 0;
}
?>

Rearrange an array such that 'arr j ' becomes 'i' if 'arr i ' is 'j', Given an array of integers, rearrange the array such that every second element of the array is greater than its left and right elements. Assume no duplicate  As long as the number of elements in each shape are the same, you can reshape them into an array with any number of dimensions. Using the elements from A, create a 2-by-2-by-3 multidimensional array. C = reshape (A,2,2,3) C = C (:,:,1) = 1 3 2 4 C (:,:,2) = 5 7 6 8 C (:,:,3) = 9 11 10 12.

Rearrange array such that A[A[i]] is set to i for every element A[i], e.g., (2-3) if 2 and 3 are the numbers we need to club together… But its given array is not String. Alas !!! So what can we do ??? need to solve it numerically only :P  Rearrange an array such that it contains positive and negative numbers at alternate positions Given an array of integers, rearrange the array such that it contains positive and negative numbers at alternate positions. If array contains more positive or negative elements, they should be moved to end of the array.

Rearrange an array with alternate high and low elements, If there were a numpy function called arrange, it would do the following: newarray = np.arrange(array, [1, 0, 3, 4, 2]) print newarray [20, 10, 40, 50, 30] Formally, if the array to be reordered is m x n, and the "index" array is 1 x n, the ordering would be determined by the array called "index". Does numpy have a function like this?

How to rearrange a given array so that Arr [I] becomes Arr [Arr [I , Method 1. One naive approach is to have another array of same size and navigate the input array and one scan place all the negative numbers to the new array and in second scan place all the positive numbers to new array. Here the Space complexity will be O (n). We have a better solution which can solve this in O (1) space.

Comments
  • Show us what you have tried in the question.
  • what form? please provide more details in your question
  • Instead of using function and foreach simply use foreach
  • yep @Uchiha ,it just came fast to me so this ..i hadn't deleted since just showing an alternative :)
  • Why are you using a counter?
  • @AgeDeO why not? The elements which need to be joined share the same index in different arrays :P
  • You could use the key of the array btw ;)
  • @nowhere It's an extra variable and when the keys do have a meaning in the array (you don't know they don't) the code is going to break
  • Still outputs first two elements if array has more than 2 elements, check eval.in/447453
  • Updated as per your requirement @Adnan