0

I have an array store in this variable $month_g

dd($month_g);

I got

array:12 [▼
  0 => array:2 [▼
    "up_bytes" => 277664452
    "down_bytes" => 198868462
  ]
  1 => array:2 [▼
    "up_bytes" => 0
    "down_bytes" => 0
  ]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]
  8 => array:2 [▶]
  9 => array:2 [▶]
  10 => array:2 [▶]
  11 => array:2 [▶]
]

I'm hoping to achieve

array:12 [▼
  0 => array:2 [▼
    "up_bytes" => 0
    "down_bytes" => 0
  ]
  1 => array:2 [▼
    "up_bytes" => 277664452
    "down_bytes" => 198868462
  ]
  2 => array:2 [▶]
  3 => array:2 [▶]
  4 => array:2 [▶]
  5 => array:2 [▶]
  6 => array:2 [▶]
  7 => array:2 [▶]
  8 => array:2 [▶]
  9 => array:2 [▶]
  10 => array:2 [▶]
  11 => array:2 [▶]
]

I want to shift my 0 element to the second one. How do I do that in PHP ?

1

2 Answers 2

4
$temp = $a[0];
$a[0] = $a[1];
$a[1] = $temp;
Sign up to request clarification or add additional context in comments.

Comments

1

You can use one line solution like this:

$arr = array_combine(array(1, 0, 2), $arr);

Probably too fancy for that simple solution, but can be effective in larger swaps.

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.