0

I have an array $data:

Array
(
    [0] => Array
        (
            [quarter] => Q1
            [category] => DEODORANTS,FACE CARE
            [fund_type] => EOT
        )

    [1] => Array
        (
            [quarter] => Q2
            [category] => BODY CARE
            [fund_type] => A&P
        )

    [2] => Array
        (
            [quarter] => Q2
            [category] => ORAL CARE,NOCATEGORY
            [fund_type] => A&P
        )

)

and other array $categories:

Array
(
    [0] => Array
        (
            [id] => 1
            [descriptions] => DEODORANTS
        )

    [1] => Array
        (
            [id] => 2
            [descriptions] => BODY CARE
        )

    [2] => Array
        (
            [id] => 3
            [descriptions] => FACE CARE
        )

    [3] => Array
        (
            [id] => 4
            [descriptions] => ORAL CARE
        )
)

I need to change value of category in $data with id from $categories:

code:

function lookup($data = array(), $categories = array())
{

  if(is_array($data))
  {

    foreach ($data as $k => $v)
    {

      $valCat = explode(',', $v['category']);

      if(!empty($valCat))
      {

        foreach ($valCat as $vc)
        {

          foreach ($categories as $c)
          {

            if($c['descriptions'] === $vc)
            {

              $CatID[] = $c['id'];

            }

          }

        }

      }

      $data[$k]['category'] = $CatID;

    }

    return $data;

  }

}

result lookup($data, $categories):

Array
(
    [0] => Array
        (
            [quarter] => Q1
            [category] => Array
                (
                    [0] => 1
                    [1] => 3
                )

            [fund_type] => EOT
        )

    [1] => Array
        (
            [quarter] => Q2
            [category] => Array
                (
                    [0] => 1
                    [1] => 3
                    [2] => 2
                )

            [fund_type] => A&P
        )

    [2] => Array
        (
            [quarter] => Q2
                (
                    [0] => 1
                    [1] => 3
                    [2] => 2
                    [3] => 4
                )

            [fund_type] => A&P
        )

)

correct result should be:

Array
(
    [0] => Array
        (
            [quarter] => Q1
            [category] => Array
                (
                    [0] => 1
                    [1] => 3
                )

            [fund_type] => EOT
        )

    [1] => Array
        (
            [quarter] => Q2
            [category] => Array
                (
                    [0] => 2
                )

            [fund_type] => A&P
        )

    [2] => Array
        (
            [quarter] => Q2
                (
                    [0] => 4
                )

            [fund_type] => A&P
        )

)

1 Answer 1

1

You're not resetting the value of $CatID. After this line

foreach ($data as $k => $v)
{

add

    $CatID = array();
Sign up to request clarification or add additional context in comments.

Comments

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.