PHP withdrawing attributes from database using explode - php

I'm searching the best way to withdraw some data from my MySQL field by I fail everytime. So here I come...
I got some data in my db which looks following: "attribute1=0::attribute2=1::attribute3=5 .. etc.".
Now I need to get that data so I can use it like this:
foreach($xxx as $attributeName => $attributeValue)
echo $attributeName . ' = ' . $attributeValue;
So the above will print smg like;
attribute1 = 0
attribute2 = 1
... etc.
Hope you understand and help me out with this.
Thank you in advance.

$final = array();
$str = "attribute1=0::attribute2=1::attribute3=5";
$pairs = explode('::', $str);
foreach ($pairs as $pair)
{
$keyValue = explode('=', $pair);
$final[$keyValue[0]] = $keyValue[1];
}
print_r($final);

So here is what you do:
$data = 'attribute1=0::attribute2=1::attribute3=5';
$data_tree = explode("::", $data);
foreach($data_tree as $node)
{
list($field,$value) = explode('=',$node);
echo $field.' : '.$value.'<br/>';
}
it will print:
attribute1 : 0
attribute2 : 1
attribute3 : 5
Good Luck!

Related

Select one item from array

So, I wanna select item per item from this array ["A185","A740","A540"]
Like for example I wanna select
$cie[0] = A185 or A740
with something like $cie[0] = A185
This is my code so far, since I fetch that code from a row in a MySQL table.
while ($row = pg_fetch_array($resul)) {
$cie10 = array($row["cie"]);
}
$cie = ["A185","A740"];
$values = array_count_values($cie);
$top = array_slice($values, 0, 1);
print_r($top);
What I get:
Array ( [["A185","A740","A540"]] => 1 )
It just won't work.
I'm Sure you are looking to display the data that is in the array variable
$var = ["A185","A740","A540"]; // Asume as you stored the values in the array called var
foreach($var as $x){
print_r($x);
echo "<br/>";
}
EDIT: Highlighted the code
If i understand your problem. You are looking for this:-
$Fullarray = ["A185","A740","A540"];
$cie = array_slice($Fullarray,0,2);
foreach ($cie as $name) {
$d[] = '"' . $name . '"';
}
$implodekeys = "[".implode(',',$d)."]";
$newarray[$implodekeys] =1;
echo "<pre>"; print_r($newarray);
Hope it helps!

How to extract PHP array values and remove data

I have seen a bunch of ways, but none that seem to work. My array data is coming back like this.
Array
(
[0] => RESULT=0
[1] => RESPMSG=Approved
[2] => SECURETOKEN=8cpcwfZhaH02qNlIoFEGZ1wO4
[3] => SECURETOKENID=253cad735251571cebcea28e877f4fd7
I use this:
<?php echo $response[2];?>
too get each out, that works. But I need to remove “SECURETOKEN=” so im left with just the number strings. I have been trying something like this with out success.
function test($response){
$secure_token = $response[1];
$secure_token = substr($secure_token, -25);
return $secure_token;
}
Also Im putting end number into a form input “Value” field. Not that that matters, unless it does?
Thanks
This is what I would do:
$keyResponse = [];
foreach ($response as $item) {
list($k, $v) = explode('=', $item, 2);
$keyResponse[$k] = $v;
}
Now you can easily access just the value part of each item based on the name:
echo $keyResponse['SECURETOKEN']; // output: 8cpcwfZhaH02qNlIoFEGZ1wO4
The advantage to this method is the code still works if the order of the items in $response changes
I get your secure token like this (tested):
<?php
$arr = array(
'RESULT=0',
'RESPMSG=Approved',
'SECURETOKEN=8cpcwfZhaH02qNlIoFEGZ1wO4',
'SECURETOKENID=253cad735251571cebcea28e877f4fd7'
);
$el = $arr[2];
$parts = explode('=', $el);
echo '#1 SECURETOKEN is ' . $parts[1];
// This break just for testing
echo '<br />';
// If you wanted to, you could revise the whole array
$new = array();
foreach( $arr as $el ){
$parts = explode('=', $el);
$new[$parts[0]] = $parts[1];
}
// Which would mean you could then get your securetoken like this:
echo '#2 SECURETOKEN is ' . $new['SECURETOKEN'];

create php variable from string

Hello i have a script that extract company names from a string. I want that the extracted names to be converted to php variable. So for example first result Real Coffee Sweeden must be converted to $RealCoffeeSweeden = 0 so i can assign a value to it
$test='/showname/0406741848 : Real Coffee Sweeden
/showname/0406741849 : Healthzone SE
/showname/16133663413 : FREE
/showname/16133663414 : RadioPlantes Canada
/showname/16133663417 : Dealsoftoday.Eu Canada
/showname/16136995593 : FREE
/showname/16136995594 : Club White Smile Canada
/showname/16138007442 : FREE
/showname/16138007547 : Mybitwave.com Canada
/showname/16465596150 : BABY
/showname/16465696956 : FREE
/showname/16465696957 : FREE
/showname/16467419944 : Mybitwave.com UK
/showname/16469181975 : FREE
/showname/21501350 : SecureSurf.EU NO
/showname/21501351 : FileCloud365 Norwegian
/showname/21501352 : FREE
/showname/21501353 : RadioPlantes Norwegian
';
$myRows= explode("\n", $test);
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$result[] = $pieces[1];
}
foreach ($result as $res){
$res // covert to php variable
//example: $RealCoffeeSweeden = 0;
}
You can try it this way
$my_array = explode("\n", $test);
foreach($my_array as $key => $value {
$my_string = explode(':', $value)
${str_replace(' ','', $my_string[1])} = $my_string;
echo $$my_string;
}
You should use an array for that. But if you want to do it the way you write, you can simply do something like that:
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$res = str_replace(' ', '', $pieces[1]); // replace whitespaces for valid names
$$res = 0; // note the double dollar signs
}
If you want to use an array tho, do something like this:
$result = [];
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$key = str_replace(' ', '', $pieces[1]);
$result[$key] = 0;
}
According to your comment, change second last line in the foreach loop with following:
$res = str_replace(' ', '', $res) . '_f';

