Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts

    is_assoc_array()?

    trying to figure out at runtime whether a given array is associative or not .. e.g.

    PHP Code:
    <?
    $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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,132
    Thanks
    2
    Thanked 328 Times in 320 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,132
    Thanks
    2
    Thanked 328 Times in 320 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    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 Code:
    <?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

    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

    PHP Code:
    <?
        
    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;
        }
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •