0

I have a string that I converted to a multi-dimensional array.

String: 13,4,3|65,1,1|27,3,2

I wanna be able to move 27,3,2 to index 1 for example, so it would become: 13,4,3|27,3,2|65,1,1

Or remove one of those sections.

I know I can unset(), but I'm not sure how to search for an index then move it or unset it.

3
  • can you tell me why you want to move '27,3,2' to index one? Commented Oct 17, 2016 at 17:31
  • 1
    php.net/manual/en/function.array-splice.php Commented Oct 17, 2016 at 17:42
  • @Albert Akki It's for a specific project, but does it really affect the answer to my question? Commented Oct 17, 2016 at 18:12

2 Answers 2

1

You can try the below one for interchanging the position of last two elements

$array = [0 => array(13,4,3), 1=>array(65,1,1), 2 => array(27,3,2)];
foreach($array as $key => $value) {
    if($key == count($array)-1) {
        $array[$key] = $array[$key-1]; 
        $array[$key-1] = $value;
    }
}

This is for removing the second element.

$array = [0 => array(13,4,3), 1=>array(65,1,1), 2 => array(27,3,2)];
foreach($array as $key => $value) {
  if($key == count($array)-1) {
    $array[$key-1] = $value;
    unset($array[$key]);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Loop through the array using foreach

   foreach($array as $key => $value)

From key you can get the key and can do whatever you like.

Other wise, you can do this if you know the key

   echo $array['pass_key_name_here'];

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.