Jump to content

Recommended Posts

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

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

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

  • 4 weeks later...

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.