Solved

Convert associative array to a numeric array?

Posted on 2010-08-17
6
779 Views
Last Modified: 2012-06-27
Is it possible to convert an associative array to a numeric array?

I need to remove the key from an array.
0
Comment
Question by:pillmill
6 Comments
 
LVL 24

Expert Comment

by:mankowitz
Comment Utility
$array = array_values($array);
0
 
LVL 24

Expert Comment

by:mankowitz
Comment Utility
of course, in php, all arrays are associative, but the ones we think of as non-associative use the numbers 0..n as the keys.
0
 
LVL 8

Expert Comment

by:ropenner
Comment Utility
I don't think there is a way to do it without iterating through the array like below.

$ass_array = array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
print_r($ass_array);
print "<BR>\n";
$num_array = array();

$loop = 0;
foreach ($ass_array as $key=>$value) {
    $num_array[$loop++]=$value;
}
print_r($num_array);

which produces the output:

Array ( [5] => 43 [6] => 32 [7] => 56 [b] => 12 )
Array ( [0] => 43 [1] => 32 [2] => 56 [3] => 12 )
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:ropenner
Comment Utility
mankowitz's is shorter and sweeter
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
Not sure I understand the advantage of removing the keys from an array, but to each his own...

At some point, you may find yourself wondering about using numbers as array indexes.  Consider these two constructs:

$a = array('X', 'Y');
$a = array('0'=>'X', '1'=>'Y');

You might think that these arrays are not the same, since you let PHP generate numeric indexes for the first array, and you used a character string for the array indexes of the second array.  In other words, zero is not the same as "zero" - a number is not the same as a string.

For better or worse, PHP is a loosely typed language.  And for array indexes, that means that loose typing prevails.  So if you want to use numbers as if they were character strings in associative array keys, you need to prepend something to the keys or PHP will simply treat the keys as numeric indexes.  When you sort an array by its keys, this kind of thing creates unpleasant surprises.
0
 
LVL 40

Assisted Solution

by:RQuadling
RQuadling earned 250 total points
Comment Utility
Whereas array_values() is the way to go if you want to preserve your existing array, you could use sort(). This effectively sorts the array in place and drop the associative index.

http://docs.php.net/manual/en/function.array-values.php
http://docs.php.net/manual/en/function.sort.php
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now