javascript - PHP & JS numeric and assoc arrays index trouble -
this problem trivial 99% of you. far tried avoid situation this, don't have choice.
for php: $array = array(); $array[5] = 'element'; $array['s_5'] = 'alternative element'; $array[7] = 'element2'; $array['s_7 '] = 'alternative element2'; js var array = new array(); array[5] = 'element'; array['s_5'] = 'alternative element'; array[7] = 'element2'; array['s_7 '] = 'alternative element2';
and need secondth element of array. how it? of course create table containing array keys each element, or use foreach/while , action on specific element. can last array element in php using end(), there other, faster way specific element random array (implying don't know keys , length of array)?
thanks helping me.
you might want use 2 dimensional array instead:
$matrix = array(); $matrix['elements'] = array(); $matrix['alt_elements'] = array(); $matrix['elements'][5] = 'element'; $matrix['alt_elements'][5] = 'alternative element';
etcetera...
avoid using associative arrays in js. use arrays when keys numeric. otherwise, use objects.
var matrix = {}; matrix.elements = []; matrix.altelements = []; matrix.elements[5] = 'element'; matrix.altelements[5] = 'alternative element';
Comments
Post a Comment