user3078306
user3078306

Reputation: 13

Warning: Cannot use a scalar value as an array

function convert_realsoft($type, $input_id) {
  $dial_arr = array();

  switch ($type) {
    case 'action':
      $dial_arr = array(
        'lorem'       => 1,
        'ipsum'       => 2,
        'dolor'       => 3,
      );

      return $dial_arr[$input_id];

    break;

    case 'desk':
      $dial_arr = array(
        'sit'  => 1,
        'amet' => 2,
      );

      return $dial_arr[$input_id];

    break;

    ...and next case...
  }
}

This function returns me: Warning: Cannot use a scalar value as an array in...

Problem is in line return. After delete this line, warning disappear...

I tested several options. Anyone know where is the problem?

Upvotes: 1

Views: 2772

Answers (2)

Anil
Anil

Reputation: 2554

What are you exactly passing into the function for the $input_id parameter when you make the function call? Is it an integer or string? Can you post a sample function call?

My guess is that you are attempting to use an integer value to find the value in an array, yet your array needs a key to get the value based on how you are creating the array's in the function.

Also, I would write your return statement like this (I spoke of this in a previous comment)

function convert_realsoft($type, $input_id) {

  $dial_arr = array();

  switch ($type) {

    case 'action':

      $dial_arr = array(
        'lorem'       => 1,
        'ipsum'       => 2,
        'dolor'       => 3,
      );

    break;

    case 'desk':

      $dial_arr = array(
        'sit'  => 1,
        'amet' => 2,
      );

    break;

    //...and next case...
  }

  return $dial_arr[$input_id];

}

Upvotes: 0

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

i think this should be like this :-

function convert_realsoft($type, $input_id) {
  $dial_arr = array();

  switch ($type) {
    case 'action':
      $dial_arr[$input_id] = array(
        'lorem'       => 1,
        'ipsum'       => 2,
        'dolor'       => 3,
      );

      return $dial_arr[$input_id];

    break;

    case 'desk':
      $dial_arr[$input_id] = array(
        'sit'  => 1,
        'amet' => 2,
      );

      return $dial_arr[$input_id];

    break;

    ...and next case...
  }
}

Upvotes: 1

Related Questions