62

Hi out there in Stackland. I was wondering if there was either a function or an easy way to change an associative array into an indexed array.

To elaborate, I'm using the Zend framework, and I've got a point in my site where I take out a row of an SQL table as an associative array. I've passed it to javascript via an echoed in JSON. However, I've noticed that I can see the names of my database's columns in Firebug. Having outsiders know the names of your tables and columns is a big security no-no, so I'd like to change it from

SQLarray[user_id]
SQLarray[block_id]
SQLarray[b_price] etc.

to

SQLarray[0]
SQLarray[1]
SQLarray[2] etc.

Is there a good way to do this?

It would also work to be able to have a Zend_Table_Abstract->fetchAll() return a non-associative array, but I don't think that's possible. Thanks for your help!

jason
  • 8,918
  • 2
  • 37
  • 43
Ethan
  • 5,660
  • 9
  • 44
  • 51

4 Answers4

189

Is pure php ok?

$array = array_values($array);

Source

laurent
  • 88,262
  • 77
  • 290
  • 428
Ian Elliott
  • 7,588
  • 5
  • 35
  • 42
  • If the initial array was generated from odbc_fetch_array, can I count on array_values to always honor the proper column-order of the resultset? It seems like I can. – Lonnie Best Jan 05 '17 at 20:24
6

define 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:28
  • 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:09
  • This logic is helpful. – Vrushal Raut Dec 17 '18 at 08:01
1

for multi layered array i use this:


function getIndexedArray($array) {
        $arrayTemp = array();
        for ($i=0; $i < count($array); $i++) { 
            $keys = array_keys($array[$i]);
            $innerArrayTemp = array();
            for ($j=0; $j < count($keys); $j++) { 

                $innerArrayTemp[$j] = $array[$i][$keys[$j]];                
            }
            array_push($arrayTemp, $innerArrayTemp);
        }
        return $arrayTemp;
    }

it turns this:

(
    [0] => Array
        (
          [OEM] => SG
            [MODEL] => Watch Active2
            [TASK_ID] => 8
            [DEPT_ASSIGNED] => Purchashing  
        )
)

into this :

[0] => Array
        (
          [0] => SG
            [1] => Watch Active2
            [2] => 8
            [3] => Purchashing  
        )
Kaloy
  • 91
  • 1
  • 12
0

You could use this simple piece of code, if you do not want to use the inbuilt PHP function.

$input_array;           // This is your input array
$output_array = [];     // This is where your output will be stored.
foreach ($input_array as $k => $v){
    array_push($output_array, $v);
}
print_r($output_array);
Hareesh Sivasubramanian
  • 1,265
  • 7
  • 17
  • 27