How to generate a WHERE clause from an array with multiple AND-conditions

I have an HTML-table, where various selections can be made. The selected variables that contain the respective values, build the array $data[]. Now, with this array I would like to make an SQL request where all selected criteria should be met. This means, that I need the following request:
SELECT * FROM fruitgroups
WHERE $selection[1] = $value[1]
AND $selection[2] = $value[2]
AND $selection[3] = $value[3]
etc ...
Can anybody please help me with the loop that generates exactly the string:
...
$selection[1] = $value[1]
AND $selection[2] = $value[2]
AND $selection[3] = $value[3]
... etc ...
...that I need for the request?
Thank you in advance!
You can make a SQL request like this:
$selection = array("one", "two", "three");
$value = array("Tone", "Ttwo", "Tthree");
$concat = array();
foreach($selection as $key => $var){
$new = $selection[$key] . " = " . $value[$key];
array_push($concat, $new);
}
$concat = implode(" AND ", $concat);
$request = 'SELECT * FROM fruitgroups WHERE ' . $concat . ';';
echo $request;
Run example
Similar to the answer above, but keep it simple and don't forget the single quotes around the values:
$clauses = [];
foreach ($values as $i => $value) {
$conditions[] = "{$selection[$i]} = '$value'";
}
$query = "SELECT * FROM fruitgroups WHERE " . implode(' AND ', $conditions);
Even better, use an ORM like Eloquent:
$conditions = [];
foreach ($values as $i => $value) {
$conditions[$i] = $value;
}
$result = App\FruitGroups::where($conditions)->get();
I'm assuming you are sanitizing your inputs first, of course.

How to make a logical implementation of the below php code?

I need an out put like this.Can you please help me to sort this out?
a-1,2,4
b-3
for the following code:
<?php
$paytype = array('a','a','b','a');
$payno= array(1,2,3,4);
for($i=0;$i<count($paynum);$i++){
$paytypes = $paytype[$i];
$paynum = $payno[$i];
}
?>
Please Help
Just use array_unique, array_map and array_keys like this:
<?php
$paytype = array('a','a','b','a');
$payno= array(1,2,3,4);
$uniqArr = array_unique($paytype); //Get all the unique value from array
foreach ($uniqArr as $value) {
echo $value . "-" . implode(",",array_map("matchVal",array_keys($paytype,$value)))."<br/>";
}
function matchVal($x) {
global $payno;
return $payno[$x];
}
Output:
a-1,2,4
b-3
Demo
You can try this:
<?php
$paytype = array('a','a','b','a');
$payno= array(1,2,3,4);
$newArr = array();
for($i=0;$i<count($paytype);$i++){
if(!isset($newArr[$paytype[$i]])) {
$newArr[$paytype[$i]] = $payno[$i];
} else {
$newArr[$paytype[$i]] = $newArr[$paytype[$i]].",".$payno[$i];
}
}
print '<pre>';print_r($newArr);
?>
Output:
Array
(
[a] => 1,2,4
[b] => 3
)
Another alternative:-
Just use a simple foreach loop:-
$paytype = ['a','a','b','a'];
$payno= [1,2,3,4];
$res = [];
foreach($paytype as $k=>$v){
$res[$v] = empty($res[$v]) ? $payno[$k] : $res[$v].','.$payno[$k];
}
print '<pre>';print_r($res);
output:-
Array
(
[a] => 1,2,4
[b] => 3
)
If you want output
a-1,2,4
b-3
then add one additional foreach at the end.
foreach($res as $k=>$v){
echo "$k-$v<br>";
}
just use this code.
$paytype = array('a','a','b','a');
$payno= array(1,2,3,4);
$result = array();
for($i=0;$i<count($paytype);$i++){
$result[$paytype[$i]][] = $payno[$i];
}
$var = '';
foreach($result as $k => $v)
{
$var .= "{$k} - ". implode(",", $v) . "<br />";
}
print_r($var);
Result :
a - 1,2,4
b - 3

Categories