alecthorne Posted December 2, 2012 Share Posted December 2, 2012 I have the following array within a field 'elements'. "b832581e-ef49-4cbf-a89b-7d67aa363e94": { "0": { "value": "Great Yarmouth" } }, "c74a0dba-5307-4f90-b04e-fdc88c4dd434": { "0": { "value": "Norfolk" } }, "91866e91-70d4-405f-900c-475d0c027399": { "country": { "0": "GB" } I was wanting tó extract the county 'Norfolk' and have tried the following $elements = $row['elements']; preg_match("@identifier=\"c74a0dba-5307-4f90-b04e-fdc88c4dd434\">\n*\s*<value><!\[CDATA\[([0-9]*)\]\]></value>@" , $elements, $array); $county=$array[1]; echo "<br>County=" . $county; This does not however extract the content from the identifier c74a0dba-5307-4f90-b04e-fdc88c4dd434 Have I missed something?? Thanks, Alec Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/ Share on other sites More sharing options...
Christian F. Posted December 2, 2012 Share Posted December 2, 2012 I think you'll find that json_decode () will help you accomplish what you want to do, not preg_match (). Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/#findComment-1396979 Share on other sites More sharing options...
alecthorne Posted December 3, 2012 Author Share Posted December 3, 2012 I used json_decode for the first time to extract data from the list below. { "metadata.title": "", "metadata.description": "CAD CAM Software Developer", "metadata.keywords": "CAD, CAM, Software", "metadata.robots": " ", "metadata.author": " ", "config.enable_comments": "0", "config.primary_category": "311" } This worked a treat using the following $catnum = json_decode($params); $catnum2 = $catnum->{'config.primary_category'}; But I am still stuck with the original question "c74a0dba-5307-4f90-b04e-fdc88c4dd434": { "0": { "value": "Norfolk" } As there are additional values after "c74a0dba-5307-4f90-b04e-fdc88c4dd434", how can I extract "Norfolk"? The following obviously did not work $county = json_decode($elements); $county2 = $county->{'c74a0dba-5307-4f90-b04e-fdc88c4dd434'}; echo $county2; I have looked at the help at php.net, but just cant get my head around this one. Thanks for your patience, Alec Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/#findComment-1397276 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 Run a var_dump () on the result from json_decode (), and it should become quite apparent. If you're still having issues after that, then I recommend reading upon on Arrays in the PHP Manual. Specifically how multi-dimensional arrays, and how you access their values. Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/#findComment-1397314 Share on other sites More sharing options...
alecthorne Posted December 26, 2012 Author Share Posted December 26, 2012 Christian, I have looked at multi-dimensional arrays, and thought the following would work. $county = $elements->getElement('c74a0dba-5307-4f90-b04e-fdc88c4dd434')->getElementData()->get('value'); All I receive is an internal server error. What have I missed? Thanks, Alec Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/#findComment-1401294 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2012 Share Posted December 26, 2012 Treating the data as an array will be much clearer - $arr = json_decode($elements,true); echo "<pre>",print_r($arr,true),"</pre>"; // take a look at the data $key = "c74a0dba-5307-4f90-b04e-fdc88c4dd434"; echo $arr[$key][0]['value']; Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/#findComment-1401302 Share on other sites More sharing options...
alecthorne Posted December 28, 2012 Author Share Posted December 28, 2012 Thanks for the help. I started looking as treating this as an array, but then was also given the following suggestion. That also worked. $county = json_decode($elements); $printcounty = $county->{"c74a0dba-5307-4f90-b04e-fdc88c4dd434"}->{"0"}->value; Thank you all for the help, and encouragement to learn php further.. Much appreciated... Alec Link to comment https://forums.phpfreaks.com/topic/271492-preg_match-help/#findComment-1401872 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.