Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. For Loop does not work

For Loop does not work

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
csharpphpdata-structuresjsonquestion
4 Posts 3 Posters 0 Views 1 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.
  • D Offline
    D Offline
    Dwayner79
    wrote on last edited by
    #1

    First time developing in PHP. Used to coding in c#, so feel free to make fun of me. Here is the code I am using:

    <?php
    $response = file_get_contents($url);
    $jsonResults = json_decode($response, true);

    echo "Title: ".$jsonResults['items']['0']['volumeInfo']['title']; //this works
    echo "Title: ".$jsonResults['items']['1']['volumeInfo']['title']; //this works

    //this does not
    for ($i = 0; $i < $jsonResults['items'].length; $i++) {
    echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
    }
    ?>

    Now the json results look like this:

    array(3) {
    ["items"]=>
    array(10) {
    [0]=>
    array(7) {
    ["volumeInfo"]=>
    array(12) {
    ["title"]=>
    string(64) "MyTitle"
    }

    ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

    G 1 Reply Last reply
    0
    • D Dwayner79

      First time developing in PHP. Used to coding in c#, so feel free to make fun of me. Here is the code I am using:

      <?php
      $response = file_get_contents($url);
      $jsonResults = json_decode($response, true);

      echo "Title: ".$jsonResults['items']['0']['volumeInfo']['title']; //this works
      echo "Title: ".$jsonResults['items']['1']['volumeInfo']['title']; //this works

      //this does not
      for ($i = 0; $i < $jsonResults['items'].length; $i++) {
      echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
      }
      ?>

      Now the json results look like this:

      array(3) {
      ["items"]=>
      array(10) {
      [0]=>
      array(7) {
      ["volumeInfo"]=>
      array(12) {
      ["title"]=>
      string(64) "MyTitle"
      }

      ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      PHP doesn't use the . operator for class members. If $jsonResults['items'] was an instance of a class, then the way to access the length member would be $jsonResults['items']->length. Native arrays are not classes though, so to find their length you need to use the count function:

      <?php
      $length = count($jsonResults['items']);
      for($i = 0; $i < $length; $i++) {
      echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
      }

      or you could use foreach instead of for:

      <?php
      foreach($jsonResults['items'] as $item) {
      echo "Link: ".$item['volumeInfo']['title'];
      }

      F 1 Reply Last reply
      0
      • G Graham Breach

        PHP doesn't use the . operator for class members. If $jsonResults['items'] was an instance of a class, then the way to access the length member would be $jsonResults['items']->length. Native arrays are not classes though, so to find their length you need to use the count function:

        <?php
        $length = count($jsonResults['items']);
        for($i = 0; $i < $length; $i++) {
        echo "Link: ".$jsonResults['items'][$i]['volumeInfo']['title'];
        }

        or you could use foreach instead of for:

        <?php
        foreach($jsonResults['items'] as $item) {
        echo "Link: ".$item['volumeInfo']['title'];
        }

        F Offline
        F Offline
        Firo Atrum Ventus
        wrote on last edited by
        #3

        I always sizeof() thou

        A hidden needle is way more effective than an unsheathed sword. That is, in the hand of professionals. What about you? Just pray your enemies are blind

        D 1 Reply Last reply
        0
        • F Firo Atrum Ventus

          I always sizeof() thou

          A hidden needle is way more effective than an unsheathed sword. That is, in the hand of professionals. What about you? Just pray your enemies are blind

          D Offline
          D Offline
          Dwayner79
          wrote on last edited by
          #4

          Thank you both!

          ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups