Solved

Convert associative array to a numeric array?

Posted on 2010-08-17
6
801 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 33457731
$array = array_values($array);
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 33457738
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
ID: 33457753
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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 8

Expert Comment

by:ropenner
ID: 33457787
mankowitz's is shorter and sweeter
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 33467297
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:Richard Quadling
Richard Quadling earned 250 total points
ID: 33489677
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

719 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