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/>" ;
Related
I have a PHP Script which works quite fine except that I get this error Message
Undefined index: Array in [...]/exp.php on line 239
On this line there is this code:
$out_kostenstelle = $kostenstellen[$nextShift["kostenstelle"]][1].
"(".$nextShift["kostenstelle"].")";
I think the only part where an Array as an Index can occur ist the part where $nextShift["kostenstelle"] is the index for $kostenstellen.
However when I try to catch this part (it is in a loop with many hundred runs so I can not manually check it) with this code, my script never enters the part inside the if clause
if(is_array($nextShift["kostenstelle"]))
{
echo "<pre>";
var_dump($nextShift);
echo "</pre>";
die();
}
This does not make any sense to me and I tried many things. without success.
I think this might be enough of the code where the error could be but just in case here are the structure of $kostenstellen and $nextShift
Kostenstellen:
array(2) {
[100]=>
array(2) {
[0]=>
string(3) "100"
[1]=>
string(11) "Company A"
}
[200]=>
array(2) {
[0]=>
string(3) "300"
[1]=>
string(12) "Company B"
}
}
and nextShift:
array(4) {
["id"]=>
string(2) "168"
["start_unix"]=>
string(10) "1466780000"
["end_unix"]=>
string(10) "1466812400"
["kostenstelle"]=>
string(3) "100"
}
There's no way around it: the problem is that the index you're trying to use is itself an array.
When you access an array in php, $array[$index], PHP will try to stringify it if it's not already a string or numeric. Stringifying an array gives the literal "Array"; like you have here.
However, there's another possibility: that when you run your loop, the array was stringified already. It means someplace before, someone casted it to a string.
You could check if with such an if:
if(is_array($nextShift["kostenstelle"]) || $nextShift["kostenstelle"] == "Array")
{
echo "<pre>";
var_dump($nextShift);
echo "</pre>";
die();
}
Using PHP & codeigniter.
I posting below to my PHP api
submitting from front end - > {"academicYear":"2015","subjectClassification":"Primary","subjectId":"55","teacherId":[10,16,17]}
I need to find or print teacherId values in my PHP code.
Basically my target is to print "HELLO" 3 time if there is 3 Id in teacherId array.
My code look like below,
function subjectTeacherAllocation_post(){
$data = remove_unknown_fields($this->post() ,$this->form_validation->get_field_names('subjectTeacherAllocation_post'));
$this->form_validation->set_data($data);
var_dump($data);
$teacherList = array($data['teacherId']);
echo $teacherList[0];
echo array_values($teacherList);
var_dump output --> array(3) { ["academicYear"]=> string(4) "2014" ["subjectId"]=> string(2) "55" ["teacherId"]=> array(3) { [0]=> int(9) [1]=> int(15) [2]=> int(32) } }
You are needlessly wrapping $data['teacherId'] in an extra array, instead just do:
$teacherList = $data['teacherId'];
echo $teacherList[0]; //9
Specifically, to produce hello x number of times, where x is the number of elements in the above $teacherList array:
foreach($teacherList as $unused){
echo 'hello';
}
I have an array with the following contents:
$tester
array(1) {
[0]=>
object(CategoryItem)#79 (17) {
["type"]=>
string(0) ""
["addedInVersion"]=>
string(4) "0.02"
["lastUpdatedInVersion"]=>
string(4) "0.02"
["AToZ"]=>
bool(false)
["name"]=>
string(22) "Page Name"
["scopeNotes"]=>
string(0) ""
["historyNotes"]=>
string(13) "Added in 0.02"
["broaderItems"]=>
array(0) {
}
I want to echo out the name, if this case Page Name and then use this in an if statement.
I have but this errors, I also tried $tester->CategoryItem->name but no joy.
Is there anything obvious I am missing?
You need to access it like this:
$name = $tester[0]->name;
echo $name;
you have some leaks in your php OOP understanding, you should fix them by following some tutorials like these ones:
http://www.killerphp.com/tutorials/object-oriented-php/
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
http://www.tutorialspoint.com/php/php_object_oriented.htm
Now to answer your question, your code should be this:
$the_name = $tester[0]->name;
if($the_name == 'whatever value you want') {
echo $the_name;
}
first of all, your initial variable is a array, therefor, $tester[0], then, this position is an object of the class CategoryItem so you use scope: $tester[0]->value
As for the last of your values in the class properties, broaderItems, this is again an array, so to access one of his values, you will have to call it like:
$tester[0]->broaderItems[0]; //or whatever the keys you will have here
Hope this helps!
:D
The var_dumps of the two arrays are different but they were created the exact same way . Why is this?????
first array dump:
array(2) {
[0]=>
array(0) {
}
[1]=>
string(6) ",2,2,1"
}
second array dump:
array(3) {
[0]=>
array(0) {
}
[1]=>
string(1) "2"
[2]=>
string(1) "2"
[3]=>
string(1) "1"
}
array 1 is made on fileA.php
while ($row = mysql_fetch_assoc($res))
{
$iState[] = $row["state"];
}///end while
then i use ajax to send to fileB.php
js_array=<? echo json_encode($iState); ?>;
var url_js_array=js_array.join(',');
xmlhttp.open("GET","fileB.php?istate="+ url_js_array,true);
xmlhttp.send();
then in fileB.php(ajax response file) i retrieve the array
$iStateValues[] =$_GET["istate"] ;
then I create array 2 on fileB.php
while ($row = mysql_fetch_array($res))
{
$currentiState[]= $row["state"];
}///end while
then I compred the two
echo"\n\nsame test\n";
if($iStateValues==$currentiState)
echo "same";
else
echo "not same";
The problem:
Currently, you're sending the comma-separated string to fileB.php.
$iStateValues[] = $_GET["istate"] ;
$_GET["istate"] contains the comma-separated string, and in the above statement, you're pushing the value into an empty array $iStateValues.
Now, in fileB.php, you create another array $currentiState using a while loop. Then you try to compare them.
$iStateValues is a string, not an array. $currentiState is a real array. So the comparison will always return FALSE.
How to fix the issue:
Instead of sending the comma-separated string to fileB.php, send the actual JSON string. So your code will look like:
js_str=<? echo json_encode($iState); ?>;
xmlhttp.open("GET","fileB.php?istate="+ js_str,true);
xmlhttp.send();
Now, in fileB.php, you can receive the JSON string and then decode it, like so:
$jsonArray = json_decode($_GET['istate'], true);
(The second parameter in JSON decode says it should be decoded to an associative array instead of an object).
Then do the comparison.
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...