-2

The function below was contrived to fetch a .csv file, read down a column and turn its values into an array. Then if $category - in this case "printers" - occurs in the array, return "shipping-for-medium-sized-goods";.

function get_ship_class($category){
    $csv = array_map("str_getcsv", file("https://siliconharvest.com.au/cassius-files/Shipping%20Classes.csv", "r")); 
    $header = array_shift($csv); 

    // Seperate the header from data
    $col = array_search("medium_shipping_class", $header); 

    // Pass the extracted column back to calling method
    return array_column($csv,$col); 

    if ( in_array( $category, get_ship_class() )) {
   return "shipping-for-medium-sized-goods";
    }
}

I then run the function:

        get_ship_class("printers"); 
    // Note you can access the CSV file at that URL in the function above; 
    // See that"printers" is a value that occurs in the "medium_shipping_class" column
    // Therefore we have a match for if ( in_array( $category = "printers", get_ship_class() ))

Yet, I get the error:

Warning: file() expects parameter 2 to be integer, string given in /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php on line 147

Warning: array_map(): Argument #2 should be an array in /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php on line 147

Warning: array_shift() expects parameter 1 to be array, null given in /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php on line 148

Warning: array_search() expects parameter 2 to be array, null given in /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php on line 151

Warning: array_column() expects parameter 1 to be array, null given in /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php on line 154

7
  • 3
    The errors are pretty clear? Commented Nov 22, 2018 at 15:16
  • 1
    All these errors are self explaining Commented Nov 22, 2018 at 15:17
  • If you still don’t get it, then go read the documentation for file! php.net/manual/en/function.file.php The second parameter you are passing to this function here clearly makes no sense. Commented Nov 22, 2018 at 15:18
  • 1
    It would be nice to think that all answers are correct, but unfortunately there are both minor (as well as major) bugs as well as PHP moving on which means you have to check everything yourself and not assume anything will work. Commented Nov 22, 2018 at 15:28
  • 1
    It's never a good idea to just pipe through a response from one function (that you don't have control over, like calling external URL's) into another function without checking the data first. That URL might go down at some point or send you back invalid results. It's better to do one thing at the time and have proper validation and error handling. Commented Nov 22, 2018 at 15:31

2 Answers 2

1

As @Maio290 says, the errors are very clear:

You're using a string of "r" rather than an integer as the second parameter:

http://php.net/manual/en/function.file.php

Flags

You need an integer, or one of the following constants:

FILE_USE_INCLUDE_PATH Search for the file in the include_path. I think this is 1.

FILE_IGNORE_NEW_LINES Omit newline at the end of each array element I think this is 2.

FILE_SKIP_EMPTY_LINES Skip empty lines. I think this is 3.

Resolving that may well resolve the other errors, but if not, or if any questions, let me know, and I'll do my best to extend my answer to help you.

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

6 Comments

After removing the optional flag, one error remains Warning: array_column(): The column key should be either a string or an integer in /home/ptrcao/public_html/siliconharvest/wp-content/uploads/wpallimport/functions.php on line 154
What is the content of $col?
$col = array_search("medium_shipping_class", $header);
column_key: The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).
@ptrcao - If array_search() doesn't find anything, it returns false which is an invalid value for array_column(). You should really add some checks and validation to your code instead of just assuming that all functions returns what you expect.
|
1
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
function get_ship_class($category){
    $csv = array_map("str_getcsv", file("https://siliconharvest.com.au/cassius-files/Shipping%20Classes.csv"));
    $header = array_shift($csv);
    // Seperate the header from data
    if(in_array( "medium_shipping_class" ,$header)){
        $col = array_search("medium_shipping_class" ,$header);
        if($col = 'false'){
            $col = '0';
        }
    }

    // Pass the extracted column back to calling method

    return array_column($csv,$col);
}


print_r(get_ship_class('printers'));

Seemed easier at first but there was this issue with the boolean value. So let me explain a bit. Except of the file problem where you were passing a string as second parameter (don't know why you did that) moving forward you would have an issue with the $col. Due to the fact that your element was in the 1st position of the array 0 would be converted to false (boolean value). So what i did is i worked around it a bit to first ensure that the value you are looking for is in the array and after get it's position and if it is false then to convert it to 0.

  if ( in_array( $category, get_ship_class() )) {
   return "shipping-for-medium-sized-goods";
    }

This part i just took it out it is unreachable any way. It comes after your return so you will never be able to reach there. A function returns the values and nothing happens after that. Consider it something like die();. Further more the output result is like this:

Array ( [0] => printers [1] => monitors [2] => cases )

1 Comment

Thanks for that - seems very insightful - I'll have to revisit this tomorrow - it's so late here and my brain is dead. I'll be back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.