0

I am having an issue populating my array. I have a csv file which has 1 column containing some ids. I load and process the csv and end up with an array like so

array:5 [▼
  0 => array:2 [▼
    0 => "ID123"
  ]
  1 => array:2 [▼
    0 => "ID234"
  ]
  2 => array:2 [▼
    0 => "ID345"
  ]
  3 => array:2 [▼
    0 => "ID456"
  ]
  4 => array:2 [▼
    0 => "ID567"
  ]
]

Using these ids I hit an API on an id to get data specific to that id. So I loop the array containing the ids and do something like the following

$jobData = array();

foreach($csv as $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.somdomain/some.api/get/".$data[0].");
    //other curl stuff

    $output = curl_exec($ch);
    $output = new \SimpleXMLElement($output);
}

Now if I output the result of output, I get something like this

SimpleXMLElement {#352 ▼
  +"Job": SimpleXMLElement {#383 ▼
    +"ID": "ID123"
    +"Client": SimpleXMLElement {#387 ▼
      +"ID": "12345"
    }
    +"Assigned": SimpleXMLElement {#392 ▼
      +"Staff": array:5 [▼
        0 => SimpleXMLElement {#403 ▼
          +"ID": "12345"
        }
        1 => SimpleXMLElement {#404 ▼
          +"ID": "23456"
        }
        2 => SimpleXMLElement {#405 ▼
          +"ID": "34567"
        }
      ]
    }
  }
}

So I need to build up an array containing the data for all the IDs that I have. So within the foreach loop above I am doing something like this

foreach($csv as $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.somdomain/some.api/get/".$data[0].");
    //other curl stuff

    $output = curl_exec($ch);
    $output = new \SimpleXMLElement($output);

    if (!empty($output)) {
        $jData['jobInfo'] = array(
            'clientId' => (string)$output->Job->Client->ID,
            'startDate' => '20160701',
            'dueDate' => '20160731'
        );

        if(!empty($output->Job->Assigned->Staff)) {
            foreach ($output->Job->Assigned->Staff as $staff) {
                $jData['staffInfo'][] = array(
                    'staffId' => (string)$staff->ID
                );
            }
        }
    }
    $jobData[] = $jData;
}

Now this works to a point. I added an extra [] onyo the staffInfo element because there can be more than 1 staff member as the output above demonstrates. The problem I am having with the above is that it seems to duplicate some things. So the output of jobData array is as follows

array:5 [▼
  0 => array:4 [▼
    "jobInfo" => array:4 [▶]
    "staffInfo" => array:5 [▶]
  ]
  1 => array:4 [▼
    "jobInfo" => array:4 [▶]
    "staffInfo" => array:6 [▶]
  ]
  2 => array:4 [▼
    "jobInfo" => array:4 [▶]
    "staffInfo" => array:7 [▶]
  ]
  3 => array:4 [▼
    "jobInfo" => array:4 [▶]
    "staffInfo" => array:8 [▶]
  ]
  4 => array:4 [▼
    "jobInfo" => array:4 [▶]
    "staffInfo" => array:15 [▶]
  ]
]

As you can see, element 0 has an array of size 5 for staffInfo. Element 1 increases to 6, element 2 is 7, element 3 is 8 and element 4 is 15. If I manually check the system the API is calling, element 0 should have 5 which is correct. Element 1 should have 1, element 2 should have 1, element 3 should have 1 and element 4 should have 7.

So how do I stop this from adding the previous loops result onto the next result?

Thanks

2
  • This line: curl_setopt($ch, CURLOPT_URL, "https://api.somdomain/some.api/get/".$data[0]."); is a syntax error. You've missed the closing quotes. Commented Jul 5, 2016 at 12:24
  • Sorry was removing code to cut down on what was posted Commented Jul 5, 2016 at 12:45

1 Answer 1

1

You don't initialize $jData anywhere. This is a bad practice. As a result, it is always the same. You keep overriding the jobInfo, and keep adding to staffInfo.

You need to initialize the array at the beginning of every iteration.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I have initialised it now but same result
My mistake, was initialising it in the wrong place. Manay thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.