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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:50:53+00:00 2026-06-08T11:50:53+00:00

I’m busy with sorting the structure of a menu in my application. Once the

  • 0

I’m busy with sorting the structure of a menu in my application. Once the menu is reorderd by the user, the values (say; Menu item 1, Menu item 2, etc) are still in the same place.

Now I have two arrays, one that holds the way they are sorted (Array 1) and one that holds the values of the menu items. (Array 2)

Example of both arrays;
(Array 1, that holds the keys)

    Array
(
    [0] => 1
    [1] => 2
    [2] => 0
)

The above array’s values are the keys for the new array.

(Array 2, holds the values)

Array
(
    [0] => value_0
    [1] => value_1
    [2] => value_2
)

So I thought it would be best to create a new array which consist out of;

  1. The values of Array 1
  2. The values of Array 2

However, i’m running into a problem. I want the values in array 2 to stick to their keys. So lets say I change the position of value_0 to the last, the new array would look like this;

Array
(
    [1] => value_1
    [2] => value_2
    [0] => value_0
)

Is there a way to achieve this or am I doing it completely wrong?

Edit

Ok, so multidemensional array it is. However i’m having problems creating one.

Array 1 and Array 2 both come from the database. Array 1 with the sorting order and Array 2 contains the values. Now, the values in array 2 are stored like this; value1,value2,value3. So to be able to work with them I explode on , (comma).

The results on the fetchs are both different;

  • For the first array it returns as many as how many values there are.
    (So if there are 3 values, it will return 3 different positions.)
  • For the second array it will return 18 records, since this is tied to
    other menu items (sub menu’s etc).

So for the first array I do;

 while ($row = mysql_fetch_assoc($result_query_test)) {
       $positions[] = $row['position'];
  }

For the second array I do;

while ($row = mysql_fetch_assoc($result_values)) {
      $array_values = explode(',', $row['values']);
}

From then on i’m having problems creating the multidimensinonal array;

while ($row = mysql_fetch_assoc($result_values)) {
     $array_values = explode(',', $row['values']);
     foreach ($positions as $new_key) {
         foreach ($array_values as $value) {
              $new_array[] = array('key' => $new_key, 'value' => $value);
         }
     }
}

Edit two:

This is what I use now;

(Since $all_values is a multidimensional array because I have to explode on the values beforehand.)

    foreach ($all_values as $values) {
        foreach ($values as $key => $value) {
            $new_array[] = array('key' => $positions[$key], 'value' => $value);
        }
    }

This is what the $new_array returns;

Array
(
    [0] => Array
        (
            [key] => 0
            [value] => value_0
        )

    [1] => Array
        (
            [key] => 2
            [value] => value_2
        )

    [2] => Array
        (
            [key] => 1
            [value] => value_1
        )

    [3] => Array
        (
            [key] => 0
            [value] => value_0
        )

    [4] => Array
        (
            [key] => 2
            [value] => value_2
        )

    [5] => Array
        (
            [key] => 1
            [value] => value_1
        )

Now I need to get the values and implode them with comma’s. However, since not every 3 values (value_0, value_1, value_3) are together I can’t do that now.

In this example there are 3 keys, (0,1,2) which should be a different array along with their values, like you did in your example:

Array (
  [0] = Array (
    [key] = 1,
    [value] = value_1
  ),
  [1] = Array (
    [key] = 2,
    [value] = value_2
  ),
  [2] = Array (
    [key] = 0,
    [value] = value_0
  )
)
  • 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-08T11:50:55+00:00Added an answer on June 8, 2026 at 11:50 am

    Why not make a multidimensional array?

    $arrayThree = 
    Array (
      [0] = Array (
        [key] = 1,
        [value] = value_1
      ),
      [1] = Array (
        [key] = 2,
        [value] = value_2
      ),
      [2] = Array (
        [key] = 0,
        [value] = value_0
      )
    )
    

    No matter what order they’re in, the key and value are always the set.

    foreach ($arrayThree as $tempArray)
    {
     echo $tempArray['key'];
     echo $tempArray['value'];
    }
    

    Create Array

    $arrayOne = array();
    $arrayTwo = array();
    $arrayThree = array();
    
    $query = 'SELECT key FROM table1 ';
    $result = mysql_query($query) or die(mysql_error());
    
    while($data = mysql_fetch_assoc($result))
    {
      $arrayOne[] = $data['key'];
    }
    
    $query = 'SELECT value FROM table2 ';
    $result = mysql_query($query) or die(mysql_error());
    
    while($data = mysql_fetch_assoc($result))
    {
      $arrayTwo[] = $data['value'];
    }
    
    foreach($arrayOne as $key => $value)
    {
      $arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
    }
    

    You can always use the mysqli or PDO versions, if you’re using them.

    Example Data

        //THESE MIMIC YOUR SELECT RESULTS
    $test_keys = array(1,2,3);
    $test_values = array('value_1', 'value_2', 'value_3');
    
        //DEFAULTS
    $arrayOne = array();
    $arrayTwo = array();
    $arrayThree = array();
    
        //WHILE FIRST SELECT
    for($i=0;$i<count($test_keys);$i++)
    {
        $arrayOne[] = $test_keys[$i];
    }
    
        //WHILE SECOND SELECT
    for($i=0;$i<count($test_values);$i++)
    {
        $arrayTwo[] = $test_values[$i];
    }
    
        //MAKE THE FINAL ARRAY
    foreach($arrayOne as $key => $value)
    {
      $arrayThree[] = array('key' => $value, 'value' => $arrayTwo[$key]);
    }
    
        //CHECK THE OUTPUT FOR THE NEW ARRAY
    echo '<pre>'.print_r($arrayThree,true).'</pre>';
    

    Example Output

    Array
    (
        [0] => Array
            (
                [key] => 1
                [value] => value_1
            )
    
        [1] => Array
            (
                [key] => 2
                [value] => value_2
            )
    
        [2] => Array
            (
                [key] => 3
                [value] => value_3
            )
    
    )
    

    Imploded List

    $implodeValues = array_map(function($item) { return $item['value']; }, $arrayThree);
    $implodeVariable = implode(',', $implodeValues);
    
    echo $implodeVariable;
    

    Implode Output

    value_1,value_2,value_3

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.