SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Array Help

  1. #1
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Help

    Hello Guys,

    I'm having a hard time to make the opposite of this code

    Code:
    $array = array("n1" => 'blue', "n2" => 'yellow', "n3" => 'green', "n4" => 'red');
    $key1 = array_search('green', $array); // $key = n3;
    echo $key1;
    What I wanted to the above code is to search the key then if found,
    print the value.

    Any help will be much appreciated.

    Best regards
    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $tmp = array("n1" => 'blue'"n2" => 'yellow'"n3" => 'green'"n4" => 'red');
    foreach( 
    $tmp as $K => $V ) {
        if ( 
    $V "green" ) {
            echo 
    "Key is " $k ;
        }

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Perth, Australia
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure if i'm following you 100% on this but is this what you're trying to achieve?

    PHP Code:
    $key 'n3';
    $array = array("n1" => 'blue'"n2" => 'yellow'"n3" => 'green'"n4" => 'red');
    if (
    array_key_exists($key$array)) {
          echo 
    $array[$key];


  4. #4
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried that solution but I've got a huge array values/keys to search and display.So my primary concern is the performance.
    Using loop is a pain in my *** but thanks for the help though.

    Quote Originally Posted by byron3@earthlink View Post
    PHP Code:
    $tmp = array("n1" => 'blue'"n2" => 'yellow'"n3" => 'green'"n4" => 'red');
    foreach( 
    $tmp as $K => $V ) {
        if ( 
    $V "green" ) {
            echo 
    "Key is " $k ;
        }

    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  5. #5
    SitePoint Zealot darksystem's Avatar
    Join Date
    Jan 2005
    Location
    Fayettville, North Carolina
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you're following in the right track. This is what I am looking for.
    I really appreciate your help and your time.

    THanks a lot buddy.

    Cheers


    Quote Originally Posted by rodp View Post
    not sure if i'm following you 100% on this but is this what you're trying to achieve?

    PHP Code:
    $key 'n3';
    $array = array("n1" => 'blue'"n2" => 'yellow'"n3" => 'green'"n4" => 'red');
    if (
    array_key_exists($key$array)) {
          echo 
    $array[$key];

    Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
    Lucki Multimedia - Email

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You will find this to be of better performance.

    Code php:
    $key = 'n3';
    $array = array('n1' => 'blue', 'n2' => 'yellow', 'n3' => 'green', 'n4' => 'red');
     
    if (isset($array[$key])) {
        echo $array[$key];
    }

    If you would like to test it:

    Code php:
    <?php
     
    header('Content-type: text/plain');
     
    $key = 'n3';
    $array = array('n1' => 'blue', 'n2' => 'yellow', 'n3' => 'green', 'n4' => 'red');
    $data = '';
     
    //------------------------------------------------------------------------------
     
    $t1Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            if (isset($array[$key])) {
                  $data = $array[$key];
            }
        }
     
    $t1Stop = time() + microtime();
     
    //------------------------------------------------------------------------------
     
    $t2Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            if (array_key_exists($key, $array)) {
                  $data = $array[$key];
            }
        }
     
    $t2Stop = time() + microtime();
     
    //------------------------------------------------------------------------------
     
    print 'Test 1: ' . round($t1Stop - $t1Start, 10) . "\n";
    print 'Test 2: ' . round($t2Stop - $t2Start, 10) . "\n";
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Since SitePoint cut short my post...


    If you would like to test it:

    Code php:
    <?php
     
    header('Content-type: text/plain');
     
    $key = 'n3';
    $array = array('n1' => 'blue', 'n2' => 'yellow', 'n3' => 'green', 'n4' => 'red');
    $data = '';
     
    //------------------------------------------------------------------------------
     
    $t1Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            if (isset($array[$key])) {
                  $data = $array[$key];
            }
        }
     
    $t1Stop = time() + microtime();
     
    //------------------------------------------------------------------------------
     
    $t2Start = time() + microtime();
     
        for ($i = 0; $i < 1000; $i++) {
            if (array_key_exists($key, $array)) {
                  $data = $array[$key];
            }
        }
     
    $t2Stop = time() + microtime();
     
    //------------------------------------------------------------------------------
     
    print 'Test 1: ' . round($t1Stop - $t1Start, 10) . "\n";
    print 'Test 2: ' . round($t2Stop - $t2Start, 10) . "\n";
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

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