Example
$arr1 = array("1","2","3","4");
$arr2 = array("red","","green","");
I have 2 arrays, I need to make this key and value so I am using here array_combine() function
$keyVal = array_combine($arr1,$arr2);
when I use this array_combine
array_combine(): Both parameters should have an equal number of elements
This above error will be shown so I need to insert some dummy values where array values are empty, what can I do to this.
And this $arr2(array) values coming from input textbox,checkbox and radio buttons so I already tried that array_map function that only inserted the textbox null values not checkbox and radio buttons values.
Please, anyone, solve this issue. And thanking you spending time to reading this
Related
I just want to know that is there any simplest way to achieve the requirements below?
I Have 2 arrays
ARRAY1=[ "1","5","3","4"]
and
ARRAY2=[ "1","1","5","5","3","4"]
firstly I want to check is ARRAY2 contains all the values in ARRAY1.
i used array_unque and the diff which is working fine.
So now I want to also check is every values in ARRAY1 is repeat morethan one in array2 if it does return true..
How can I achieve this.. Iam a beginner.. can anyone help me with this?
$array1 = ["1","5","3","4"];
$array2 = ["1","1","5","5","3","3","4","4","7"]; // every value from array 1 is repeated
$array3 = sizeof(array_diff($array1, array_values(array_diff_key($array2, array_unique($array2))) )) > 0 ? false : true;
Link : https://3v4l.org/InJb6
I'm trying to store the users's input via the method get in an array to store it and further process it without overwriting the initial get-value. But I dont know how.. do I have to store them in a database to do that? Or can I just push every input into an array?
I believe the following should work for you... This will take all the $_GETs that you supply and put them in a new array so you can modify them without affecting the original $_GET array.
if(is_array($_GET)){
$newArr = $_GET; // modify $newArr['postFieldName'] instead of $_GET['postFieldName'] to preserve original $_GET but have new array.
}
That solution there will dupe the $_GET array. $_GET is just an internal PHP array of data, as is $_POST. You could also loop through the GETs if you do not need ALL of the GETs in your new array... You would do this by setting up an accepted array of GETs so you only pull the ones you need (this should be done anyways, as randomly accepting GETs from a form can lead to some trouble if you are also using the GETs for database/sql functions or anything permission based).
if(is_array($_GET) && count($_GET) > 0){
$array = array();
$theseOnly = array("postName", "postName2");
foreach($_GET as $key => $value){
if(!isset($array[$key]) && in_array($key, $theseOnly)){ // only add to new array if they are in our $theseOnly array.
$array[$key] = $value;
}
}
print_r($array);
} else {
echo "No $_GET found.";
}
I would just add to what #Nerdi.org said.
Specifically the second part, instead of looping through the array you can use either array_intersect_key or array_diff_key
$theseOnly = array("postName", "postName2");
$get = array_intersect_key( $_GET, array_flip($theseOnly);
//Or
$get = array_diff_key( $_GET, array_flip($theseOnly);
array_intersect_key
array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.
So this one returns only elements you put in $theseOnly
array_diff_key
Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
So this one returns the opposite or only elements you don't put in $theseOnly
And
array_flip
array_flip() returns an array in flip order, i.e. keys from array become values and values from array become keys.
This just takes the array of names with no keys (it has numeric keys by default), and swaps the key and the value, so
$theseOnly = array_flip(array("postName", "postName2"));
//becomes
$theseOnly = array("postName"=>0, "postName2"=>1);
We need the keys this way so they match what's in the $_GET array. We could always write the array that way, but if your lazy like me then you can just flip it.
session_start();
if(!isset($_SESSION['TestArray'])) $_SESSION['TestArray'] = array();
if(is_array($_GET)){
$_SESSION['TestArray'][] = $_GET;
}
print_r($_SESSION['TestArray']);
Thanks everybody for helping! This worked for me!
I've a multidimensional array of some arrays, in which the first values are ordered and included in a specific range.
Example:
A1=[[0,a],[3,b],[5,c],[6,a],[9,c]]
in which A1[i][0] are in range (0,10)
How can I obtain an array where, if the first value
(A1[i][0]) isn't a value present in the first array, e.g.
A1[i][0]==2
I insert an array with that value in the right position, with a specified second value (example A)?
Example of output i want:
A1=[[0,a],[1,A],[2,A],[3,b],[4,A],[5,c],[6,a],[7,A],[8,A],[9,c]]
This will help
$A1 = [[0,'a'],[3,'b'],[5,'c'],[6,'a'],[9,'c']];
foreach($A1 as $A2) $A3[] = $A2[0];//make a new array contain keys of the first array.
for($i=0;$i<=9;$i++){
if(!in_array($i, $A3)){
$A1[] = [$i, 'A']; //check if the key not exist, make a new array with key who does not exist.
}
}
asort($A1);//sort the new element inside the array
print_r($A1);
output is,
[[0,a],[1,A],[2,A],[3,b],[4,A],[5,c],[6,a],[7,A],[8,A],[9,c]]
I'm validating a form that submits up to 3 different id's depending on what the user selects.
I've put them into an array:
$submitted_genres = array($_POST['genre1'], $_POST['genre2'], $_POST['genre3']);
How I can check to make sure that none of the array values are equal each other?
You could use array_unique() to get an array of all unique values and then compare the size against the original array:
if (count(array_unique($submitted_genres)) !== count($submitted_genres)) {
// there's at least one dupe
}
how do i get the relation between the two array when both of them have values in some relation on the same indexes of both array for example,
i have retrieved "tagname" and "path" from one table of mysql and then i put these two column values in two arrays using loop so "array Tag[]" have vale "Introduction" and "array Path[]" have path value for introduction both values are on index "0" of there respected array and all data is collecten in "arrat Tag[]" and "array Path[]" in this manner after that i sort my "Tag" according to some other array using this code,
$sorted =array_intersection($some_other_array,$array Tag)
now how would i know the related path values for Tag as tag sorted ??
Hopes for your suggestions
from mysql/DB result set when you are creating array, create as
while($row = mysql_fetch_assoc($query)){
$array[$row['path']] = $row['tag'];
}
assuming your array as
$array['xyz'] = 'pqr';
$array['abc'] = 'wsx';
$array['poi'] = 'qaz';
$array['lkj'] = 'abc';
sort your array as per need based on tag or sorting methods available.
in this case instead of int index it will have key as path
sorting with tag also binds it with path.
after sorting your array (assuming sorting with first letter alphabates of tag name)
$array['lkj'] = 'abc';
$array['poi'] = 'qaz';
$array['xyz'] = 'pqr';
$array['abc'] = 'wsx';
so you can easily find path for your tag with foreach loop with key and value or with aray_keys if you want particular tag path and you know the tag value.
You need to combine those two arrays into associative arrays and then use asort() or uasort(), depending on particular sort scenario.