Explode string into foreach - php

I have the following GET parameters:
?data=prop_123&data=prop_124&data=prop_125&data=prop_126&data=prop_129&data=prop_127
I was wondering if anybody knew a quick method of getting each value into a foreach? Thanks!

This won't work, because all of your parameters have the same name, so only the value "prop_127" is going to be passed to your script. You can create this as an array instead, like this:
?data[]=prop_123&data[]=prop_124&data[]=prop_125&data[]=prop_126&data[]=prop_129&data[]=prop_127
And then you can use foreach() to loop through them like this:
foreach ( $_GET['data'] as $data ) {
// This loops once for each instance of data[] in your URL
}
If you have no control over the URL, you'd have to read it in manually with $_SERVER['QUERY_STRING'], then parse through it to pull the values out.

Each data will overwrite the previous in $_GET or with parse_str so something like this maybe:
$s = 'data=prop_123&data=prop_124&data=prop_125&data=prop_126&data=prop_129&data=prop_127';
$s = str_replace('=', '[]=', $s);
parse_str($s, $a);
foreach($a as $something) { }
If you are actually trying to put the vars in a GET request and can get them from $_GET, then use the form data[] in the query string and then just foreach over $_GET.

You can use the PHP function: parse_str
http://www.php.net/manual/en/function.parse-str.php

Related

How to get params from encoded current URL?

Encoded URL
www.example.com/apps/?bmFtZTE9QUhTRU4mbmFtZTI9TUFFREEmcGVyY2VudD02NQ
Decoded URL
www.example.com/apps/?name1=AHSEN&name2=MAEDA&percent=65
now i want to get params from encoded URL
ob_start();
$name1 = $_GET['name1'];
You probably have the decoded url somewhere in a variable. If you extract the query part from it (the part after the ?), you can feed that query string to the function parse_str.
If you use parse_str with just the query string as argument, it sets the appropriate variables automatically. For example
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// feed it into parse_str
parse_str($my_query_string);
would set the global variables
$name1
$name2
$percent
But I'd advise to make use of the second parameter that parse_str offers, because it provides you with better control. For that, provide parse_str with an array as second parameter. Then the function will set the variables from your query string as entries in that array, analogous to what you know from $_GET.
// whereever you get the query part of your decoded url from
$my_query_string = "name1=AHSEN&name2=MAEDA&percent=65";
// provide parse_str with the query string *and* an array you want the results in
parse_str($my_query_string, $query_vars);
echo $query_vars['name1']; // should print out "AHSEN"
UPDATE: To split your complete decoded url, you can use parse_url.
You can use the function urldecode.
This should get you started...
<?php
$query = "my=apples&are=green+and+red";
foreach (explode('&', $query) as $chunk) {
$param = explode("=", $chunk);
if ($param) {
printf("Value for parameter \"%s\" is \"%s\"<br/>\n",
urldecode($param[0]), urldecode($param[1]));
}
}
?>

PHP Build url query from array

