I am writing a script that collects all the meta data records and generates an array of all the posts and their respective meta data.
Everything seems to work fine except for when I go to push the meta data into the array. Below is the code that I am using and an example of the output that I get. What it is
if (mysql_num_rows($currentCourtMetaGet) != null) {
$metaArray = array();
while($currentCourseMetaArray = mysql_fetch_array($currentCourseMetaGet)){
$metaKey = $currentCourseMetaArray['meta_key'];
$metaValue = $currentCourseMetaArray['meta_value'];
$metaInfo = "$metaKey => $metaValue";
array_push($metaArray, $metaInfo);
}
$currentcourse = array(
"course_name" => $queryOneResultsArray['post_title'],
"course_id" => $queryOneResultsArray['id'],
"course_meta" => $metaArray
);
array_push($courseArray, $currentcourse);
}
Meta Array output:
[course_meta] => Array
(
[0] => licence_code =>
[1] => is_vocable => 0
[2] => region => Gladstone
)
My desired output is this.
[course_meta] => Array
(
licence_code =>
is_vocable => 0
region => Gladstone
)
Can anyone suggest a solution?
You can try this:
if (mysql_num_rows($currentCourtMetaGet) != null) {
$metaArray = array();
while($currentCourseMetaArray = mysql_fetch_array($currentCourseMetaGet)){
$metaKey = $currentCourseMetaArray['meta_key'];
$metaValue = $currentCourseMetaArray['meta_value'];
$metaArray[$metaKey] = $metaValue;
}
$currentcourse = array(
"course_name" => $queryOneResultsArray['post_title'],
"course_id" => $queryOneResultsArray['id'],
"course_meta" => $metaArray
);
array_push($courseArray, $currentcourse);
}
It will output:
[course_meta] => Array
(
[licence_code] =>
[is_vocable] => 0
[region] => Gladstone
)
Arrays are "dumped" in format [key] => value
. Unless you output them manualy, the exact format you are trying to achieve is not possible.
Have you tried doing it like this ?
$metaArray[$metakey] = $metaInfo;
There isn't a way to add values with key using associative arrays, what you've created with
$metaInfo = "$metaKey => $metaValue";
is a string variable, which has no key