3

I'm trying to combine two arrays with PHP's array_combine() function and I receive this error:

array_combine() expects parameter 1 to be array, string given

var_dump of $subjects shows this:

array(
  (int) 0 => 'English',
  (int) 1 => 'Mathematics',
  (int) 2 => 'Biology',
  (int) 3 => 'Physics',
  (int) 4 => 'Chemistry'
) 

var_dump of $custom show this:

array(
  (int) 0 => array(
      'score' => '72',
      'grade' => 'B+',
      'points' => '10'
  ),
  (int) 1 => array(
      'score' => '99',
      'grade' => 'A',
      'points' => '12'
  ),
  (int) 2 => array(
      'score' => '77',
      'grade' => 'A-',
      'points' => '11'
  ),
  (int) 3 => array(
      'score' => '50',
      'grade' => 'C+',
      'points' => '7'
  ),
  (int) 4 => array(
      'score' => '66',
      'grade' => 'B+',
      'points' => '10'
  )
)

function:

function score($received,$arr) {
    $keys = $received;
    $data_set = array_combine($keys,$arr);
    return $data_set;
}

Here is where I call the function:

$data_set = array_map(array($this->Scores,'score'),$subjects ,custom);

if the debug shows all the variables as arrays: why is $subjects appearing as string to array_combine?

EDIT:

expected output

array(
'English' => array(
    'score' => '72',
    'grade' => 'B+',
    'points' => '10'
),
'Mathematics' => array(
    'score' => '99',
    'grade' => 'A',
    'points' => '12'
),
'Biology' => array(
    'score' => '77',
    'grade' => 'A-',
    'points' => '11'
),
'Physica' => array(
    'score' => '50',
    'grade' => 'C+',
    'points' => '7'
),
'Chemistry' => array(
    'score' => '66',
    'grade' => 'B+',
    'points' => '10'
)
)
2
  • 1
    What would be the expected output? I guess you just want to do this: $data_set = array_combine($subjects, $custom); You don't need array_map otherwise you loop through each element of $subjects, which then are strings Commented Apr 5, 2015 at 15:53
  • @ Rizier123 i have edited and included the expected output Commented Apr 5, 2015 at 15:57

2 Answers 2

2

As you updated your question with the expected output you don't need array_map(). Just do this:

$data_set = array_combine($subjects, $custom);

Otherwise what you did is loop through all elements of $subjects and $custom.

So what you did is this each iteration:

return array_combine("English", ["score" => "72", "grade" => "B+", "points" => "10"]);
return array_combine("Mathematics", ["score" => "99", "grade" => "A", "points" => "12"]);
//...
Sign up to request clarification or add additional context in comments.

Comments

1

Try this simply, no need for extra array_map

$data_set = array_combine($subjects, $custom);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.