I'm trying to build URL query from an Array that looks like that:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
I would like to get query like that:
localhost/get?serial=3804689&serial=3801239&serial=3555689
(you get the idea)
I'm trying to use http_build_query($serials, 'serial', '&'); but it adds the numeric index to the prefix 'serial'.
Any idea how to remove that numeric index?
Well you can't really have the same GET parameter in the string, After all if you try to access that variable server side, what would you use?
$_GET['serial'] - But which serial would it get?
If you really want to get a list of serials, Simply turn the array into a string, save it as an array and there you go. for example :
$serials = "string of serials, delimited by &";
Then you can use the http build query.
Maybe use a foreach:
$get = "localhost/get?serial=" . $serials[0];
unset( $serials[0] );
foreach( $serials AS serial ){
$get .= "&serial=$serial;
}
Just as an FYI, PHP doesn't handle multiple GET variables with the same name natively. You will have to implement something fairly custom. If you are wanting to create a query string with multiple serial numbers, use a delimiter like _ or -.
Ex: soemthing.com/serials.php?serials=09830-20990-91234-12342
To do something like this from an array would be simple
$get_uri = "?serial=" . implode("-", $serials);
You would be able to get the array back from a the string using an explode to
$serials = explode("-", $_GET['serials']);
Yes its quite possible to have such format, you have to build it query string by indices. Like this:
No need to build the query string by hand, use http_build_query() in this case:
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$temp = $serials;
unset($serials);
$serials['serial'] = $temp;
$query_string = http_build_query($serials);
echo urldecode($query_string);
// serial[0]=3804689&serial[1]=3801239&serial[2]=3555689&serial[3]=3804687&serial[4]=1404689&serial[5]=6804689&serial[6]=8844689&serial[7]=4104689&serial[8]=2704689&serial[9]=4604689
And then finally, if you need to process it somewhere, just access it thru $_GET['serial'];
$serials = $_GET['serial']; // this will now hold an array of serials
You can also try this
$get = "localhost/get?serial=";
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$list = implode("&serial=",$serials);
echo $get.$list;
Having the same GET parameter will not work this way. You will need to use an array in the get parameter:
localhost/get?serial[]=3804689&serial[]=3801239&serial[]=3555689
IF you just print $_GET through this URL, you will receive
Array ( [serial] => Array ( [0] => 380468 [1] => 3801239 [2]=> 3555689) )
Then you can do this to build your query string
$query_str = 'serial[]='.implode('&serials[]=',$serials);
You can try this version.
$serials = ['3804689','3801239','3555689','3804687','1404689','6804689','8844689','4104689','2704689','4604689'];
$get = implode('&serials[]=',$serials);
echo 'test';
var_dump($_GET['serials']);
Result

Array to string conversion and Only variables should be passed by reference

I am not experienced with php(i am new).
i am trying to use this code http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php to find the name of a variable, but I keep getting:
Array to string conversion for the line :
$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
and also i get "only variables should be passed by reference" when I use
var_name($a, get_defined_vars());
How can I make those messages disappear? Because the entire code is working(I get the desired output).
Here is the code
<?php
function var_name (&$iVar, &$aDefinedVars)
{
foreach ($aDefinedVars as $k=>$v)
$aDefinedVars_0[$k] = $v;
$iVarSave = $iVar;
$iVar =!$iVar;
$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
$iVar = $iVarSave;
return $aDiffKeys[0];
}
$a=12;
echo var_name($a,get_defined_vars());
//ini_set('display_errors', '0');
?>
The Array to String conversion notice started in PHP v5.4.0. Since array_diff_assoc() doesn't search recursively, it is notifying you that it found that one of the values in your array is also an array and it had to convert it to a string.
Here's an example on how to use array_diff_assoc() for a multi-dimensional array... http://nl3.php.net/manual/en/function.array-diff-assoc.php#73972
Or perhaps switching out array_diff_assoc() for array_diff_key() would work for your purpose if you are only comparing the keys?
only variables should be passed by reference
You are passing the result of a function call as an argument. You aren't passing a variable.
$vars = get_defined_vars();
echo var_name($a,$vars);
Also, unless you're intentionally modifying one of the variables you shouldn't be passing it as a reference. That way any changes made are local to the function.

php issue accessing array

I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

How can I get all posted data as one single line in php?

I want to capture all posted data in one single line like name=asd&age=12&city=something and as an array while the data were posted by using form. (I don't wanna capture values like "$name=$_POST['name']")
(Ques-1: as a single line.
Ques-2: as an array.)
How can I do that ?
-Thanks.
I don't quite understand what you want, but I think you're looking for the following:
On a single line:
$raw_data = file_get_contents("php://input");
in an array
$array_data = $_POST // this is already an array?
$postAsLine = file_get_contents("php://input");
$postAsArray = $_POST;
echo $_SERVER['QUERY_STRING'] for current GET query
http_build_query for any array
in your case http_build_query($_POST)
You can use http_build_query($yourKeyValArray) which creates a query string from an array.
it sound like it:
$getAll = serialize($_POST);
$getAsArray = $_POST;
GOOD LUCK

Categories