With
$maids = getMaids();
I get $maids to be an array of PHP objects each one with the same structure. Is there any way I can sprintf the values inside of these objects with just two lines of code?
foreach($maids as &$maid)
sprintf($form, $maid->name, $maid->pbx, $maid->id);
Why is the code above outputting nothing? Not even an error in the console.
I want to keep my code as clean as possible and using 3 lines just to assign the contents of the object to a variable and then echo() only that variable is too long. Why my shortcut is not working?
Thanks!
sprintf doesn't output anything, it returns a string that you can then echo to output it; or try printf() instead
Use printf() instead of sprintf().
printf() will output it's results to stdout while sprintf() will return the results as a string what is useful in some cases too
Related
When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.
There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.
This is so strange, i tried a bunch of combinations for the quotes but with no positive results, i just want to put the children_of_row variable to be inside a string, but it's not working as i expect, this is strange but since i know that it is me that is wrong and not php i want you guys to tell me where is my "mistake" ? (anything that helps me understand nesting php strings is appreciated).
$children_of_array is an array, $children_of_row["question_name"] is a string when i use php's gettype function, i push the value into $children_of_array, and then inside a php echo statement i implode the array (can i run php functions inside the echo statement ?), but even if i don't the results are the same, when outputted into javascript the type is not string anymore.
array_push($children_of_array,'".$children_of_row["question_name"]."');
If you want the string of $children_of_row["question_name"], you just need to put that:
array_push($children_of_array, $children_of_row["question_name"]);
As long as the variable is already a string, or a value that is allowed in an array (including arrays), it'll work.
So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.
When I print an object using print_r, is there any specific utility function in php to convert the output to an object?
There seems to be enough output to reconstruct the object, but I couldn't find the exact functions to accomplish this.
There is no automatic method to change print_r value back to an object. You are also missing important information like what type is stored at a key (string or number or ..)
You could use var_dump to get more detailed output that can be changed back to an object. But still this does not mean there is an automatic function for it.
Last you could use var_export ( http://php.net/manual/en/function.var-export.php ) to get valid PHP output you can use.
You could also use http://php.net/manual/en/function.serialize.php to pass objects around.
Hi this is the code i am following in my project.
$reports = $this->curl->simple_get('url');
echo is_array($reports)?"Is Array":"Not Array";exit;
It's giving Not Array as output.
I want to convert that into associative array.
The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.
To use this you can use a similar solution as brought out here:
Create variable from print_r output
it describes the print_r_reverse function that's brought out in php.net page.
how ever - this is kind of an ugly hack. I would suggest to change the page content and use json_encode() in the "url" page, and parse the content using json_decode()