How to count... into a matrix

Discussion in 'PHP' started by redhits, Aug 17, 2007.

  1. #1
    Hell i am having a matrix like , well it's not as a matrix matrix[100][100] , but...


    I have something like


    vector['city']['X']=number


    So ... if i have a list of vectors like this


    vector['newyork']['aaa']
    vector['newyork']['aab']
    ...
    vector['chicago']['hui']

    ...
    vector['bucharest']['ddfg']


    How i can parse them all ?!
     
    redhits, Aug 17, 2007 IP
  2. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #2
    I known that if it was about a simple array then i could do

    $arr = array(1, 2, 3, 4);
    foreach ($arr as &$value) {
    $value = $value * 2;
    }
     
    redhits, Aug 17, 2007 IP
  3. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If you want to access each element in the "New York" subarray (e.g get each $vector['NewYork'][X] value) you would do this:

    
    foreach ($vector['NewYork'] as $key => $value)
            echo $key, ' => ', $value, "\n";
    
    PHP:
    If you need to find out the names of all the cities (e.g., $vector['NewYork'], $vector['chicago'], etc.) use

    
    $cities = array_keys($vector);
    echo "Here are all your cities:\n";
    foreach ($cities as $city)
         echo "$city\n";
    
    PHP:
     
    sea otter, Aug 17, 2007 IP