How can i read this data i'm getting on my page via post?
This are the variables as they appear on firebug under POST.
list[0].firstName =test 1
list[0].name =test
list[1].name =test
I know that php replaces dots with underscores so i can access something like "address.box" by doing:
$_POST[address_box];
but i really can't figure out how to access all the above data.
Seems like that php doesn't want to read whatever comes after the square brackets, overwriting all the fields with the same index (list[0].firstName seems to get overwritten by list[0].name .
Any solution?
EIDT:
Here a var_dump of the data:
address.number 1
list[0].firstName firstname0
list[0].name name0
list[1].name name1
array(2) { ["list"]=> array(2) { [0]=> string(10) "firstname0" [1]=> string(5) "name1" } ["address_number"]=> string(1) "1" }
No. This is not possible within the PHP-language.
You must use either $object->field or $array['key'] notation.
Related
I'm having a difficult time understanding how to print out an attribute value of an object. The particular example I am working from is this:
object(SimpleXMLElement)#1 (1) {
["links"]=>
object(SimpleXMLElement)#4 (2) {
["#attributes"]=>
array(3) {
["total-matched"]=>
string(2) "31"
["records-returned"]=>
string(2) "10"
["page-number"]=>
string(1) "3"
}
I want to print the value of the links total-matched (which is 31). I've tried this: echo $object->links->total-matched; but I only get the value of 0.
How can I do this?
$object->links->total-matched evaluates as $object->link->total - matched (- is minus, I suppose you should see warning about using unknown constant - turn on error reporting). To access attributes with names like this you can do following: $object->links->{'total-matched'} although in this case, since it's SimpleXML attribute, I think you need to get attributes array:
$attr = $object->links->attributes();
echo $attr['total-matched'];
I have a problem, I fetched all the rows from DB using PDO::FETCH_OBJ.
The response is completely okay. But I would like to loop through it and add an element with name 'photo' to each object in the array. This photo will include a small image converted to string.
foreach ($rows as $row) {
$row->photo = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/xxx/uploads/'.$row->id.'/'.$row->filepath.'_thumbnail.jpg');
}
Pictures are being found without any problems, and even after I echo their content after I add it to $row, I get their contents without any problem too. But when I return the $rows variable, and I try to receive it in Android, the photo element is never there. What could be the problem?
This is how i return:
return base64_encode(json_encode($rows));
Example of output:
object(stdClass)#3 (10) {
["position"]=> string(1) "1"
["id"]=> string(2) "24"
["fbname"]=> string(12) "Vl Mar"
["filepath"]=> string(13) "1481809734438"
["photo"]=> string(5760) "������JFIF���� .....and many more characters
}
Formatting at the end of output is not important, as its not Base64 decoded.
I have a PHP array, that I want to pass over to jQuery and update the rows on the page.
This PHP array is the 'name' of the checkboxes selected on the page. (So this array can be of any length, depending on what the user selects)
PHP Array:
var_dump($sr->conflict_return);
OUTPUT CHECK: array(5) { [0]=> string(33) "hours_9_7_reg_session_102_905_925" [1]=> string(33) "hours_9_7_reg_session_101_905_925" [2]=> string(33) "hours_9_7_reg_session_103_905_925" [4]=> string(33) "hours_9_7_reg_session_104_845_915" [13]=> string(33) "hours_9_7_reg_session_103_845_905" }
This this case... there are '5' elements in my [php] array.
Here is where my problem comes into play...
Sometimes it 'works'... sometimes it doesnt..
The 'key' seems to be what is IN the array:
OUTPUT CHECK: array(3) { [0]=> string(33) "hours_9_7_reg_session_102_845_905" [1]=> string(33) "hours_9_7_reg_session_101_845_905" [2]=> string(33) "hours_9_7_reg_session_104_845_915" }
this seems to work.. 3 items in array.. all 3 rows on stage get highlighted.
this:
OUTPUT CHECK: array(4) { [0]=> string(33) "hours_9_7_reg_session_102_845_905" [1]=> string(33) "hours_9_7_reg_session_101_845_905" [2]=> string(33) "hours_9_7_reg_session_103_845_905" [4]=> string(33) "hours_9_7_reg_session_104_845_915" }
doesnt work... and none of the rows are highlighted
(seems like if there is 4 items in the array it breaks??)
my jQuery to parse the data:
var conflictItems = <?=json_encode($sr->conflict_return); ?>;
//has a conflict list
if(conflictItems.length > 0){
alert("Has conflicts");
//loop through and highlight elements on stage
for(i=0; i<conflictItems.length; i++){
console.log(conflictItems[i]);
$("#sr_table_"+conflictItems[i]+"_row").addClass("conflict_border");
}
}
When I trace (console.log()) the data.... I get odd results.
console.log('CONFLICT ITEMS: ' + conflictItems);
console.log('CONFLICT COUNT: ' + conflictItems.length);
3 x items in array... the above shows:
CONFLICT ITEMS: hours_9_7_reg_session_103_845_905,hours_9_7_reg_session_102_845_905,hours_9_7_reg_session_104_845_915
CONFLICT COUNT: 3
which to me is correct. I have 3 items in my array passed over from PHP..jQuery runs through list and adds a class to each 'row'.
however, when I add a 4th item... the traced output is:
CONFLICT ITEMS: [object Object]
CONFLICT COUNT: undefined
So how? is my array turning into an object?.. and more so WHY??
and how can I fix this? I dont understand why having 3 items in the array works.. but not 4?
Javascript doesn't have a concept of non-sequential array keys (your example has keys 0,1,2 and 4), thus when running json_encode on the array, it converts it to JSON notation for a JS object.
As #Kenney says in the comments, a possible solution is to keep using arrays, but make use of array_values() function which takes your array, and basically regenerates the array dropping the existing keys and using sequential ones.
You're missing index 3 from your array, therefore js turns it into an object.
I have an object being passed into a function which I have no control over and it is in the below format, with the root being 'entity'.
object(SimpleXMLElement)#6 (1) { ["#attributes"]=> array(2) { ["id"]=> string(2) "12" ["name"]=> string(17) "Test Object Value" } }
Now I'm trying to pull out just the name by using both the below snippets but both output empty values.
entity[0]->name;
and
entity->{'#attributes'}->name;
Is there a special way to deal with characters in element names when the curly brackets format doesn't work?
You need to use attribute() function for getting the attributes in a simpleXML object. Your code should be something like:
$parsed = $simplexmlObject->entity->attribute()->desiredProperty;
Update: Got this technique from a question asked from me, How to parse value `#attribute` from a SimpleXMLObject in PHP
You can get the name attribute as follows:
$name = $entity->attributes()->name;
echo $name;
The PHP code I am interating through is as follows for the Update process:
$data = $_POST;
foreach ($data['answers'] as &$d):
if(!isset($d['default'])):
$d['default'] = "false";
endif;
endforeach;
And when I var_dump it after that iteration, I get the following:
array(2) {
["question"]=>
string(20) "Which did you like?"
["answers"]=>
array(6) {
[0]=>
array(2) {
["default"]=>
string(4) "true"
["option"]=>
string(5) "First"
}
[1]=>
&array(2) {
["option"]=>
string(5) "Second"
["default"]=>
string(5) "false"
}
}
}
As you can see, the second array has "&array" keyword, I am assuming that's implying a reference. My question is, can I serialize this array and save it into MYSQL DB? I was getting some data error on the display page after, so I want to make sure if this has anything to do with this.
UPDATE
Error message I get on the display page is that
Undefined index: option
From the docs:
serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost.
About yout error message, post the line youre calling the "option" index, so we can see what is going wrong...
And as Sammitch said, you can use serialize() to store this data in your DB.