Get Value from URL - GET Request - php

Hello I'm trying to get specific values out of my url that looks like this:
http://test.com/search.php?go&s=20&cacheKey=-5d36c171:14b930086c4:-561&cacheLocation=10.186.170.204:7300&customerSessionId=0ABAAACC-36C1-7191-4B92-30086C490817
What I have tried is e.g this:
$url .= '&cacheKey=' . strval($_GET['$cacheKey']);
But the value is not parsed correctly - What am I doing wrong ?
When I var_dump the $_GET I get the array out with all the values:
array(5) { ["go"]=> string(0) "" ["s"]=> string(2) "20" ["cacheKey"]=> string(26) "-5d36c171:14b930086c4:-561" ["cacheLocation"]=> string(19) "10.186.170.204:7300" ["customerSessionId"]=> string(36) "0ABAAACC-36C1-7191-4B92-30086C490817" }

Try using:
$url .= '&cacheKey=' . strval($_GET['cacheKey']);
Note the missing $ in the array index.

Remove $ from CacheKey.
$CacheKey is different from CacheKey.

Related

PHP acess value of array using key of 2D array

I'm working on a php magento script which have a array variable for store some script urls.
array variable $items['js']
var_dump
array(1) {
[""]=>
array(17) {
["prototype/prototype.js"]=>
string(22) "prototype/prototype.js"
["varien/form.js"]=>
string(14) "varien/form.js"
["mage/translate.js"]=>
string(17) "mage/translate.js"
["mage/cookies.js"]=>
string(15) "mage/cookies.js"
["wyomind/layer/native.history.js"]=>
string(31) "wyomind/layer/native.history.js"
["varien/weee.js"]=>
string(14) "varien/weee.js"
["geissweb/vatvalidation-min.js"]=>
string(29) "geissweb/vatvalidation-min.js"
}
}
I tried to access the "geissweb/vatvalidation-min.js" value like this
$items['js']['geissweb/vatvalidation-min.js']
but it return empty value, is there have a way to get that value without use foreach or for loop. Thank You
Your index is '', shown by...
array(1) {
[""]=>
so you need to use...
$items['js']['']['geissweb/vatvalidation-min.js']
You have your variable $items['js'] as an array of arrays what your looking for without a foreach is this :
$items['js'][0]['geissweb/vatvalidation-min.js'] is not valid
after tests
$items['js'][""]['geissweb/vatvalidation-min.js'] is valid.

Cannot convert the same properties key in json string to array by PHP json_decode

<?php
$array = json_decode('{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}');
echo json_encode($array,JSON_FORCE_OBJECT);
?>
My code is like that.
And it result just like :
{"image":"9711995133.jpg"}
Could you guys help me to have :
{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}
Thank you very much.
You must declare image as an array:
{"image":["9794948495.jpg","9703471814.jpg","9711995133.jpg"]}
Your format is overwriting each time the image key and you end up with the last parsed value :
{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}
when you will var_dump the array you will have:
array(1) { ["image"]=> array(3) { [0]=> string(14) "9794948495.jpg" [1]=> string(14) "9703471814.jpg" [2]=> string(14) "9711995133.jpg" } }
thus the original: $images = array('image'=>array("9794948495.jpg","9703471814.jpg","9711995133.jpg"));

Output Get Variables with multiple arrays in PHP

I am perplexed in getting the GET Variables for the url of this specific page.
The URL is :-
http://thisthat.com/category/?field_issue_month_value%5Bvalue%5D%5Byear%5D=2013&field_issue_month_value%5Bvalue%5D%5Bmonth%5D=10
I am trying to output both of these GET variables.
I tried following ways and it failed :(
echo urldecode("field_issue_month_value%5Bvalue%5D%5Bmonth%5D");
[field_issue_month_value]
echo $_GET['field_issue_month_value[value][year][0]'];
echo $_GET["field_issue_month_value[value][month]"];
echo urldecode($_GET['field_issue_month_value%5Bvalue%5D%5Bmonth%5D']);
The Var_dump is as follows:-
{ ["field_issue_month_value"]=> array(1) { ["value"]=> array(2) { ["year"]=> string(4) "2013" ["month"]=> string(2) "10" } } ["q"]=> string(19) "Page-Name" }
If $_GET contains an array, you should access the data the following way:
Your array is multidimensional, thus you need several indeces to get that data:
$_GET['field_issue_month_value']['value']['year'] ;
Otherwise using $_GET['field_issue_month_value[value][year][0]']; you just go 1-level deep, using one string as an index.
UPD:
This example works fine for me:
parse_str(urldecode('field_issue_month_value%5Bvalue%5D%5Byear%5D=2013&field_issue_month_value%5Bvalue%5D%5Bmonth%5D=10'), $r);
echo $r['field_issue_month_value']['value']['month'] . "<br/>" ;
echo $r['field_issue_month_value']['value']['year'] . "<br/>" ;

Parsing an un-named JSON array in PHP

My question: How can I break up and iterate through the JSON array pictured below?
I am making an AJAX web app and I need to serialize an array of objects in Javascript and put them in a url to pass to a php script. This is all going fine and the php script recieves the JSON like so..
$passed = $_GET['result'];
if(isset($passed)){
$passed = str_replace("undefined" , " " , $passed); /*had to add this to remove the undefined value*/
$json = json_decode(stripslashes($passed));
echo"<br/>";
var_dump($json ); //this is working and dumps an array
}
When I call var_dump on the decoded JSON I echo an output like so...
array(1) { [0]=> object(stdClass)#70 (2) { ["itemCount"]=> int(0) ["ItemArray"]=> array(2) { [0]=> object(stdClass)#86 (6) { ["itemPosition"]=> int(0) ["planPosition"]=> int(0) ["Name"]=> string(5) "dsfsd" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } [1]=> object(stdClass)#85 (6) { ["itemPosition"]=> int(1) ["planPosition"]=> int(0) ["Name"]=> string(4) "fdad" ["Description"]=> string(3) "sdf" ["Price"]=> string(0) "" ["Unit"]=> string(0) "" } } } }
The JSON
This is the JSON I am receiving. It seems like some of the pairs don't have names? How can I access elements in this Array?
Thanks alot guys
Some of these elements are coming back as stdClass objects as you can see in the var_dump output. You can get at the attributes with the standard object notation, for example, with your $json variable:
echo $json[0]->itemCount; // 0
echo $json[0]->itemArray[0]->itemPostion; // 0
You can also iterate over stdClass instances just like any PHP object, you'll be looping through the public data members, so again with your $json:
foreach(echo $json[0]->itemArray[0] as $key => $value)
echo 'key: ' . $key . ', value: ' . $value . PHP_EOL;
will loop through that first object, echoing out the member names and values of the object.
You just access them by index:
data[0] // first data item
Note that that's how you would "normally" access an array in the usual sense, so I might be missing something about your question here...

Newbie PHP: Insert a variable for echoing

I'd like to be able to echo $domain from this
$domain = $response['results']['$MYVAR']['shortUrl'];
I've tried curly braces and various other ways of formatting $MYVAR but the syntax is wrong.
Help most welcome !
EDIT --> var_dump($response):
object(stdClass)#1 (4) {
["errorCode"]=> int(0)
["errorMessage"]=> string(0) ""
["results"]=> object(stdClass)#2 (1) {
["http://www.domain.com"]=> object(stdClass)#3 (5) {
["userHash"]=> string(6) "oSEMki"
["shortKeywordUrl"]=> string(0) ""
["hash"]=> string(6) "oms2ZB"
["shortCNAMEUrl"]=> string(20) "http://bit.ly/LALALA"
["shortUrl"]=> string(20) "http://bit.ly/LALALA"
}
}
["statusCode"]=> string(2) "OK"
}
I can see the "domain.com" element fine but when I do this:
var_dump($response['results'][$MYVAR]);
it returns NULL ! Which must be why $domain = $response['results'][$MYVAR]['shortUrl']; fails too. Odd !
--EDIT 2 --
var_dump($MYVAR); gives:
string(118) "http://www.domain.com"
Try this:
$domain = $response['results'][$MYVAR]['shortUrl'];
echo $domain;
Are you sure it's stored in a 3 dimentional array like that?
Because that looks like needless complication.
Try it without quotes
$domain = $response['results'][$MYVAR]['shortUrl'];
or use double quotes
$domain = $response['results']["$MYVAR"]['shortUrl'];
EDIT:
In reaction to your edit. You are accessing variable like an associative array but the variable is instance of stdObject. So if you want to acces it, you must retype it like this:
$tmp = (array) $response;
$domain = $tmp['results'][$MYVAR]['shortUrl'];
or access it like object
$domain = $tmp->results->$MYWAR->shortUrl;
EDIT 2:
So it is strange, because http://www.domain.com is not 118 characters long, as var_dump wrote.
Where and how did you filled up the variable $MYVAR?
$domain = $response['results'][$MYVAR]['shortUrl'];
You don't need quotes around $MYVAR.
Have you tried without the quotes around $MYVAR?
It's an object.
$domain = $response->results->$MYVAR->shortUrl;

Categories