I am very much hoping you can help me with this as I've spent all too much time on this. First, my JSON formatting is unfortunately not very mutable and I have moved it to a number of different formats to support both some jquery and a php-based search. Each time I move it, the search will work and the rest of the site will break or vice-versa.
Is it possible to access a JSON array by both name and index number? Here is my JSON (stored in PHP file and being retrieved & converted successfully to valid JSON):
<?php
$contents = array(
'Song Name #1 by Artist Name #1 (maininfo)' => array(
'contentid' => '1',
'aname' => 'Artist Name',
'sname' => 'Song Name',
'main' => 'core content #1',
'maininfo' => 'url')
),
'Song Name #2 by Artist Name #2 (maininfo)' => array(
'contentid' => '2',
'aname' => 'Artist Name',
'sname' => 'Song Name',
'main' => 'core content #2',
'maininfo' => 'url')
);
?>
My search works when something in the array title is matched on, otherwise it returns no matches so I must leave the array title as-is.
Another part of my project uses jquery and has the following:
parse(jsonobj[0][1]['sname']) //successfully already returning 'Song Name'
The above will ONLY work when the array title is not provided (e.g. 'Song Name #1 by Artist Name #1 (maininfo)' => array( becomes simply array(.
For those curious, file is being converted to JSON using:
var jsonobj;
$.ajax({
url: 'getjson.php',
dataType: "json",
success: function (doc) {
jsonobj = doc;
}
});
On the PHP side, when getjson.php is called the JSON array (above) is loaded in and converted to valid JSON using:
$final = array($final_contents);
header('Content-type: application/json');
echo json_encode($final);
Note: $final_contents is just $contents with an additional header added. See Searching JSON array for values and accessing surrounding keys/values; output as JSON for the PHP I have running specifically.
Thank you in advance.