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'
)
)
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"]);
//...
Try this simply, no need for extra array_map
$data_set = array_combine($subjects, $custom);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With