I have a URL formatted like this:
https://chipperyman.com/commander/codes/check.php?type=check&group=w&cpu=63_char_string&version=27&group=1
However, when I use var_dump($_GET);, the w variable is set to &string(1) => 1. This is the full array:
array(4) { ["type"]=> string(5) "check" ["group"]=> &string(1) "1" ["cpu"]=> string(63) "63_char_string" ["version"]=> string(2) "27" }
Why is $_GET['group'] not outputting correctly? When I try to echo it or use it in anything else (.=, etc) it is not treated as a w.
group=1 overrides group=w since it comes last and you can't have 2 variables with the same name.
It doesn't seem like you want this, but it is possible to have a group array using:
group[]=w&group[]=1
Related
I have really no idea what's happening. The story:
I use PDO for a SELECT statement on a database.
$sql = "SELECT a,b,c,performance,points,compare
FROM normvalues
WHERE x=:x AND y=1 AND z=:z";
$stmt = $GLOBALS['PDO']->prepare($sql);
$stmt->bindParam(":x",$x);
$stmt->bindParam(":z",$z);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
So that's fine and it is working. When I var_dump the $res variable I get something like:
array(6) {
["a"]=> string(2) "44"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "50.1"
["points"]=> string(1) "1"
["compare"]=> string(2) "-1"
}
[1]=>
array(6) {
["a"]=> string(2) "57"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "47.7"
["points"]=> string(1) "2"
["compare"]=> string(2) "-1"
}
[2]=>
array(6) {
["a"]=> string(2) "70"
["b"]=> string(4) "1176"
["c"]=> string(4) "1166"
["performance"]=> string(4) "44.7"
["points"]=> string(1) "3"
["compare"]=> string(2) "-1"
}
...
That's also okay. But I have to sort the result another way. So I am doing:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
$this->normTable[$e['a']]['compare']=$e['compare'];
}
And now I am completely lost. By assigning $e['performance'] I get wrong values. Actually this should be the performance values.
[1176]=>
array(4) {
[1]=> string(2) "50"
["compare"]=> string(2) "-1"
[2]=> string(2) "48"
[3]=> string(2) "45"
}
I already checked the values in the database and they are correct. By doing doubleval() I'd get the right values, but the problem is that not every value is a double but also integer or string. I also tried to typecast with (string) but it's the same result. I have no explanation.
Update:
It's a very big project and I just tried to minimize it as possible and to make my problem as clear as possible. But now I have figured out something new:
I do an 'echo()' of my first variable in the normTable during the loop:
foreach($res as $e){
$this->normTable[$e['a']][$e['points']]=$e['performance'];
echo "a:".$e['a']." pt: ".$e['points']." perf: ".$e['performance']."-".$this->normTable[1176][1]."\n";
$this->normTable[$e['a']]['compare']=$e['compare'];
}
and the value is changing from '50.1' to '50'. Still can't figure out the reason. Is there a size limitaion of arrays in PHP?
UPDATE 2 and a big SORRY!
As I said, it is a big project. So the table I read out, has some values for some attributes twice or more. Actually such a case should not happen. That's why the answer is simple: It became 50 because 50 was assigned. I'm so sorry for having waisted your time. But I totally excluded this case and since I am also coding in C, my first thought was: memory leak - clear case!
Thanks for your help.
Meh, I don't have 50 reputation; can't comment only answer.
If you replace a with be in your loop, you should get your expected result (given your data sample). I.e.:
foreach($res as $e){
$this->normTable[$e['b']][$e['points']]=$e['performance'];
$this->normTable[$e['b']]['compare']=$e['compare'];
}
But I'm not sure this really solves your problem. YMMV.
So, I am returning 2 different multi-dimensional arrays from 2 separate db calls.(Can't use a join because of our specific API for db calls)
I need to search through 2nd array and if a value matches that of a value in the first array, I need to append the related data to that row in first array for display in datatable. I have tried numerous things to just get the values to show. Here is what my arrays look like and where I am at with it.
Array 1
{"status":"SUCCESS","message":"Successfully read","data":
[{"ordnbr":"12345",
"custid":"CUSTID #",
"custname":"My Customers Name",
"custordnbr":"54321",
"slsperid":"ID",
"orddate":"2015-05-14",
"shipdate":"2015-06-03",
"dtrdy":"2015-05-29",
"carrier":"Some Carrier",
"totord":"21703",
"shipped":"4341",
"notes":"These are notes",
"siteid":"Some Site",
"orderstatus":"RDY"},
{"ordnbr":"23456",
"custid":"Anoter Customer",
"custname":"Another Customers Name",
"custordnbr":"65432",
"slsperid":"ID2",
"orddate":"2015-05-19",
"shipdate":"2015-06-05",
"dtrdy":"2015-06-05",
"carrier":"Another Carrier",
"totord":"98875",
"shipped":"0",
"notes":" ",
"siteid":"Another Site",
"orderstatus":"GR"},
etc... The list goes on as there are numerous records.
Second array looks kind of like this(var_dump) I realize this is an array of objects but am casting each line to array.
array(41) {
[0]=>
object(stdClass)#2923 (15) {
["trknbr"]=>
string(12) "7890"
["siteid"]=>
string(3) "This Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "822"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(30) "CustName"
["detail"]=>
array(1) {
[0]=>
object(stdClass)#2922 (7) {
["trknbr"]=>
string(12) "BR549"
["ordnbr"]=>
string(6) "12345"
["dtrelease"]=>
string(10) "2015-06-04"
["custid"]=>
string(6) "Custid"
["shipto"]=>
string(13) "Some City"
["slsperid"]=>
string(2) "WE"
["comments"]=>
string(21) "Truck # 2 of 5
$2360"
}
}
}
[1]=>
object(stdClass)#2916 (15) {
["trknbr"]=>
string(12) "34563"
["siteid"]=>
string(3) "My Other Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "244"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(25) "Another Customer"
["detail"]=>
array(1) {
[0]=>
object(stdClass)#2830 (7) {
["trknbr"]=>
string(12) "34563"
["ordnbr"]=>
string(6) "34578"
["dtrelease"]=>
string(10) "2015-06-08"
["custid"]=>
string(6) "wwweee"
["shipto"]=>
string(12) "Another City"
["slsperid"]=>
string(3) "RRR"
["comments"]=>
string(31) "DO NOT SHIP BEFORE 6/8
$ 769P"
}
}
}
So what I need to do is, for example, if the ordnbr field is the same for any 2 arrays, I need to append a couple fields from the 2nd array onto the first array. Right now, I am just trying to get the data to return so I can make the comparison and it doesn't seem to be comparing. I have tried using a foreach loop and if array1->ordnbr == array2->ordnbr, then do something... That doesn't work. Tried using in_array and searching for array1->ordnbr in array2.
Nothing I have done works. Can someone please tell me in what direction I should proceed to make these comparisons?
Thanks in advance.
I may not have been real clear on what I need to do... Ok... With Array 1(which did start out as a JSON object but which I converted to multidimensional array using json_decode), I need all rows regardless of whether they match Array 2 or not. However, if ordnbr matches an ordnbr on a row in Array2, then I need to include some data from that row in Array2 into the corresponding row of Array1.
What you would do is to create 2 associative arrays, one for each array with key is ordnbr and value is the whole object contain ordnbr. For example:
$arr1 = array()
foreach ($input1["data"] as $row){
$arr1[$row["ordnbr"]] = $row;
}
The do the same for your second array $arr2. After that, a simple foreach would do the job
foreach ($arr1 as $order_number => $row){
if in_array($order_number, $arr2){
// $row2 will have the same $order_number as $row1
$row2 = $arr2[$order_number];
// then do whatever you need here
// ...
}
}
I have written an xml-parser to read an XML-file. The XML-file is not mine so I can't change the structure. Things work great till I got to this special point. I want to read a value but I don't have a key to access this value.
I marked the values (in red) in the screenshot below which I want to access.
When I dump the parent element (the PRAT->VALUE) I get this in return:
object(SimpleXMLElement)#31 (3) { ["#attributes"]=> array(5) { ["nr"]=> string(1) "1" ["unit"]=> string(3) "bar" ["unit_id"]=> string(4) "3103" ["vo"]=> string(0) "" ["vo_id"]=> string(0) "" } [0]=> string(2) "20" [1]=> string(1) "2" }
As seen, at the end of the dump the values that I want to access are presented. I tried to access it like an array but that doesn't work. The values are not part of the attributes.
use (String) keyword in front of it.
eg.
echo (String) PRAT->VALUE;
I'm trying to get the values of a sub array out in a foreach loop.
I looked at this example as it seemed really relevant to my problem, but unfortunately I've not been able to get it to work, and I'm hoping someone here can pick up where I've gone wrong.
PHP foreach not working with sub-array
I have an array called $booked and inside that array I have a sub array $booked['30_booked'].
Within that second array there are multiple values, such as title, name, address etc.
My code currently looks like this:
foreach($booked['30_booking'] as $new_var){
var_dump('</br>', $booked['30_booking']);
var_dump('</br>', $new_var);
var_dump($new_var['title'], $new_var->title, $booked['30_booking']->title); exit();
}
I've output the data as you can see above in var_dump statements to try and get one of these methods to work.
Unfortunately nothing within $new_var is pulling out the title, but $booked['30_booking']->title
Have I not put it into the foreach statement correctly?
All help appreciated - thanks!
EDIT:
Main array output snippet:
array(6) { ["30_booked"]=> object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" } }
EDIT 2:
Sub Array $booked['30_booking'] snippet:
object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" }
EDIT 3:
My var_dump of the $new_var by itself is bringing back the value of the id - but when I try and get the second value out the sub array "title" it doesn't return anything.
FINAL FIX:
Thanks to Kita I realised I was returning a std class object and not a second array, something that I stupidly missed the first time round. Because of that I can't actually foreach on the object.
Which led me to this post which will help me fix the issue:
PHP foreach array with stdClass Object
Thank you very much for all your help!!!
You expected an array inside $booked['30_booking'] but in fact there was a stdClass object.
array(6) {
["30_booked"]=> object(stdClass)#21 (34) {
["id"]=> string(2) "30"
["title"]=> string(2) "Ms"
["firstname"]=> string(5) "FIRST NAME"
["surname"]=> string(9) "LAST NAME"
["address"]=> string(6) "- -- -"
["postcode"]=> string(7) "FAK E99"
["country"]=> string(14) "United Kingdom"
["phone"]=> string(11) "01221111111"
["alt_phone"]=> string(0) ""
["email"]=> string(25) "fake#fake.co.uk"
["notes"]=> string(8) "FAKE DEAL"
}
//I assume you have left out the other array elements from the Main array snippet.
}
Getting stdClass instead of array usually happens when you parse a JSON string with json_decode() without the second parameter.
// without second parameter
var_dump( json_decode( '{"id":"30"}' ) );
object(stdClass)#1 (1) {
["id"]=>
string(2) "30"
}
// 'true' as second parameter
var_dump( json_decode( '{"id":"30"}', true ) );
array(1) {
["id"]=>
string(2) "30"
}
Above examples are hosted at: http://ideone.com/GNMRlD
Since stdClass object itself does not provide iteration functionality, using it with foreach will yield errors.
You can convert stdClass into array with functions such as http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ .
For eg. if your array looks like below. foreach which I used will be working
$new_array = array(array('total'=>array('title'=>'test','text'=>'text')));
foreach ($new_array as $val)
{
print_r($val['total']['title']); // Use this for array
print_r($val->total->title); // Use this for object
}
if your array looks like below. Use the below foreach.
$new_array = array(array('total'=>array(array('title'=>'test','text'=>'text'),array('title'=>'test1','text'=>'text1'))));
foreach ($new_array as $val)
{
foreach($val['total'] as $new_val)
{
print_r($new_val['title']);
}
}
You can try this, I hope that helps you
foreach($booked as $new_var){
var_dump('</br>', $new_var);
var_dump('</br>',$new_var['30_booking']->title);
}
I think in your foreach there is mistake of word 30_booking
since in your main array out put display it shows 30_booked
check that also
I'm writing a PHP script to work with some JSON data. Below is an (abridged) var_dump($data). I want to return the value associated with ["[question(13), option(0)]"] which is 20. I can't figure out how to do it. I've tried $data->[question(13), option(0)] and $data->question(13). (I tried to look this up but I'm not sure what the notation means, so I'm not sure what I'm looking for)
object(stdClass)#133 (36) {
["id"]=>
string(1) "1"
["contact_id"]=>
string(0) ""
["status"]=>
string(8) "Complete"
["is_test_data"]=>
string(1) "0"
["datesubmitted"]=>
string(19) "2012-04-19 17:11:00"
["[question(5)]"]=>
string(11) "C. 40%, 40%"
["[question(9)]"]=>
string(47) "D. EBITDA and Free cash flow are the same thing"
["[question(10)]"]=>
string(48) "A. Accounts Payable as % of sales would increase"
["[question(11)]"]=>
string(20) "E. None of the above"
["[question(12)]"]=>
string(97) "A. A larger portion of initial investment is equity which can increase exit return potential."
["[question(13), option(0)]"]=>
string(2) "20"
["[url("embed")]"]=>
string(0) ""
["[variable("STANDARD_IP")]"]=>
string(13) "38.107.74.230"
["[variable("STANDARD_LONG")]"]=>
string(10) "-73.976303"
["[variable("STANDARD_LAT")]"]=>
string(9) "40.761902"
}
Either use extended object access notation:
$data->{'[question(13), option(0)]'}
Or just ask for normal array and use it as normal array.
json_decode($json, true);
try this
echo $data->{'[question(13), option(0)]'};