admin管理员组

文章数量:1335179

I would have thought this would be simple, however, I'm having a hard time with it. I have this function and I'm having a hard time mapping numbers. I have a spreadsheet that I import with WP All Import and utilize this function for a variety of other fields that are not numbers and it works great.

In example, other fields where I use this function is for something like 'Gasoline' => 'Gas' and it maps "Gasoline" to the "Gas" category without any issues, however when using only numbers (2, 4, 6 etc) as seen below, it won't map it to the "X Passenger" category that I have in the database. I hope I'm explaining this appropriately.

Anybody have any thoughts on what I'm doing wrong? Thank you in advance.

function seating_translate_data_sample( $data ) {

    if (empty($data)) {
        echo "Not Specified";
    }

    $map = array(
        '2'  => '2 Passenger',
        '4'  => '4 Passenger',
        '6'  => '6 Passenger',
        '8'  => '8 Passenger',
        '10' => '10 Passenger',
    );

    foreach ( $map as $partial_match => $mapped_value ) {
        if ( stristr( $data, $partial_match ) ) {
            return $mapped_value;
        }
    }
    return $data;
}

I would have thought this would be simple, however, I'm having a hard time with it. I have this function and I'm having a hard time mapping numbers. I have a spreadsheet that I import with WP All Import and utilize this function for a variety of other fields that are not numbers and it works great.

In example, other fields where I use this function is for something like 'Gasoline' => 'Gas' and it maps "Gasoline" to the "Gas" category without any issues, however when using only numbers (2, 4, 6 etc) as seen below, it won't map it to the "X Passenger" category that I have in the database. I hope I'm explaining this appropriately.

Anybody have any thoughts on what I'm doing wrong? Thank you in advance.

function seating_translate_data_sample( $data ) {

    if (empty($data)) {
        echo "Not Specified";
    }

    $map = array(
        '2'  => '2 Passenger',
        '4'  => '4 Passenger',
        '6'  => '6 Passenger',
        '8'  => '8 Passenger',
        '10' => '10 Passenger',
    );

    foreach ( $map as $partial_match => $mapped_value ) {
        if ( stristr( $data, $partial_match ) ) {
            return $mapped_value;
        }
    }
    return $data;
}
Share Improve this question edited Jun 1, 2020 at 21:09 Chad H asked Jun 1, 2020 at 19:18 Chad HChad H 11 bronze badge 3
  • It would be helpful if you would edit your question to show us some of the $data that's causing you problems. – Pat J Commented Jun 1, 2020 at 19:39
  • A quick tip for getting great answers: Many experts are busy people. Help them get to all the facts as soon as possible (without asking follow up questions) and you will get many more answers. Check our guide to asking good questions if you need to. – Matthew Brown aka Lord Matt Commented Jun 1, 2020 at 19:56
  • Hi @PatJ, does my revised question help some? – Chad H Commented Jun 1, 2020 at 21:13
Add a comment  | 

1 Answer 1

Reset to default 1

I'd guess that $data is being treated as an integer, and therefore your stristr is returning false.

Try explicitly casting $data as a string:

if ( stristr( strval( $data ), $partial_match ) ) {
            return $mapped_value;
        }

本文标签: How to map numbers utilizing array function