Creating Arrays

Hello.

I am trying to create arrays of categories for an APP I am developing. Basically, I have an array that holds the identifiers for specific categories. For EG: M40, M60, M70 etc

What I am trying to accomplish is, if the HTML table name starts with a category Identifier, I would like to add that table to an array for that category. For EG: All the tables that start with m40 should go into an array that only holds table names that start with m40, if the table name starts with m60, that table should go into an array that holds tables for the m60 category.

This is the code that I have at the moment, but it is returning an empty array.

for($i=0; $i<count($tables); $i++) {
    // If the table name starts with a category identifier in category list, add it to an array for that category
    if(strpos($tables[$i], $category_ids[$i]) === 0) {
        $last[] = array($tables[$i]);
    }
}

So basically I am checking if the tablename starts with m40 or m60 etc, put it into an array that holds the table names that start with that category. So I would have 3 arrays. m40, m60, m70 that each contain the table names that start with the identifier.

The app I am trying to create is the most difficult one I have attempted thus far, so I am struggling quite a lot to make everything work. From my knowledge, the code above SHOULD work, but obviously something is going wrong, and I am not sure what.

If anyone could give any insight to where I am going wrong, or how I should correct it, I would appreciate it!

Sample input for this?

Hi!

I am getting the data from an API call. So for EG the API call:

public function callAPI($method) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://li.........k/api/data?eventid=4..1&type=json&apiuser=s..c&apikey=bb........9f&method=" . $method . "");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_HTTPGET, true);

            $response = curl_exec($ch);
            if($response == FALSE) {
                die("cURL error " . curl_error($ch));
            }
            $responseObj = json_decode($response, true);

            return $responseObj;
    } 

The $responeObj contains the categories I need: EG: m40, m60

 $category_ids = array();
 foreach($categories['Records']['Record'] as $record) {
     array_push($category_ids, $record['ID']);
 }

Here I am putting only the category identifiers (m40, m60) into an array.

It is from there, that I need to find out which HTML tables start with the identifier, and place them into the appropriate category.

I hope this is enough of an example of input and what I am trying to do.

Okay, so you are getting a JSON object back from the call. Are the tables coming back from the API as well? I am trying to ascertain where html comes into this; what value the $last array should be holding; and what is held by the $tables array.

Hello, sorry for the late reply.

No I am creating the HTML tables manually.

What I would like is for the $last array to hold the array for each of the categories, So, the $last array would hold the array for the m40 category, the array for the m60 category etc.

So basically, the API call WILL return an array containing the values m40, m60, m70 etc. The HTML tables are created by a user by entering the table name and column the table will contain and the table names will start with either m40, m60, m70 etc so the tables will be named m40_maletopten, m60_maletopten etc.

If the array returned from the API call contains the values m40 and m60, I need to create arrays that will hold the table names that start with that same value. So if the array returned from the API call contains : (‘m40’, ‘m60’), I check which table names start with m40 or m60 and and put those table names into an appropriate array, so in the end I would be left with X amount of arrays holding table names EG: m40array = (‘m40_maletopten’, ‘m40_womentopten’, ‘m40veterantopten’) and m60array = (‘m60_maletopten’, ‘m60_womentopten’, m60_veterantopten’)

I apologize of this does not make sense, I wish I could make it easier to understand.

So you want to take a fixed length prefix from the string and use that as a grouping index?

In your loop you can get that prefix from the string with substr() and build a new array like $groupedRecords[$prefix][] = $record

This seems to be exactly what I am looking for!

I am just finishing up another part of the APP, and then I will come back to the API part. As soon as I work on it again, I will post here and give an update.

Thank you so much for the help and I will try to give an update by tomorrow evening!

Thank you!

Hi again!

Okay, I have tried this in my code but I seem to be going wrong somewhere along the line.

An example of what I am doing:

An API call for Categories will return the categories for the event. The data I need from each category would the categories ID EG: M40 and the Distance for that Category EG: 3 (the ID for the distance)

Here is the code:

foreach($categories['Records']['Record'] as $category) {
   $groupedRecords[$category['ID']]['catID'] = $category['ID'];
   $groupedRecords[$category['ID']]['Distance'] = $category['Distance'];
}

So if I say:

foreach($groupedRecords as $record) {
    echo "The record id is: " . $record['ID'] . " and the Distance is " . $record['Distance'] . "";
}

What i see is: "The category is M40 and the distance is 3, The category is M50 and the distance is 3, The category is M60 and the distance is 3.

What I am expecting to see is: The category is M40 and the distance is 1, The category is M40 and the distance is 2, The category is M40 and the distance is 3.

I am sure if I understand correctly the above should work?

looks ok, but you used to overwrite every ID with the next one. Why don’t you just use the whole record?

$groupedRecords[$category['ID']][] = $category;

And always have a look at your results with print_r()

You really should provide

so anybody can reproduce that, use var_export here.

A sample of the records returned when getting the list of categories for the event:

[
  {
    Records: {
      Record: {
        0: {
        ID: "M40",
        Distance: "1",
        Title: "Veteran Men",},

       1: {
       ID: "M40",
       Distance: "2",
       Title: "Veteran Men"},

       2: {
       ID: "M40",
       Distance: "3",
       Title: "Veteran Men"},

      3: {
      ID: "M50",
      Distance: "1",
      Title: "Master Men"},

      4: {
      ID: "M50",
      Distance: "2",
      Title: "Master Men"},

      5: {
      ID: "M50",
      Distance: "3",
      Title: "Master Men"},

The data I need from the categories records is the ID of the category, and the distance.

A sample of the records returned when getting the distances for the event:

[
 {
  Records: {
     Record: {
       0: {
        ID: "1",
       Title: "Marathon",
       TimeTitleFinish: "42,195 km",},

     1: {
     ID: "2",
     Title: "Half Marathon",
     TimeTitleFinish: "21,097 km"},

     2: {
     ID: "3",
    Title: "10 km",
    TimeTitleFinish: "10 km",}}}},

The data I need from the distances is the ID and the TimeTitleFinish, to match the with the categories. So if The distance ID is 1 in the categories records, it needs to be matched with 42,195 km in the distance records.

So using the information in these records I would need to create a database table for each category and distance. So using the above records the tables would be:

M40_10KM
M40_21KM
M40_42KM

M50_10KM
M50_21KM
M50_42KM

I hope this information helps. As I said I have been struggling to get this set up fir a few days now so any help I can get is appreciated.

1 Like

What kind of madness is that format you get there? It’s not even valid JSON. I’m unwilling to build a parser myself, the fastest way is to post example data with var_export here so anybody can use it for testing purposes. If you don’t want to put in much afford for fancy stuff like indexed arrays, you could at least use a nested loop to link categories to distances like

foreach($categories as $category){
  foreach($distances as $distance){
    if($category->distance === $distance->ID){ // distance to this category
      $key = $category->distance.'_'.$distance->TimeTitleFinish;
      $grouped[$key] = ['category' => $category, 'distance' => $distance];
    }
  }
}

you may want to apply some additional procedures to get a valid key part from the distance.

Sponsor our Newsletter | Privacy Policy | Terms of Service