28

Ive seen a few examples by using array_values, but cant quite make out how to get it to work...

I have an associative array thats passed via POST, I need to convert it into a indexed array...

My print_r($_POST) gives me this... I need all of this put into an indexed array :)

Array (
    [fieldnames] => 36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202 
    [36771X21X198] => 3434343
    [display36771X21X198] => on
    [36771X21X199] => 5656565
    [display36771X21X199] => on 
    [36771X21X200] => 89898989 
    [display36771X21X200] => on 
    [36771X21X201] => 90909090 
    [display36771X21X201] => on 
    [36771X21X202] => 12121212 
    [display36771X21X202] => on 
    [move] => movesubmit 
    [move2] => ONLINE Submit 
    [thisstep] => 1 
    [sid] => 36771 
    [token] => 1234567890
) 
Phil
  • 157,677
  • 23
  • 242
  • 245
Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38
  • 6
    It would be easier to provide an answer if you showed an example of what you're wanting to achieve – Phil Oct 25 '11 at 11:55
  • 1
    Also, terminology check: all PHP arrays are associative. There are a few names in use for those where the indexes are consecutive integers; "numerically indexed" is a good choice. – Jon Oct 25 '11 at 11:58
  • **what for** you need that. mr. director? – Your Common Sense Oct 25 '11 at 11:59
  • `array_values()` does exactly what you describe. How can you not `make out how to get it to work`? What is the problem you are having with it? – DaveRandom Oct 25 '11 at 12:00
  • 1
    +1 guys. I can't figure out why you would like to turn this into a "numerically indexed" array. A lot of data goes missing right there. Try to explain better – OptimusCrime Oct 25 '11 at 12:07
  • Do you want the indexes become values as well? – Can Vural Oct 25 '11 at 12:11

3 Answers3

83

Observe this amazing way to convert your $_POST into a numerically indexed array:

$numerical = array_values($_POST);


but what if you want to preserve your keys? Perhaps you want something like this?

$numerical = array();
$sep = ':';

foreach($_POST as $k=>$v)
{
  $numerical[] = $k.$sep.$v;
}

$numerical will then have:

Array
(
    [0] => fieldnames:36771X21X198|36771X21X199|36771X21X200|36771X21X201|36771X21X202
    [1] => 36771X21X198:3434343
    [2] => display36771X21X198:on
    [3] => 36771X21X199:5656565
    [4] => display36771X21X199:on
    [5] => 36771X21X200:89898989
    [6] => display36771X21X200:on
    [7] => 36771X21X201:90909090
    [8] => display36771X21X201:on
    [9] => 36771X21X202:12121212
    [10] => display36771X21X202:on
    [11] => move:movesubmit
    [12] => move2:ONLINE Submit
    [13] => thisstep:1
    [14] => sid:36771
    [15] => token:1234567890
)


or, for my final example:

$fieldnames_original = explode('|', $_POST['fieldnames']);
$fieldnames_actual = array();
$values = array();

foreach($_POST as $k=>$v)
{
  if($k!='fieldnames')
  {
    $fieldnames_actual[] = $k;
    $values[] = $v;
  }
}

which will set 3 arrays:

$fieldnames_original:

Array
(
    [0] => 36771X21X198
    [1] => 36771X21X199
    [2] => 36771X21X200
    [3] => 36771X21X201
    [4] => 36771X21X202
)

$fieldnames_actual:

Array
(
    [0] => 36771X21X198
    [1] => display36771X21X198
    [2] => 36771X21X199
    [3] => display36771X21X199
    [4] => 36771X21X200
    [5] => display36771X21X200
    [6] => 36771X21X201
    [7] => display36771X21X201
    [8] => 36771X21X202
    [9] => display36771X21X202
    [10] => move
    [11] => move2
    [12] => thisstep
    [13] => sid
    [14] => token
)

and $values:

Array
(
    [0] => 3434343
    [1] => on
    [2] => 5656565
    [3] => on
    [4] => 89898989
    [5] => on
    [6] => 90909090
    [7] => on
    [8] => 12121212
    [9] => on
    [10] => movesubmit
    [11] => ONLINE Submit
    [12] => 1
    [13] => 36771
    [14] => 1234567890
)
ghbarratt
  • 11,496
  • 4
  • 41
  • 41
1

function

function array_default_key($array) {
   $arrayTemp = array();
    $i = 0;
    foreach ($array as $key => $val) {
         $arrayTemp[$i] = $val;
          $i++;
     }
      return $arrayTemp;
}

Pass the associative array as a parameter and it will convert into the default index of the array. For example: we have Array('2014-04-30'=>43,'2014-04-29'=>41) after the call to the function the array will be Array(0=>43,1=>41).

Illidanek
  • 996
  • 1
  • 18
  • 32
  • 1
    Could you provide a description or brief explanation of how your code solves the problem? – Illidanek Jul 09 '14 at 10:29
  • just pass associative array as parameter and it convert into default index of array. For example: we have Array('2014-04-30'=>43,'2014-04-29'=>41) after call function array will be Array(0=>43,1=>41) –  Jul 10 '14 at 05:05
0

Recursive assoc to indexed converter tested on a small array.

function assoc2indexedMulti($arr) {

    // initialize destination indexed array
    $indArr = array();

    // loop through source
    foreach($arr as $val) {

        // if the element is array call the recursion
        if(is_array($val)) {

            $indArr[] = assoc2indexedMulti($val);

        // else add the value to destination array
        } else {

            $indArr[] = $val;
        }
    }

    return $indArr;
}
Nady Shalaby
  • 604
  • 7
  • 7