Currently I have this code that stores all $_GET requests:
$array_name = array();
foreach ($_GET as $key => $value) {
$array_name[] = "'%".escape_string($value)."%'";
}
My goal is to somehow only store certain values, if for example I have 5 different $_GET request, named 1,2,3,4 and 5. I only want to store 1-3 in this array. How would this be possible?
$array_name = array();
foreach ($_GET as $key => $value) {
if(in_array($key, array(1, 2, 3)))
$array_name[] = "'%".escape_string($value)."%'";
}
}
It this what you are looking for?
You can get the intersection of an array of keys you want:
foreach(array_intersect_key($_GET, array_flip([1,2,3])) as $value) {
$array_name[] = "'%".escape_string($value)."%'";
}
Slightly different approach to the other answers. $keysToStore is an array of the keys you want to store.
The foreach loop then pulls these values from the $_GET array rather than looping the entire array.
$keysToStore = [1, 2, 3];
$array_name = [];
foreach ( $keysToStore as $key ) {
$array_name[] = "'%" . escape_string( $_GET[$key] ) . "%'";
}
Edit: can check isset inside the loop if keys are not validated elsewhere.
$keysToStore = [1, 2, 3];
$array_name = [];
foreach ( $keysToStore as $key ) {
if ( isset( $_GET[$key] ) ) {
$array_name[] = "'%" . escape_string( $_GET[$key] ) . "%'";
}
}
For this, you need to add check on $key, that $key has value 1 or 2 or 3. Sample code is this:
$array_name = array();
foreach ($_GET as $key => $value) {
if($key == '1' || $key == '2' || $key == '3'){
$array_name[] = "'%".escape_string($value)."%'";
}
}
Or the other sample code is:
$array_name = array();
foreach ($_GET as $key => $value) {
if(in_array($key, array(1, 2, 3))){
$array_name[] = "'%".escape_string($value)."%'";
}
}
Here 1 , 2 and 3 is key of $_GET. Sample input is: $_GET['1'] = 'Ghazal' , $_GET['2'] = 'Taimur' , $_GET['3'] = 'Malik' , $_GET['4'] = 'Test' , $_GET['5'] = 'City'
Related
I am trying to generate sql statement dynamically with loops but i am not getting any idea on how to do it with html name array.
assuming $name and $message are post arrays ... and assuming length of both will be equal,
following is a method i tried;
<?php
$name = array('name1','name2' );
$mess= array('message1','message2' );
$values = array($name,$mess);
foreach ($values as $key){
foreach ($key as $value){
echo $value.",";
}
}
?>
output is =
name1,name2,message1,message2,
but i want output as =
(name1,message1),(name2,message2),
Edit : I have acess to $values only and I will not be able to determine how many values are going to join in $values ..
like it can be
$values=array($name,$message,$phone);
and the result i want will be
(name1,message1,phone1),(name2,message2,phone2)
My solutions is
Step 1: Loop your $values this will gonna loop every index of your array like $name
foreach($name as $index => $value) {
// do something
}
Step 2: Inside your loop values start with ( which mean wrap your detail in (
echo "(";
Step 3: loop parent array
foreach($values as $key => $arr) {
// do something
}
Step 4: Inside the loop of your parent array display each data according to your index
echo $values[$key][$index];
$key represents the index of your parent array while $index represents the index of your child array like $name
this loop will create data like
(name1,message1,phone1)
and I just add this
if ($key < sizeof(array_keys($values))-1) {
echo ",";
}
to avoid adding , after the last loop
This code will dynamically display array you put inside $values
So your code would be like this
$name = array('name1','name2','name3','name4' );
$mess= array('message1','message2','message3','message4' );
$phone= array('phone1','phone2','phone3','phone4' );
$married= array('yes','no','yes','yes' );
$values = array($name,$mess,$phone);
foreach($name as $index => $value) {
echo "(";
foreach($values as $key => $arr) {
echo $values[$key][$index];
if ($key < sizeof(array_keys($values))-1) {
echo ",";
}
}
echo "),";
}
Demo
or this
$name = array('name1','name2','name3','name4' );
$mess= array('message1','message2','message3','message4' );
$phone= array('phone1','phone2','phone3','phone4' );
$values = array($name,$mess,$phone);
foreach($name as $index => $value) {
$join = array();
foreach($values as $key => $arr) {
$join[] = $values[$key][$index];
}
echo "(".implode(",",$join)."),";
}
Demo
$name = array('name1','name2' );
$mess = array('message1','message2' );
foreach ($names as $k => $v){
echo "(".$v.",".$mess[$k]."),";
}
Try this, you not need two foreach loop, only use foreach loop and pass key to other array and get value
$name = array('name1','name2' );
$mess= array('message1','message2' );
$values = array($name,$mess);
foreach ($name as $keys => $vals)
{
echo "(".$vals.",".$mess[$keys]."),";
}
DEMO
You can use arrap_map() function in order to join two array. Here is reference http://php.net/manual/en/function.array-map.php
<?php
$name = array('name1','name2' );
$mess= array('message1','message2' );
$value = array_map(null, $name, $mess);
print_r($value);
?>
If I have several post results that are like this:
$_POST["ResponseA"] = 1, $_POST["ResponseB"] = 1, $_POST["ResponseC"] = 2, $_POST["ResponseD"] = 3, $_POST["ResponseE"] = 1, etc.
How can I perform a loop that gets an array based upon the values? So If I'm checking for a value of 1, I get ResponseA, ResponseB, ResponseE ?
<?php
$results = array_keys($_POST, 1);
var_dump($results);
?>
Use array_flip() like this...
$flipped = array_flip($_POST);
echo $flipped['1']; // ResponseA
You will get issues doing this though as your values are not unique
simple build a loop
<?php
$fields = array('ResponseA','ResponseB','ResponseC','ResponseD','ResponseE')
function searchValue(array $fields, $value) {
$out = array();
foreach($fields as $name) {
if(isset($_POST[$name]) && $_POST[$name] == $value) $out[]=$name;
}
return $out;
}
var_dump(searchValue($fields,1));
You can loop through the $_POST:
foreach ($_POST as $key => $value) {
if ($value == 1) {
// $key will equal the value ResponseA if $_POST["ResponseA"] = 1
}
}
$Ascore = 30
$Bscore = 30
$Cscore = 20
$Dscore = 20
$data = array(
'A1' => $Ascore,
'B1' => $Bscore,
'C1' => $Cscore,
'D1' => $Dscore
);
$highest = max($data);
foreach($data as $key => $value){
if($value === $highest){
echo $key;
//echo output (t1,t3);
}
something like this
getting them store in different variables
$type1 = $key[0]; //this will be t1//
$type2 = $key[1]; //this will be t3//
My intention is to somehow make the element I found at $key and put them into different variable , how I'm going to achieve that? As I have the idea but I cant get it work on.
Assuming I'm reading the question correctly, because it is a bit vague:
$data = [1,3,5,3,5];
$highest = max($data);
$result = array_keys(
array_filter(
$data,
function($value) use ($highest) {
return $value == $highest;
}
)
);
var_dump($result);
Do you mean store the keys where the corresponding value is the maximum value in the array? If so try:
$highest = max($data);
$max_keys = array();
foreach($data as $key => $value){
if ($value === $highest){
array_push($max_keys, $key);
}
}
If you must have the keys in separate variables just add:
list($type1, $type2) = $max_keys;
i am trying to generate dynamic array as
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key]=$value;
}
and got output as
array(
$data['Hieght'] => 5,
$data['Width'] =>6
)
But i need output as
array(
$data['Hieght'] >= => 5,
$data['Width'] >= =>6
)
i tried this
foreach($this->data['Carcase'] as $key=> $value)
{
if(!empty($value))
$data[$key].">=".=$value;
}
this is not Working.Anybody have an Idea About this.
Thanks in Advance.
I'm going to go out on a limb and guess that you want to concatenate the string ">=" to each key if the value is not empty:
$data = array();
foreach ($this->data['Carcase'] as $key => $value) {
if ($value) {
$data[$key . ' >='] = $value;
}
}
I want to create an new array with duplicated MAX value from an array
and put other duplicate value in an other array
$etudiant = array ('a'=>'2','b'=>'5', 'c'=>'6', 'd'=>'6', 'e'=>'2');
and i want this result
$MaxArray = array ('c'=>'6', 'd'=>'6');
$otherarray1 = array ('a'=>'2', 'e'=>'2');
Thank you !
First, find the maximum value:
$etudiant = array ('a'=>'2','b'=>'5', 'c'=>'6', 'd'=>'6', 'e'=>'2');
$maxValue = max($etudiant);
Second, find values that appear more than once:
$dups = array_diff_assoc($etudiant, array_unique($etudiant));
Lastly, check the original arrays for values matching either $maxValue or values that are listed in $dups:
$MaxArray = $OtherArray = $ElseArray = array();
foreach ($etudiant as $key => $value) {
if ($value == $maxValue) {
$MaxArray[$key] = $value;
} else if (in_array($value, $dups)) {
$OtherArray[$key] = $value;
} else {
$ElseArray[$key] = $value;
}
}
You'll get:
$MaxArray: Array
(
[c] => 6
[d] => 6
)
$OtherArray: Array
(
[a] => 2
[e] => 2
)
Note: I wasn't sure if you wanted the $MaxArray to contain the maximum value elements only if it appears more than once in the source array. If so, just change the max call to:
$maxValue = max($dups);
You can use array_values(array_intersect($array1, $array2)) to get duplicated values, and then make a loop to capture the keys which have those values and store them into another array.
$dups = array_values(array_intersect($array1, $array2))
$max = max($dups);
$result = array();
foreach ($array1 as $key => $value){
if (in_array($value, $dups)) {
$result[$key] = $value;
}
}
foreach ($array2 as $key => $value){
if (in_array($value, $dups)) {
$result[$key] = $value;
}
}
$maxArray = array();
foreach ($dups as $key => $value) {
if ($value == $max){
$maxArray[$key] = $value;
}
}
// results are in $dups and $maxArray
If you are looking to find elements with the min and max values from an array, the following will work.
// get min keys
$min_value = min($etudiant);
$min_keys = array_keys($etudiant, $min_value);
// get max keys
$max_value = max($etudiant);
$max_keys = array_keys($etudiant, $max_value);
You could then either rebuild your example arrays with these keys in a loop. Or access them directly, i.e. $etudiant[$min_keys].
Check out the documentation for array_keys, min, max