Need help with code

Discussion in 'PHP' started by deriklogov, Nov 6, 2009.

  1. #1
    Hey,

    I need to create something like that:
    I have a group of numbers like
    1 - 2
    1 - 6
    1 - 34
    1 - 4

    2 - 5
    2 - 7
    2 - 11
    2 - 43

    what I need to do is to match second column to find out first column number
    lets say I have 43 - script have to find that 43 belongs to 2
    , its very easy to do with mysql, but this part have to be done only in php
    probably with array

    any ideas ?
    thanks in advance
     
    deriklogov, Nov 6, 2009 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    If you want to do this with array, show us the array structure? Like this below?

    $array[1] = array(2,6,34,4);
    $array[2] = array(5,7,11,43);
    
    PHP:
     
    Last edited: Nov 6, 2009
    ads2help, Nov 6, 2009 IP
  3. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #3
    I suppose that numbers in second column are unique, so there is no situation when 43 belongs to 1 and 2.
    In that case make an array like that:
    
    $numbers = array(
    2  => 1,
    6  => 1,
    34 => 1,
    4  => 1,
    5 => 2,
    7 => 2,
    11 => 2,
    43 => 2);
    
    $result = array[43];
    
    PHP:
     
    AsHinE, Nov 6, 2009 IP
  4. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #4
    Or if they're not unique, which is most likely;
    $numbers = array(
    1  => 2,
    1  => 6,
    1 => 34,
    1  => 4,
    2 => 5,
    2 => 7,
    2 => 11,
    2 => 43);
    $key = array_search('43', $numbers)
    PHP:
     
    Sky AK47, Nov 7, 2009 IP
  5. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #5
    So the main problem now is to make such an array.
    deriklogov, tell us how are these numbers stored? Plain text, xml, database or what?
     
    AsHinE, Nov 7, 2009 IP
  6. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #6
    @AsHinE, your code should be :

    $numbers = array(
    2  => 1,
    6  => 1,
    34 => 1,
    4  => 1,
    5 => 2,
    7 => 2,
    11 => 2,
    43 => 2);
    
    $result = $numbers[43];
    PHP:
    @Sky AK47, the key is duplicated.
     
    ads2help, Nov 7, 2009 IP
  7. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,080
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Thank you very much guys,
    code working like a charm.


    $numbers = array(
    2 => 1,
    6 => 1,
    34 => 1,
    4 => 1,
    5 => 2,
    7 => 2,
    11 => 2,
    43 => 2);

    $result = $numbers[43];

    Problem solved
    THANK YOU
     
    deriklogov, Nov 7, 2009 IP