Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:24:33+00:00 2026-06-18T05:24:33+00:00

I was writing one code, when suddenly I’ve found out, that some strange values

  • 0

I was writing one code, when suddenly I’ve found out, that some strange values appear.

    function purify($output){
    $temp = 0;
    $max_index = count($output);
    for($i=0;$i < $max_index; $i++){
        if(strlen($output[$i]) == 3){
                $str = str_split($output[$i]);
                arsort($str);
                $str = implode($str);
                $output[$temp] = $str;
                $temp++;
        }
        else unset($output[$i]);
    }
    return array_unique($output);
}

When I pass an array consisting from these elements:

11 115 165 138 999 885 999 456 135 726 642 425 426

I get this output:

array(11) { [1]=> string(3) "651" [2]=> string(3) "831" [3]=> string(3) "999" [4]=> string(3) "885" [6]=> string(3) "654" [7]=> string(3) "531" [8]=> string(3) "762" [9]=> string(3) "642" [10]=> string(3) "542" [12]=> string(3) "426" [0]=> string(3) "511" }

How did the 12th element ([12]=> string(3) “426”) get there when $temp only went up to 11? I can’t get my head around it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:24:35+00:00Added an answer on June 18, 2026 at 5:24 am

    It’s not a good practice to change the array $output as you are iterating through it.

    Consider creating a new array called $result and change this line:

     $output[$temp] = $str;
    

    to:

     $result[$temp] = $str;
    

    This way you won’t touch $output until the end of the loop.

    It seems to me that you are seeing weird indexes because your changes of $output influence the next iterations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing this code that one function calls itself recursively. But I am
I'm currently re-writing one of my old PHP scripts so that the code is
I found myself keep writing pretty long one-liner code(influenced by shell pipe), like this:
does any one know if, for example, I'm writing in c++ the following code:
I am writing one tool and I need to gather some information about running
Update Turns out the error was in the crypto processor code, that is fixed.
Has anyone found it strange that the default context for uint and ulong is
So I'm writing some code and have recently come across the need to implement
I am writing a script that calls another java program, to perform some tasks.
i am writing one function in hibernate to recursively initialize all properties of object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.