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'));
Related
i have arrays as box1 box2 etc...i am trying to find the common elements/values between any 2 random arrays...but i keep getting error that target1 and target2 are not arrays below is my code:-
$box1=array("1","2","3","4","7","9");
$box2=array("11","2","34","4","72","9");
$box3=array("13","42","3","64","7","89");
$box4=array("71","24","38","43","7","19");
$box5=array("1","52","37","94","7","79");
$nos1=rand(1,5);
$nos2=rand(1,5);
$target1="$box".$nos1;
$target1="$box".$nos2;
$common=array();
$common=array_intersect($target1,$target2);
You are assigning random box to target in wrong way. Use the code below:
$box1=array("1","2","3","4","7","9");
$box2=array("11","2","34","4","72","9");
$box3=array("13","42","3","64","7","89");
$box4=array("71","24","38","43","7","19");
$box5=array("1","52","37","94","7","79");
$nos1=rand(1,5);
$nos2=rand(1,5);
$target1=${"box".$nos1};
$target2=${"box".$nos2};
$common=array();
$common=array_intersect($target1,$target2);
Adding to #KamalPaliwal 's answer:
Another way to accomplish/write this with variable variables:
<?php
$box1=array("1","2","3","4","7","9");
$box2=array("11","2","34","4","72","9");
$box3=array("13","42","3","64","7","89");
$box4=array("71","24","38","43","7","19");
$box5=array("1","52","37","94","7","79");
$varname1="box".rand(1,5);
$varname2="box".rand(1,5);
$target1 = $$varname1;
$target2 = $$varname2;
$common=array();
$common=array_intersect($target1,$target2);
I'm a beginner in json please help
I'm trying to access value of certain objects from an online published json file via php script and not able to do so following the examples from this forum
<?php
$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primary topic"]["CompanyName"];
print $companyname;
?>
i get the following error
( ! ) Notice: Undefined index: primary topic in C:\wamp\www\json.php on line 4
Call Stack
# Time Memory Function Location
1 0.0000 244456 {main}( ) ..\json.php:0
I have tried single and double quotes, [0] for array but to no avail
You should have to use primaryTopic :
$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primaryTopic"]["CompanyName"];
print $companyname;
Output will : ZENITH PRINT (UK) LIMITED
I think you had a mistake at 'primary topic' key. The key-name i saw in the response is 'primaryTopic'. Could you please check again?
In my application I make a call to an API which returns a JSON, unfortunately, this JSON is surrounded by a string with HTML elements
Example return
{"code":[{"name":"code","text":null,"attributes":[],"children":[]}],"message":[{"name":"message","text":"An error occurred while processing this request.","attributes":[],"children":[]}],"innererror":[{"name":"innererror","text":null,"attributes":[],"children":{"message":[{"name":"message","text":"Entiteit: Validatiefout","attributes":[],"children":[]}],"type":[{"name":"type","text":"System.Exception","attributes":[],"children":[]}],"stacktrace":[{"name":"stacktrace","text":"at Exact.Services.REST.DataServiceUpdateProvider.ErrorException(FaultException`1 fe, IExactRestConnection connection)\r\n at Exact.Services.REST.DataServiceUpdateProvider._Closure$__6._Closure$__7._Lambda$__6()\r\n at Exact.Services.REST.DataServiceUpdateProvider.SaveChanges()\r\n at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description)\r\n at System.Data.Services.DataService`1.HandleRequest()","attributes":[],"children":[]}],"internalexception":[{"name":"internalexception","text":null,"attributes":[],"children":{"message":[{"name":"message","text":"Property: Ongeldige referentie, Message: Artikelcode: 'EY05547'","attributes":[],"children":[]}],"type":[{"name":"type","text":"System.Exception","attributes":[],"children":[]}],"stacktrace":[{"name":"stacktrace","text":null,"attributes":[],"children":[]}]}}]}}]}<div class="debugmsgs"><div class="debugtitle"><span id="debugtitlename">Debugger</span> | <a id="debugtitlename2">Autofill</a> <div class="debugtitle-link"><a id="debugtitle-a" href="#" onclick="toggleDebug(); return false;">inklappen</a></div></div><div class="debugerrors"><div class="debugerror debugerror-php">Undefined index: content<br /><em>C:\laragon\www\b-</div><div class="debugerror debugerror-php">Undefined index: content<br /><em></em><br /></div></div></div>
as you can see in the json the following happens:
:[],"children":[]}]}}]}}]}<div class="debugmsgs">
Html is appended to the string, which causes me to be unable to JSON decode.
Therefore I need to remove the HTML, or extract the JSON.
What I would like to do is to place the JSON from the returned string into a variable for further processing.
So far if been trying to do this with a regex: {(?:[^{}]|())*} but i canot seem to get it right.
It seems that your json is malformed because some kind of debug code is produced together with the output, the right way to solve this is to find out what caused the debug message below to come out
<div class="debugmsgs"><div class="debugtitle"><span id="debugtitlename">Debugger</span> | <a id="debugtitlename2">Autofill</a> <div class="debugtitle-link"><a id="debugtitle-a" href="#" onclick="toggleDebug(); return false;">inklappen</a></div></div><div class="debugerrors"><div class="debugerror debugerror-php">Undefined index: content<br /><em>C:\laragon\www\b-</div><div class="debugerror debugerror-php">Undefined index: content<br /><em></em><br /></div></div></div>
If you can't get rid of the output, other way you can use is PHP explode to remove the unwanted string, like this:
$string = explode("<div", $api_output); //break the string into array using the '<div' as breaking point
$result = json_decode($string[0]); //decoded version of the json output
I have a table that has a field that contains what appears to be an array in it. Is there an easy way to convert this into an array that I can work with?
SELECT data FROM exp_cartthrob_item_options_options
Print_r(data);
Returns the following:
a:19:{i:0;a:6:{s:12:"option_value";s:18:"cp-1202-faraglioni";s:11:"option_name";s:16:"1202 Faraglioni ";s:5:"price";s:0:"";s:5:"color";s:5:"Putty";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:1;a:6:{s:12:"option_value";s:21:"cp-1203-scala-fenicia";s:11:"option_name";s:18:"1203 Scala Fenicia";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:2;a:6:{s:12:"option_value";s:19:"cp-1204-castiglione";s:11:"option_name";s:17:"1204 Castiglione ";s:5:"price";s:0:"";s:5:"color";s:5:"Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:3;a:6:{s:12:"option_value";s:19:"cp-1205-villa-jovis";s:11:"option_name";s:17:"1205 Villa Jovis ";s:5:"price";s:0:"";s:5:"color";s:3:"Tan";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:4;a:6:{s:12:"option_value";s:16:"cp-1207-anacapri";s:11:"option_name";s:14:"1207 Anacapri ";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:5;a:6:{s:12:"option_value";s:20:"cp-1208-monte-solaro";s:11:"option_name";s:17:"1208 Monte Solaro";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:6;a:6:{s:12:"option_value";s:13:"cp-BLCK-black";s:11:"option_name";s:10:"BLCK Black";s:5:"price";s:0:"";s:5:"color";s:6:"Black ";s:13:"grain_texture";s:5:"Full ";s:6:"finish";s:5:"Light";}i:17;a:6:{s:12:"option_value";s:21:"cd-0061-mexican-ochre";s:11:"option_name";s:18:"0061 Mexican Ochre";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:18;a:6:{s:12:"option_value";s:12:"cd-0063-soho";s:11:"option_name";s:9:"0063 SoHo";s:5:"price";s:0:"";s:5:"color";s:6:"Orange";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:19;a:6:{s:12:"option_value";s:13:"cd-0064-cocoa";s:11:"option_name";s:10:"0064 Cocoa";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:20;a:6:{s:12:"option_value";s:18:"cd-0065-brownstone";s:11:"option_name";s:15:"0065 Brownstone";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:21;a:6:{s:12:"option_value";s:13:"cd-0066-tabac";s:11:"option_name";s:10:"0066 Tabac";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:22;a:6:{s:12:"option_value";s:18:"cd-0067-toro-black";s:11:"option_name";s:15:"0067 Toro Black";s:5:"price";s:0:"";s:5:"color";s:5:"Black";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:23;a:6:{s:12:"option_value";s:23:"cd-0068-sun-dried-brick";s:11:"option_name";s:20:"0068 Sun-Dried Brick";s:5:"price";s:0:"";s:5:"color";s:12:"Medium Brown";s:13:"grain_texture";s:5:"Full ";s:6:"finish";s:5:"Light";}i:24;a:6:{s:12:"option_value";s:14:"cd-0069-gothic";s:11:"option_name";s:11:"0069 Gothic";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:25;a:6:{s:12:"option_value";s:23:"cd-0070-rembrandt-brown";s:11:"option_name";s:23:"0070 Rembrandt Brown ";s:5:"price";s:0:"";s:5:"color";s:12:"Medium Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:27;a:6:{s:12:"option_value";s:15:"cd-0078-pompeii";s:11:"option_name";s:12:"0078 Pompeii";s:5:"price";s:0:"";s:5:"color";s:3:"Red";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:30;a:6:{s:12:"option_value";s:21:"cd-0082-gulmard-green";s:11:"option_name";s:18:"0082 Gulmard Green";s:5:"price";s:0:"";s:5:"color";s:10:"Dark Green";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:31;a:6:{s:12:"option_value";s:21:"cd-0084-prussian-blue";s:11:"option_name";s:18:"0084 Prussian Blue";s:5:"price";s:0:"";s:5:"color";s:9:"Dark Blue";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}}
Yes, you can use unserialize() to achieve that:
SELECT data FROM exp_cartthrob_item_options_options
$array = unserialize($data);
var_dump($array);
Okay I am using a framework so I can pull database rows like: $username->thenthenameoftherow
But the row I want it to bet a variable because the variable is defined via a post method, I run that variable through an explode to get the first part, the text before the _
Yet when I run: and get currency like:
$coin = $_POST['coin'];
//currency they want to user
$currency = explode('_', $coin);
$username->$currency[1]
I can't set a hardcoded row for that, it has to be a userdefined viariable
I get the error:
Notice: Undefined property: stdClass::$usd in C:\xampp\htdocs\mvc\application\controllers\dashboard.php on line 193
Using mini mvc framework
Okay, for an exampple the post to set the $coin is BTC_USD I then explode it to remove the _ and get the 2nd part of the string which is USD I then want to get the USD table from my database by running the user query but I get that error.
Your code is basically:
$_tmp = $username->$currency;
$result = $_tmp[1];
Did you mean:
$_tmp = $currency[1];
$result = $username->$_tmp;
? Because if so, you want:
$result = $username->{$currency[1]};
do you mean
$username->currency[1]
without $ before currency