#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0

    Problem Key Value Array


    Hi,

    Aim: "Iam trying to read out an array"

    PHP Code:
    $ALL = array(   array('Salutation' => 'Mrs.','Name' =>'Smith'','Vorname' => 'Jane','Street' => 'An der Sache','Number' => '81','ZIP' => '801221','City' => 'New York','Text1' => 'LandeshauptstadtBranddirektion','Email' => 'example@example.de','tel' => '12233164'), array('Salutation' => 'Mrs.','Name' =>'Smith'','Vorname' => 'Jane','Street' => 'An der Sache','Number' => '81','ZIP' => '801221','City' => 'New York','Text1' => 'Landeshauptstadt, Branddirektion','Email' => 'example@example.de','tel' => '12233164')); 
    I tried:

    PHP Code:
    foreach ($ALL as $k => $v) {  
       
    //Iam trying to read all fields
      
    echo "\$ALL[$k] => $v.\n"//print "Salutation"    
      
    echo "\$ALL[$k] => $v.\n"//print "Name"

     

    What did I wrong ?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    "doesn't work" doesn't tell us anything. Could you tell me what's wrong with my car based on the statement that it "doesn't work"? I doubt it.

    So we need concrete info:

    • What exactly are you trying to do? I mean a concrete description convering all involved variables. For example, what does $MUNICHALL contain, and what is it for?
    • What's the desired output?
    • What do you get? What exactly does the current output look like?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    Also enclose your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    (there doesn't seem like much point in having that sticky since very few new posters seem to know what BEFORE POSTING means)
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Thanks for reply(I change my first post a little bit),

    What exactly are you trying to do?

    $ALL contains:
    • the data i want to read


    What's the desired output?
    • the data inside $all
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Burb of Detroit, Michigan
    Posts
    91
    Rep Power
    78
    First of all you had a few errors in you array, second of all it's a multidimensional array:
    PHP Code:
    <?php

    $ALL 
    = array(   
                  array(
                             
    'Salutation' => 'Mrs.',
                          
    'Name' =>'Smith',
                          
    'Vorname' => 'Jane',
                          
    'Street' => 'An der Sache',
                          
    'Number' => '81','ZIP' => '801221',
                          
    'City' => 'New York',
                          
    'Text1' => 'Landeshauptstadt, Branddirektion',
                          
    'Email' => 'example@example.de',
                          
    'tel' => '12233164'), 
                array(
                             
    'Salutation' => 'Mrs.',
                          
    'Name' =>'Smith',
                          
    'Vorname' => 'Jane',
                          
    'Street' => 'An der Sache',
                          
    'Number' => '81','ZIP' => '801221',
                          
    'City' => 'New York',
                          
    'Text1' => 'Landeshauptstadt, Branddirektion',
                          
    'Email' => 'example@example.de','tel' => '12233164'
            
    ));  
            
    //echo '<pre>';
    //print_r($ALL);
    //echo '</pre>';

    foreach($ALL as $inner) {
        
    $person = (object) $inner;
        echo 
    $person->Salutation '<br>';
        echo 
    $person->Name '<br>';    
            echo 
    $person->Vorname '<br>';    
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Thanks a lot, but how can i print a specific number of data sets ?

    for example only the first 10 rows of the array ... ?
    PHP Code:
    foreach($ALL 10) {  
       
    $person = (object) $inner;  
       echo 
    $person->Salutation '<br>';
       echo 
    $person->Name '<br>';
       echo 
    $person->Vorname '<br>';    

    ...
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Burb of Detroit, Michigan
    Posts
    91
    Rep Power
    78
    first 10 rows of an multidimensional array is going to be 8 more inner arrays that is assuming that is what your are intending???

    This will only display 1 for you only have two, but if you have 10 it would be $x > 9....
    PHP Code:
    $x 0;
    foreach(
    $ALL as $inner) {
        if (
    $x 0) break;
        
    $person = (object) $inner;
        echo 
    $person->Salutation '<br>';
        echo 
    $person->Name '<br>';
        echo 
    $person->Vorname '<br>';    
        
    $x++;

    P.S. you can also do this with an inner foreach statement (foreach $inner as $key => $value) and outputting those variables, but I'm a lazy programmer.
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    This counter construction seems rather weird to me. What's wrong with a good old for loop?

    PHP Code:
    for ($i 0$i count($ALL) && $i 10$i++) {
        
    $person $ALL[$i];
        echo
            
    $person['Salutation'] . '<br />'
            
    $person['Name'] . '<br />'
            
    $person['Vorname'] . '<br />'
        
    ;

    Of course, "ALL" isn't exactly a sensible variable name, so you should change that to something that actually reflects the content -- maybe "all_persons".

    You should also consider using English names only and writing them as lowercase. This makes the code more readable than a mishmash of English and German, and strict naming conventions usually lower the error rate.
    Last edited by Jacques1; May 19th, 2013 at 04:09 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Thanks it worked (:

IMN logo majestic logo threadwatch logo seochat tools logo