Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PHP foreach equivalent
Forum Updated to NodeBB v4.3 + New Features

PHP foreach equivalent

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.4k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VolebabV Offline
    VolebabV Offline
    Volebab
    wrote on last edited by
    #1

    In PHP we have foreach with a '=>' keyword that translates the key and value into a variable, for example:

    $arr = [
    	'foo' => [
    		'bar',
    		'biz'
    	]
    ];
    
    foreach($arr as $key => $value)
    {
    	// key = foo
    	// value = array with bar and biz.
    }
    

    How can I translate this to Qt?

    kshegunovK 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You don't translate it to Qt. Qt is C++.

      If you are using C++11 you can use the range for loop

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      VolebabV 1 Reply Last reply
      0
      • VolebabV Volebab

        In PHP we have foreach with a '=>' keyword that translates the key and value into a variable, for example:

        $arr = [
        	'foo' => [
        		'bar',
        		'biz'
        	]
        ];
        
        foreach($arr as $key => $value)
        {
        	// key = foo
        	// value = array with bar and biz.
        }
        

        How can I translate this to Qt?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Volebab
        Or if you're not using C++11, you can use the foreach macro that Qt defines (if not disabled explicitly):

        QList<QString> listOfStrings;
        listOfStrings << "String 1" << "String 2" << "String 3";
        
        foreach (QString string, listOfStrings)  {
             // ...
        }
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          You don't translate it to Qt. Qt is C++.

          If you are using C++11 you can use the range for loop

          VolebabV Offline
          VolebabV Offline
          Volebab
          wrote on last edited by
          #4

          @SGaist I know that, the question wasn't that, the question as related to translate the key and value using foreach as PHP does. - Your answer and the @kshegunov answer is foreach with values only.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What container are you using ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            VolebabV 1 Reply Last reply
            0
            • SGaistS SGaist

              What container are you using ?

              VolebabV Offline
              VolebabV Offline
              Volebab
              wrote on last edited by Volebab
              #6

              I'm using QHash.

              I tried using like this:

                  for (const auto& kv : iter) {
                      qDebug() << kv;
                  }
              

              But I can't get the key.

              kshegunovK 1 Reply Last reply
              0
              • VolebabV Volebab

                I'm using QHash.

                I tried using like this:

                    for (const auto& kv : iter) {
                        qDebug() << kv;
                    }
                

                But I can't get the key.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @Volebab
                Just use the regular for loop:

                typedef QHash<QString, QString> MyHash;
                
                MyHash container;
                for (MyHash::Iterator i = container.begin(), end = container.end(); i != end; i++)  {
                    QString key = i.key(), value = i.value();
                }
                

                Kind regards.

                Read and abide by the Qt Code of Conduct

                VolebabV 1 Reply Last reply
                1
                • kshegunovK kshegunov

                  @Volebab
                  Just use the regular for loop:

                  typedef QHash<QString, QString> MyHash;
                  
                  MyHash container;
                  for (MyHash::Iterator i = container.begin(), end = container.end(); i != end; i++)  {
                      QString key = i.key(), value = i.value();
                  }
                  

                  Kind regards.

                  VolebabV Offline
                  VolebabV Offline
                  Volebab
                  wrote on last edited by
                  #8

                  @kshegunov Amazing, thank you.

                  M 1 Reply Last reply
                  0
                  • VolebabV Volebab

                    @kshegunov Amazing, thank you.

                    M Offline
                    M Offline
                    MajidKamali
                    wrote on last edited by
                    #9

                    @Volebab

                    QHash<QString, QString> hash;
                    foreach (auto key, hash.keys()) {
                        qDebug() << hash[key];
                    }
                    
                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved