...

View Full Version : is_assoc_array()?



firepages
06-09-2007, 06:25 AM
trying to figure out at runtime whether a given array is associative or not .. e.g.



<?
$a=array('0','1','2');
$b=array(0=>'0',1=>'1',2=>'2');
?>

ok in that example its not really relevant since each can be accessed via $array[$index] and give the same (correct) answer.. regardless I want to know what type $a and $ are

I am sure there is a simple answer starting me in the face .. but I must be short-sighted at the moment.

CFMaBiSmAd
06-09-2007, 07:06 AM
There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices. I think the "no" is a typo and they meant "not".


A key may be either an integer or a string.Your $a array IS exactly equivalent to your $b array. They have numeric indexes 0, 1, and 2 with corresponding values '0', '1', and '2'.

Arrays are made up of key/value pairs only. That the keys can be numeric, string, or a mix within one array does not matter.

Since there is only one array type, your question - "I want to know what type $a and $b are" has the answer, they are both PHP arrays.

whizard
06-09-2007, 07:09 AM
Check out the fourth user comment from the top on this page:

http://us.php.net/is_array

(the 'user submitted note' from "adam at greatbigmassive dot net")

HTH
Dan

CFMaBiSmAd
06-09-2007, 08:17 AM
This reminds me of the database questions - "I have deleted a row and I want to reuse the empty slot or I want to move all the rows up so that a new row is always added at the end..."

Arrays are the same, it does not matter if there are gaps in the keys, if they are out of order, if they are strings/numeric/or a mix. If you reference a key, you get the corresponding value.

Don't place unnecessary restrictions on code that uses arrays. Keep it general purpose, the same way you would with a database.

If you want to find if a key exists, use - array_key_exists()
If you want a list of all the keys, use - array_keys()
If you want to iterate through all the key/value pairs, use - foreach()
If you want to reorder the elements, use one of the sort functions.

firepages
06-09-2007, 09:35 AM
Arrays are the same, it does not matter if there are gaps in the keys, if they are out of order, if they are strings/numeric/or a mix. If you reference a key, you get the corresponding value.


<?php
$k=array('val'=>'blah','val2'=>'blah');
echo $k[1];
?>

echo's nothing (as expected)

Normally this is easy to spot but in my case the $array is generated dynamically and depending on the situation may or may not have string keys, or indeed start at 0 for numeric keys, an array array(3=>'blah') may exist as well, I don't really know this until its too late (at least that was the case when I asked the question :))


Don't place unnecessary restrictions on code that uses arrays. Keep it general purpose

The class I am working on generates other classes and keeping it generic is indeed the whole point of the class, trying to ignore the construction of the array was my original goal.

All that said I have switched approach and now only generate non-indexed arrays for the class to consume (I appreciate internally they are indexed and still associative)


Check out the fourth user comment from the top on this page:
Thats actually exactly what I was looking for before I changed track :thumbsup:

Its hard to explain, this is the section of code that used to be the problem, however the introduction of the $arr['display'] has solved the original issue, both here, elsewhere in the class and in the separation of data from markup



<?
function build_its($arr, &$parser, $req=''){
$its=array();
array_shift($arr[1]);
$arr[4] = !empty($req) ? $req : $arr[4];
foreach($arr[1] as $k=>$v){
$sel = ($arr[4]==$v) ? '{SEL}' : '' ;
$its[]=array('K'=>$v, 'V'=>empty($arr['display']) ? $v : $arr['display'][$k], 'S'=>$sel);
}
$parser->set_iterator_data('IT_'.strtoupper($arr[0]), $its);
return;
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum