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 931285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:28:28+00:00 2026-05-15T20:28:28+00:00

I’m getting an array of values back from a database where I get an

  • 0

I’m getting an array of values back from a database where I get an array with values like this:

Array ( [6712] => 64.79.197.36:43444 [6712] => 64.79.197.36:43444 [6711] => 64.79.194.56:41113 [6710] => 64.21.47.20:8080 [6709]) 

Where the numbers in brackets, like [6712] are the “id” field in the database.. the issue is that in my script I want to make it so that the each array value has a second key that I assign, which starts from “0” and increments by one, so something like:

Array ( [0][6712] => 64.79.197.36:43444 [1][6712] => 64.79.197.36:43444 [2][6711] => 64.79.194.56:41113 [3][6710] => 64.21.47.20:8080 [4][6709]) 

This is for some data processing so I want to be able to have a way to track each one in a meaningful way within my script (As I can’t predict what the mysql id would be as there may be deleted records, etc so its not always incremented by 1)..

I have found some ways to create a new array by assigning a new key but I’m wondering if its possible to have 2 keys for 1 array value

  • 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-05-15T20:28:28+00:00Added an answer on May 15, 2026 at 8:28 pm

    It’s perfectly possible, using references.

    $result = array ( 6712  => '64.79.197.36:43444',
                      6711  => '64.79.194.56:41113',
                      6710  => '64.21.47.20:8080'
                    );
    $i = 0;
    foreach($result as $key => $value) {
        $result[$i++] = &$result[$key];
    }
    
    var_dump($result);
    echo '<br />';
    $result[0] = '127.0.0.1:80';
    
    var_dump($result);
    echo '<br />';
    

    but it’s likely to prove problematic for you to work with.

    The above code titbit demonstrates setting an initial array, then allocating a second key for each of the three values in the initial array.
    Then, the code modifies a single entry in the array, and demonstrates that the change is reflected in both key values;

    EDIT

    Having the two keys in one array will be cumbersome to work with if you want to use foreach(), curr(), and other control structures elements that manipulate your position in the array, because you’ll pick up each entry twice.

    It might be easier to work with two arrays, the second being a series of pointers to the first.

    $result = array ( 6712  => '64.79.197.36:43444',
                      6711  => '64.79.194.56:41113',
                      6710  => '64.21.47.20:8080'
                    );
    $wrkResult = array();
    foreach($result as $key => $value) {
        $wrkResult[] = &$result[$key];
    }
    
    var_dump($result);
    echo '<br />';
    var_dump($wrkResult);
    echo '<br />';
    $result[0] = '127.0.0.1:80';
    
    var_dump($result);
    echo '<br />';
    var_dump($wrkResult);
    echo '<br />';
    

    Then you can work using either $result or $wrkResult, and any change to the values in one will be refelected in the other; you can loop through one or other and only get the correct number of results, not twice as many, etc.

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

Sidebar

Ask A Question

Stats

  • Questions 485k
  • Answers 485k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From your description it appears that you are making a… May 16, 2026 at 7:41 am
  • Editorial Team
    Editorial Team added an answer This kind of error (when object that receives a message… May 16, 2026 at 7:41 am
  • Editorial Team
    Editorial Team added an answer The answer was to override ViewWillAppear() and move my call… May 16, 2026 at 7:41 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.