Related
I have an array inside a php file in JSON format. I want to display the most frequent element of the name object:
<?php
$mydata = '[{"data":[{"name":"amr selim","phone":"12345"},
{"name":"ame selim","phone":"12345"},
{"name":"\u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645","phone":"12345"},
{"name":"Amr Selim","phone":"12345"},{"name":"3mr","phone":"12345"},
{"name":"x","phone":"12345"},{"name":"\u0639\u0645\u0631\u0648\u0633\u0644\u064a\u0645","phone":"12345"},
{"name":"mr ","phone":"12345"},
{"name":"Amr sleem","phone":"12345"},
{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"\u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"Tv l Pc","phone":"12345"},{"name":"\u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645","phone":"12345"},{"name":"\u0625\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"Amr Selim","phone":"12345"},{"name":"Tv pc","phone":"12345"},{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"0012345"},{"name":"Amr Selim","phone":"0012345"},{"name":"Efham Computer","phone":"0012345"},{"name":"Tv 3l Pc","phone":"0012345"},{"name":"\u0627\u0641\u0647\u0645","phone":"0012345"},{"name":"Amr Selim","phone":"12345"},{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"+12345"},{"name":"\u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"+12345"},{"name":"\u0645. \u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645 \u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"\u0645. \u0639\u0645\u0631\u0648 \u0633\u0644\u064a\u0645 \u0627\u0641\u0647\u0645 \u0643\u0645\u0628\u064a\u0648\u062a\u0631","phone":"12345"},{"name":"Amr Selim","phone":"12345"},{"name":"amr Selim","phone":"12345"},{"name":"amr Selim","phone":"12345"}],"info":[{"lastid":"437397286","usernum":"12345","cantry":"EG"}]}]';
$task_array = json_decode($mydata,true);
print_r(array_count_values($task_array[0]["data"]));
?>
I'm trying to get the most frequent text in the name object
which in this case will be "amr selim".
I tried using this code but I get an error: "Warning: array_count_values(): Can only count STRING and INTEGER values!"
I don't know where to start, how to show the most frequent names in the array.
can you help me
You should use array_count_values function as follows.
array_count_values(array_column($task_array[0]["data"], 'name'));
I am trying to convert JSON plain text to php array but it always returns NULL. Actually, I am building a website which fetches the quran from other website through API. And the text type is ARABIC (utf_8).
Here's my JSON:
{"code":200,"status":"OK","data":[{"identifier":"ur.ahmedali","language":"ur","name":"\u0627\u062d\u0645\u062f \u0639\u0644\u06cc","englishName":"Ahmed Ali","format":"text","type":"translation"},{"identifier":"ur.jalandhry","language":"ur","name":"\u062c\u0627\u0644\u0646\u062f\u06c1\u0631\u06cc","englishName":"Fateh Muhammad Jalandhry","format":"text","type":"translation"},{"identifier":"ur.jawadi","language":"ur","name":"\u0639\u0644\u0627\u0645\u06c1 \u062c\u0648\u0627\u062f\u06cc","englishName":"Syed Zeeshan Haider Jawadi","format":"text","type":"translation"},{"identifier":"ur.kanzuliman","language":"ur","name":"\u0627\u062d\u0645\u062f \u0631\u0636\u0627 \u062e\u0627\u0646","englishName":"Ahmed Raza Khan","format":"text","type":"translation"},{"identifier":"ur.qadri","language":"ur","name":"\u0637\u0627\u06c1\u0631 \u0627\u0644\u0642\u0627\u062f\u0631\u06cc","englishName":"Tahir ul Qadri","format":"text","type":"translation"},{"identifier":"ur.junagarhi","language":"ur","name":"\u0645\u062d\u0645\u062f \u062c\u0648\u0646\u0627\u06af\u0691\u06be\u06cc","englishName":"Muhammad Junagarhi","format":"text","type":"translation"},{"identifier":"ur.maududi","language":"ur","name":"\u0627\u0628\u0648\u0627\u0644\u0627\u0639\u0644\u06cc \u0645\u0648\u062f\u0648\u062f\u06cc","englishName":"Abul A'ala Maududi","format":"text","type":"translation"},{"identifier":"ur.khan","language":"ur","name":"Shamshad Ali Khan","englishName":"Shamshad Ali Khan","format":"audio","type":"versebyverse"}]}
I think the problem is because of Arabic characters.
I have tried doing:
utf_unicode(json_decode($str, true));
stripcslashes(json_decode($str, true));
But it never worked
I have everything from other questions similar to mine.
Thanks
You are passing in the wrong parameters to utf_unicode() and stripcslashes(), both of these take a string not an array, json_decode() (in your case) is intended to return an array.
In order to use this JSON string in PHP directly, use PHP's NOWDOC or HEREDOC syntax like below:
Please try:
$str = <<< JSON
{"code":200,"status":"OK","data":[{"identifier":"ur.ahmedali","language":"ur","name":"\u0627\u062d\u0645\u062f \u0639\u0644\u06cc","englishName":"Ahmed Ali","format":"text","type":"translation"},{"identifier":"ur.jalandhry","language":"ur","name":"\u062c\u0627\u0644\u0646\u062f\u06c1\u0631\u06cc","englishName":"Fateh Muhammad Jalandhry","format":"text","type":"translation"},{"identifier":"ur.jawadi","language":"ur","name":"\u0639\u0644\u0627\u0645\u06c1 \u062c\u0648\u0627\u062f\u06cc","englishName":"Syed Zeeshan Haider Jawadi","format":"text","type":"translation"},{"identifier":"ur.kanzuliman","language":"ur","name":"\u0627\u062d\u0645\u062f \u0631\u0636\u0627 \u062e\u0627\u0646","englishName":"Ahmed Raza Khan","format":"text","type":"translation"},{"identifier":"ur.qadri","language":"ur","name":"\u0637\u0627\u06c1\u0631 \u0627\u0644\u0642\u0627\u062f\u0631\u06cc","englishName":"Tahir ul Qadri","format":"text","type":"translation"},{"identifier":"ur.junagarhi","language":"ur","name":"\u0645\u062d\u0645\u062f \u062c\u0648\u0646\u0627\u06af\u0691\u06be\u06cc","englishName":"Muhammad Junagarhi","format":"text","type":"translation"},{"identifier":"ur.maududi","language":"ur","name":"\u0627\u0628\u0648\u0627\u0644\u0627\u0639\u0644\u06cc \u0645\u0648\u062f\u0648\u062f\u06cc","englishName":"Abul A'ala Maududi","format":"text","type":"translation"},{"identifier":"ur.khan","language":"ur","name":"Shamshad Ali Khan","englishName":"Shamshad Ali Khan","format":"audio","type":"versebyverse"}]}
JSON;
$arr = json_decode($str, true);
print_r($arr);
Hope this helps,
I have a string like this
1 BDP-105159857 2602 BARUN SINHA MTE02 MTE07 MTE08 PHE11 PHE14
I want to split it like
1, BDP, 105159857, 2602, BARUN SINHA, MTE02, MTE07, MTE08, PHE11, PHE14
If I use
preg_split('/[-, ]+/',$string);
Then it splits the name (BARUN SINHA) in two parts as expected. But I want to keep it together. How to overcome it?
This is a screenshot of my data
I've got a JSON object containing some voter registration information that I'm trying to parse. I would like to retrieve the PersonKey from this object and store it in a variable.
[{"VoterKey":"31165750","PersonKey":"31165750","RNCID":"9959639415","State":"DC","Juriscode":"1100000000","Jurisname":"District of Columbia","CountyFIPS":"0","MCD":"","CNTY":"","Town":"","Ward":"07","Precinct":"097","Ballotbox":"","PrecinctName":"097","CD":"0","SD":"0","LD":"0","LDS":"","NamePrefix":"","FirstName":"MARCUS","MiddleName":"L","LastName":"JONES","NameSuffix":"","Sex":"M","BirthYear":"1982","BirthMonth":"11","BirthDay":"27","OfficialParty":"D","RNCCalcParty":"5","StateVoterID":"000129543","JurisdictionVoterID":"","RegistrationDate":"20030521","VoterStatus":"A","PermAbs":"","SelfReportedDemographic":"","ModeledEthnicity":"E1","ModeledReligion":"P","ModeledEthnicGroup":"Z","HHSEQ":"194061","HTSEQ":"2","RegistrationAddressKey":"19811832","RegistrationAddr1":"231 51ST ST NE","RegistrationAddr2":"","RegCity":"WASHINGTON","RegSta":"DC","RegZip5":"20019","RegZip4":"5420","RADR_LastCleanse":"2013-02-04","RADR_LastGeoCode":"2013-02-04","MailingAddressKey":"19811832","MailingAddr1":"231 51ST ST NE","MailingAddr2":"","MailCity":"WASHINGTON","MailSta":"DC","MailZip5":"20019","MailZip4":"5420","MADR_LastCleanse":"2013-02-04","AreaCode":"---","TelephoneNUm":"-------"},{"VoterKey":"31145016","PersonKey":"31145016","RNCID":"9959778025","State":"DC","Juriscode":"1100000000","Jurisname":"District of Columbia","CountyFIPS":"0","MCD":"","CNTY":"","Town":"","Ward":"07","Precinct":"102","Ballotbox":"","PrecinctName":"102","CD":"0","SD":"0","LD":"0","LDS":"","NamePrefix":"","FirstName":"MARCUS","MiddleName":"J","LastName":"JONES","NameSuffix":"","Sex":"M","BirthYear":"1900","BirthMonth":"01","BirthDay":"01","OfficialParty":"N","RNCCalcParty":"3","StateVoterID":"100019252","JurisdictionVoterID":"","RegistrationDate":"20100716","VoterStatus":"A","PermAbs":"","SelfReportedDemographic":"","ModeledEthnicity":"E1","ModeledReligion":"P","ModeledEthnicGroup":"Z","HHSEQ":"194001","HTSEQ":"1","RegistrationAddressKey":"19888718","RegistrationAddr1":"109 36TH ST NE","RegistrationAddr2":"2","RegCity":"WASHINGTON","RegSta":"DC","RegZip5":"20019","RegZip4":"2602","RADR_LastCleanse":"2013-02-04","RADR_LastGeoCode":"2013-02-04","MailingAddressKey":"19888718","MailingAddr1":"109 36TH ST NE","MailingAddr2":"2","MailCity":"WASHINGTON","MailSta":"DC","MailZip5":"20019","MailZip4":"2602","MADR_LastCleanse":"2013-02-04","AreaCode":"---","TelephoneNUm":"-------"},{"VoterKey":"31371499","PersonKey":"31371499","RNCID":"9959695444","State":"DC","Juriscode":"1100000000","Jurisname":"District of Columbia","CountyFIPS":"0","MCD":"","CNTY":"","Town":"","Ward":"07","Precinct":"097","Ballotbox":"","PrecinctName":"097","CD":"0","SD":"0","LD":"0","LDS":"","NamePrefix":"","FirstName":"MARCUS","MiddleName":"D","LastName":"JONES","NameSuffix":"","Sex":"M","BirthYear":"1900","BirthMonth":"01","BirthDay":"01","OfficialParty":"D","RNCCalcParty":"5","StateVoterID":"070016862","JurisdictionVoterID":"","RegistrationDate":"20070822","VoterStatus":"A","PermAbs":"","SelfReportedDemographic":"","ModeledEthnicity":"E1","ModeledReligion":"P","ModeledEthnicGroup":"Z","HHSEQ":"194398","HTSEQ":"1","RegistrationAddressKey":"126729320","RegistrationAddr1":"5019 FITCH PL NE","RegistrationAddr2":"","RegCity":"WASHINGTON","RegSta":"DC","RegZip5":"20019","RegZip4":"5447","RADR_LastCleanse":"2013-02-04","RADR_LastGeoCode":"2013-02-04","MailingAddressKey":"126729320","MailingAddr1":"5019 FITCH PL NE","MailingAddr2":"","MailCity":"WASHINGTON","MailSta":"DC","MailZip5":"20019","MailZip4":"5447","MADR_LastCleanse":"2013-02-04","AreaCode":"---","TelephoneNUm":"-------"}]
Any suggestions how best to handle this?
Use json_decode -- here's the entry in the PHP manual.
Example:
$data = '[{"VoterKey":"31165750","PersonKey":"31165750","RNCID":"9959639415"}]';
$arr = json_decode($data);
$person_key = $arr[0]->PersonKey;
This may be asked several times but my case is a bit different.
Let me start from the beginning.
$ck_data = db_select('ckeditor_settings', 'cs')
->fields('cs', array('settings'))
->condition('name', 'Advanced', '=')
->execute()
->fetchAssoc();
var_dump($ck_data);
Will give me...
array(1) {
["settings"]=>
string(2144) "a:33:{s:2:"ss";s:1:"2";s:7:"toolbar";s:606:"[
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
['Undo','Redo','Find','Replace','-','SelectAll'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
['Maximize','ShowBlocks'],
'/',
['Format'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript','-','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
['Link','Unlink','Anchor','Linkit','LinkToNode','LinkToMenu']
]";s:6:"expand";s:1:"t";s:7:"default";s:1:"t";s:11:"show_toggle";s:1:"t";s:7:"uicolor";s:7:"default";s:12:"uicolor_user";s:7:"default";s:5:"width";s:4:"100%";s:4:"lang";s:2:"en";s:9:"auto_lang";s:1:"t";s:18:"language_direction";s:7:"default";s:15:"allowed_content";s:1:"t";s:19:"extraAllowedContent";s:0:"";s:10:"enter_mode";s:1:"p";s:16:"shift_enter_mode";s:2:"br";s:11:"font_format";s:35:"p;div;pre;address;h1;h2;h3;h4;h5;h6";s:17:"custom_formatting";s:1:"f";s:10:"formatting";a:1:{s:25:"custom_formatting_options";a:6:{s:6:"indent";s:6:"indent";s:15:"breakBeforeOpen";s:15:"breakBeforeOpen";s:14:"breakAfterOpen";s:14:"breakAfterOpen";s:15:"breakAfterClose";s:15:"breakAfterClose";s:16:"breakBeforeClose";i:0;s:10:"pre_indent";i:0;}}s:8:"css_mode";s:4:"none";s:8:"css_path";s:0:"";s:9:"css_style";s:5:"theme";s:11:"styles_path";s:0:"";s:11:"filebrowser";s:4:"none";s:17:"filebrowser_image";s:0:"";s:17:"filebrowser_flash";s:0:"";s:13:"UserFilesPath";s:5:"%b%f/";s:21:"UserFilesAbsolutePath";s:7:"%d%b%f/";s:21:"forcePasteAsPlainText";s:1:"t";s:13:"html_entities";s:1:"f";s:17:"scayt_autoStartup";s:1:"t";s:15:"theme_config_js";s:1:"f";s:7:"js_conf";s:0:"";s:11:"loadPlugins";a:1:{s:12:"drupalbreaks";a:5:{s:4:"name";s:12:"drupalbreaks";s:4:"desc";s:51:"Plugin for inserting Drupal teaser and page breaks.";s:4:"path";s:25:"%plugin_dir%drupalbreaks/";s:7:"buttons";a:1:{s:11:"DrupalBreak";a:2:{s:5:"label";s:11:"DrupalBreak";s:4:"icon";s:22:"images/drupalbreak.png";}}s:7:"default";s:1:"t";}}}"
}
Now what I want is to get the value of toolbar.
$ck_settings = unserialize($ck_data['settings']);
$ck_plugins = $ck_settings['toolbar'];
var_dump($ck_plugins);
Will return...
string(606) "[
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
['Undo','Redo','Find','Replace','-','SelectAll'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
['Maximize','ShowBlocks'],
'/',
['Format'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript','-','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
['Link','Unlink','Anchor','Linkit','LinkToNode','LinkToMenu']
]"
My question now is how can I convert $ck_plugins from string to array?
Strange way to be storing that, but it is a string that looks like a PHP array definition:
eval("\$ck_plugins = $ck_plugins;");
print_r($ck_plugins);
If you have control over the data storage, you should probably either store the individual entries in a table or store the entire thing serialized or better in JSON.