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);
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 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
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.
I am trying to write PHP code as a hobby project to basically create a "possible" code generator. The scenario is that we have a list of 25 valid characters that can be used.
Imagine that you have a 25 character code but you have accidentally scratched off the first two characters or three characters at any location in the code. Now we need to find all the possible combinations to try out. I have put all the valid characters into the array below that can be used in the code.
$valid=array("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","Z",
"2","3","4","6","7","8","9");
$arraylength=count($valid);
The still available or seen characters are input into a text box and in the place where the character is unreadable is left blank and the variable values are fetched.
$char1= $_POST['code1'];
$char2= $_POST['code2'];
$char3= $_POST['code3'];
$char4= $_POST['code4'];
$char5= $_POST['code5'];
$char6= $_POST['code6'];
$char7= $_POST['code7'];
$char8= $_POST['code8'];
$char9= $_POST['code9'];
$char10= $_POST['code10'];
$char11= $_POST['code11'];
$char12= $_POST['code12'];
$char13= $_POST['code13'];
$char14= $_POST['code14'];
$char15= $_POST['code15'];
$char16= $_POST['code16'];
$char17= $_POST['code17'];
$char18= $_POST['code18'];
$char19= $_POST['code19'];
$char20= $_POST['code20'];
$char21= $_POST['code21'];
$char22= $_POST['code22'];
$char23= $_POST['code23'];
$char24= $_POST['code24'];
$char25= $_POST['code25'];
And put into an array...
$jada = array($char1, $char2, $char3, $char4, $char5, $char6, $char7, $char8, $char9, $char10, $char11, $char12, $char13, $char14, $char15
, $char16, $char17, $char18, $char19, $char20, $char21, $char22, $char23, $char24, $char25);
I have been stumped for a while now, the fiddling I have done at the moment is that if a variable is empty then do something (as a test echo or print the possible combinations)
if(!isset($char1) || trim($char1) == ""){
for($x=0;$x<$arraylength;$x++) {
echo $valid[$x];
echo "<br>";
} }
else{
echo ($char1);
}
Can you guys help out?
Saw this still in an open status after many years of hiatus, I figured that I may as well share some information.
In the end I figured it out, you can grab the source here and test it in your own server: https://github.com/Masterkriz/XBOX_Pre-paid_code_fixer
I am having a problem passing an array variable from Flash (AS2) to PHP. In action script I have several arrays defined like this
output["px1"]
output["px2"]
output["px3"]
and then I use the following code to pass the variables into a php file
output.sendAndLoad("orders/print2cart.php",output,"POST");
I want to know how to get the data from the array in PHP. I have tried using $_POST['px1'], $_POST['output']['px1'], $_POST['output'] but I cannot seem to get any data. Any ideas as to what I can change to get the desired result?
Thanks!
EDIT: Just noticed that I one of the other variables in output (output.username) is also not being sent to PHP, despite it showing up in flash. Using the following code to alert to flash and it does show all the variables correctly.
getURL("javascript:alert('Print Stamp: " + output.PrintStamp + " User: " + output.username "')");
EDIT: Seems like once I send a pretty long array (or a string for that matter) none of the other fields associated with the LoadVars variable are sent either. I googled it up for limits and it says text limits are ~ 63000. Still not sure if that is the problem
Try it as a String.
Use Array.join(); in flash and send the value returned by that, then use explode() in PHP convert it back to an array.
var dataOut:LoadVars = new LoadVars();
var dataIn:LoadVars = new LoadVars();
dataOut.info = your_array.join("#");
vars.sendAndLoad("url", dataIn, "post");
dataIn.onLoad = function(go:Boolean):Void
{
if(go)
{
trace('success');
}
else trace('connection failed');
}
The PHP:
<?php
$str = $_POST["info"];
$myarray = explode($str);
?>
Since there were no other alternatives and I went through a lot of stuff before finally concluding that Arrays of large sizes cannot be passed through from AS2 to PHP very easily. My array was actually an image converted to pixels, so what I did was that I split the array into 2 pieces and posted to the PHP file twice instead of only once. Another alternative would be to split and post the array to a text file first and then read that text file directly from PHP.
You can do the same as you would do with HTML, by naming your parameters "array[0]", "array[1]", etc... :
var urlVariable:URLVariables = new URLVariables();
urlVariable["phpArray[0]"] = "arrayEntry0";
urlVariable["phpArray[1]"] = "arrayEntry1";
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://yourserver.com/phpScript.php");
request.method = URLRequestMethod.POST;
request.data = urlVariable;
loader.load(request);
then serverside you can verify the result received by php script :
print_r($_POST);
it should output :
Array
(
[phpArray] => Array
(
[0] => arrayEntry0
[1] => arrayEntry1
)
)
and for multiple dimension array you can use :
urlVariable["phpArray[0][0]"]