Change an associative array into an indexed array / get an Zend_Table_Row_Abstract as non-associative

PhpZend FrameworkAssociative ArrayAssociative

Php Problem Overview


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

Php Solutions


Solution 1 - Php

Is pure php ok?

$array = array_values($array);

Source

Solution 2 - Php

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).

Solution 3 - Php

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  
        )

Solution 4 - Php

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEthanView Question on Stackoverflow
Solution 1 - PhpIan ElliottView Answer on Stackoverflow
Solution 2 - Phpuser3567805View Answer on Stackoverflow
Solution 3 - PhpKaloyView Answer on Stackoverflow
Solution 4 - PhpHareesh SivasubramanianView Answer on Stackoverflow