This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 4 years ago.
I have a problem showing the field of the array 'notes' since it does not print the numbers generated by the 'addNotes' function.
The code loads the preloaded data and assigns random numbers with the 'addNotes' function to the 'notes' fields of the array and finally shows the data of the array in showStudentsList ().
<?php session_start();?>
<html>
<body>
<?php
if(!existDataInSession()){
initializePreloadedData();
}
function existDataInSession(){
return $_SESSION['data'] != NULL;
}
function initializePreloadedData(){
$person1= [
'name' => 'person1',
'notes' => []
];
$person2= [
'name' => 'person2',
'notes' => []
];
$data=[$person1,$person2];
$_SESSION['data'] = $data;
}
function addNotes(){
foreach ($data as $key => $value) {
$data[$key]['notes'] = random_int(0,100);
}
}
addNotes();
showStudentsList();
function showStudentsList(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo $student['name'] . " ";
echo $student['notes'];
echo implode($student['notes']);
echo "<br>";
}
}
//Result
//person1 Array
//person2 Array
?>
</body>
</html>
There are several problems in your code.
You change between array and integer notes. The plural implies an array.
You write to a copy of $_SESSION
$_SESSION['data'] != null throws a notice when not defined
Fixed version using notes array:
if(!existDataInSession()){
initializePreloadedData();
}
addNotes();
showStudentsList();
function existDataInSession(){
// we check if data was set. just to make double sure we check for type array as well
return isset($_SESSION['data']) && is_array($_SESSION['data']);
}
function initializePreloadedData(){
$person1= [
'name' => 'person1',
'notes' => []
];
$person2= [
'name' => 'person2',
'notes' => []
];
$_SESSION['data'] = [ $person1, $person2 ];
}
function addNotes(){
// operate on $_SESSION referencing the value since we want to ALTER the session data
foreach ($_SESSION['data'] as &$value) {
// since notes was defined as an ARRAY, we append here
$value['notes'][] = random_int(0,100);
}
}
function showStudentsList(){
$data = $_SESSION['data'];
foreach ( $data as $student ) {
echo $student['name'] . " ";
echo implode(', ', $student['notes']);
echo "<br>", PHP_EOL;
}
}
If I am not wrong to understand your requirement, $data is not defined in addNotes() function:
function addNotes(){
$data = $_SESSION['data']; // fetch data from session
foreach ($data as $key => $value) {
$data[$key]['notes'] = random_int(0,100);
}
$_SESSION['data'] = $data; // assign into session again
}
now use just echo without implode to get notes like echo $student['notes']
Hope this help.
Related
Am creating a depenednt dropdown in yii1 but i always get an error of htmlspecialchars() expects parameter 1 to be string, object given
This is the controller action code
public function actionDistrictList() {
$id = (int)$_POST['province'];
$data = Tblsudistricts::model()->findAll('province_id=1');
Yii::app()->session['districtlist'] = $data; //save created list to session
echo CHtml::tag('option', array('value' => ''), CHtml::encode('[select one]'), true);
foreach ($data as $value => $name){
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
//echo CHtml::tag('option', array('value' => $value), CHtml::encode($name), true);
}}
What could be wrong
Here, In your code
foreach ($data as $value => $name){
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
$value is a index and $name is a object, you have to use its parameters
For eg:
Chtml::encode($name->district_name)
Instaed of:
CHtml::encode($name)
Abhishek's answer is correct. However, you also have an issue with value. $value is the index of the object in the $data array not the id.
It looks like you are expecting a value => name kind of array for $data. If this is the case you should pass $data through the CHtml::listData function.
You can also avoid the foreach loop by using CHtml::listOptions
$data = Tblsudistricts::model()->findAll('province_id=1');
$data = CHtml::listData($data, "id", "name");
Yii::app()->session['districtlist'] = $data;
echo CHtml::listOptions(null, $data, array('prompt' => '[select one]'));
This question already has answers here:
php json_encode not working on arrays partially
(2 answers)
Closed 7 years ago.
I am trying to properly create and encode and array using json_encode php function. The array i am trying to encode is $myarray . From my code if do
$myarray = array(array('name' =>$value['display_name']->scalarval(),'id' => $value['id']->scalarval())) ;
then
echo json_encode($myarray) ; // this works but only one item is pushed to my array
if i do
$myarray[] = array(array('name' =>$value['display_name']->scalarval(),'id' => $value['id']->scalarval //pushing all elements to array
result is nothing.
what i am missing ?
see full code below on what i have so far done.
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
/*
* Retrieve available Room types.
* TODO
* make accessing ids automatic..
*/
include_once("../../openerp_models.php"); // include file to connect with openerp
date_default_timezone_set('Europe/Moscow'); // Timezone settings
//openerp connection details
require_once("../../connection.php") ;
try {
//we access partner model and domain for customers only
$customer = $connection_model->search('res.partner', 'customer', '=', TRUE);
//
//create an array
$ids = array();
//create a for loop and loop through the ids from search
for($i = 0; $i <= count($customer); $i++ )
{
// assign array values
$ids [] = $customer[$i] ;
}
// read partner with $ids
$customer_details = $connection_model->read('res.partner', $ids);
//loop through the scalavar value
$myarray = null;
// loop through the value returned
foreach ($customer_details as $keys => $values)
{
$value = $values->scalarval();
//Push values to my array
$myarray [] = array(array('name' =>$value['display_name']->scalarval(),'id' => $value['id']->scalarval())) ;
//
}
//Then try to encode $myrray but this fails
$jsonstring = json_encode($myarray);
if ($jsonstring!==false)
{
echo $jsonstring;
} else {
echo 'Could not properly encode $myarray';
}
///////////////////////
/////////////////////////
}
catch(Exception $ex){
print "Error ".$ex.getMessage() ;
}
?>
please help. thank you.
The right way to create the array would be like this:
$myarray = array(
array(
'name' => 'bla',
'id' => 1
), array(
'name' => 'blas',
'id' => 2
)
);
This part of your code is perfectly fine.
$data = array() ; //create new empty array
//loop through the array
foreach($myarray as $keys => $h)
{
$data [] = $h;
}
//encode
echo json_encode($data) ; //this fails silently
If you run the code, it works perfectly fine:
[{"name":"bla","id":1},{"name":"blas","id":2}]
Your foreach() loop creates a new array $data with the same entries (also arrays) as the $myarray contains. So, you could directly encode $myarray like this:
<?php
$myarray = array(
array('name' =>' Agrolait', 'id' => 6 ),
array('name' => 'Agrolait, Michel Fletcher', 'id' => 31 ),
array('name' => 'Agrolait, Thomas Passot', 'id' => 30 )
);
$jsonstring = json_encode($myarray);
if ($jsonstring!==false) {
echo $jsonstring;
} else {
echo 'Could not properly encode $myarray';
}
?>
Which produces this JSON string:
[{"name":" Agrolait","id":6},{"name":"Agrolait, Michel Fletcher","id":31},{"name":"Agrolait, Thomas Passot","id":30}]
json_encodereturns the value FALSE if something went wrong, and the encoded string else. If you check the result from it you at least know when it fails.
Thanks for your suggestion have solved this. My string data was not properly encoded using utf-8 as suggested by http://nl3.php.net/manual/en/function.json-encode.php. Check my answer below
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
/*
* Retrieve available Room types.
* TODO
* make accessing ids automatic..
*/
include_once("../../openerp_models.php"); // include file to connect with openerp
date_default_timezone_set('Europe/Moscow'); // Timezone settings
//openerp connection details
require_once("../../connection.php") ;
try {
//we access partner model and domain for customers only
$customer = $connection_model->search('res.partner', 'customer', '=', TRUE);
//
//create an array
$ids = array();
//create a for loop and loop through the ids from search
for($i = 0; $i <= count($customer); $i++ )
{
// assign array values
$ids [] = $customer[$i] ;
}
// read partner with $ids
$customer_details = $connection_model->read('res.partner', $ids);
//loop through the scalavar value
$myarray = null;
// loop through the value returned
foreach ($customer_details as $keys => $values)
{
$value = $values->scalarval();
$myarray [] = array('name' =>utf8_encode($value['display_name']->scalarval()),'id' => utf8_encode($value['id']->scalarval())) ;
//
//array_push($better, $myarray) ;
}
//echo '<pre>';
//print_r($myarray) ;
//echo '</pre>';
echo json_encode($myarray);
exit;
}
catch(Exception $ex){
print "Error ".$ex.getMessage() ;
}
?>
I have an array that I would like to pass to a function as a parameter. These array values will be used to pull values out of another array and display them.
My Function:
function showTreadmills($listbrands) {
global $treadmills;
foreach( $treadmills as $brand=>&$features ) {
if ($brand == $listbrands) {
return '<p>'.$features["description"].'</p>';
}
}
}
Treadmills Array:
$treadmills = [
'bowflexseries3' => [
'description' => 'Bowflex Series 3',
'image' => '/images/bowflex-series-3-150x150.jpg',
'url' => '/treadmills/bowflex/series-3',
],
'solef85' => [
'description' => 'Sole F85',
'image' => '/images/sole-f85-150x150.jpg',
'url' => '/treadmills/sole/f-85',
],
'endurancet10hrc' => [
'description' => 'Endurance T10HRC',
'image' => '/images/endurance-t10hrc-150x150.jpg',
'url' => '/treadmills/endurance/t10hrc',
]
];
Values that I'm trying to pull out of array in my function:
<?php echo showTreadmills('bowflexseries3','solef85'); ?>
This only returns the first Description from the array, which is Bowflex Series 3. I'm trying to figure out how to get it to pull the description for bowflexseries3 and solef85. I'm sure it's a dumb oversight. Thanks in advance!
You're not passing an array to the function, you're passing two strings. You need to call array() to wrap an array around them:
echo showTreadmills(array('bowflexseries3','solef85'));
Then you need to change showTreadmills. You can't use == to compare a string to an array. It looks like you want to test whether the string is in the array, so it should be:
if (in_array($brand, $listbrands))
Or instead of looping through $treadmills and testing whether it's equal to one of $listbands, you could loop through $listbrands:
$result = '';
foreach ($listbrands as $brand) {
if (isset($treadmills[$brand])) {
$result .= '<p>'.$treadmills[$brand]["description"].'</p>';
}
}
return $result;
This is better, since it loops through the smaller array. And in_array() has to do a search, while accessing an associative array is just a hash lookup.
Notice that you need to concatenate the results into a string during the loop. If you use return in the loop, you'll only return the first brand found.
The problem is that you are returning on your first match:
if ($brand == $listbrands) {
return '<p>'.$features["description"].'</p>';
}
You will need to store all your matches so the entire loop can finish and then send back everything that matched.
$matches = '';
foreach( $treadmills as $brand=>&$features ) {
if ($brand == $listbrands) {
$matches .= '<p>'.$features["description"].'</p>';
}
}
return $matches;
function showTreadmills($listbrands) {
global $treadmills;
$out = '';
foreach( $treadmills as $brand=>&$features ) {
if ($brand == $listbrands) {
$out .= '<p>'.$features["description"].'</p>';
}
}
return $out;
}
I have a simple Template engine and I have a problem giving it a data from assoc array. Can anybody give me an advice? In method getStatisticData, I give an assoc array as first variable $data. My input array is in the form:
[0] => Array
(
[OrderNumber] => 1
[Name] => Zahid
[Total revenue] => 8363.38
)
I'm trying to get data from it using foreach but it doesn't work. I'm getting notice
Notice: Array to string conversion in C:\Server\htdocs\Task\lib\TemplateGen.php on line 34
protected function getStatisticData($data, $template){
$text = "";
if($data === false) {
return "We don't have any data in database";
}
foreach($data as $key => $value){
$data[$key] = $value;
$text .= $this->template_gen->getReplaceTemplate($data ,$template);
}
return $text;
}
getReplaceContent and associated methods from TemplateGen.php:
private function getReplaceContent($dataString, $content)
{
$search = array();
$replace = array();
$i = 0;
foreach ($dataString as $key => $value) {
$search[$i] = "%$key%";
$replace[$i] = $value;
$i++;
}
return str_replace($search, $replace, $content); ## LINE 34
}
function getReplaceTemplate($dataString, $template)
{
return $this->getReplaceContent($dataString, $this->getTemplate($template));
}
function getTemplate($name)
{
return $content = file_get_contents($this->config->tpl_path . $name . ".tpl");
}
UPDATE:
I got 2 new errors
Warning: implode(): Invalid arguments passed in C:\Server\htdocs\Task\lib\TemplateGen.php on line 28
Fatal error: Cannot redeclare add_percent() (previously declared in C:\Server\htdocs\Task\lib\TemplateGen.php:25) in C:\Server\htdocs\Task\lib\TemplateGen.php on line 25
Line 25:
function add_percent($i) {
Line 28:
return implode("", str_replace(array_map("add_percent", array_keys($dataString)), array_values($dataString),$content));
UPDATE2:
In theory, everything provided in new method should work very well. But there are same problems which were at the beginning
Notice: Array to string conversion in C:\Server\htdocs\Task\lib\TemplateGen.php on line 44`
line 44 :
return str_replace(
array_map($addPercent, array_keys($data)), array_values($data), $template
);
But if I'am using my getStatisticData instead of yours, it works but there are many other errors
My method :
protected function getStatisticData($data, $template){
$text = array();
if($data === false) {
return "We don't have any data in database";
}
$i=0;
foreach($data as $dataString){
if (!empty($data[$i+1])){
foreach($dataString as $key => $value){
$dataString[$i][$key] = $dataString[$i][$value];
}}
$text .= $this->template_gen->getReplaceTemplate($dataString ,$template);
}
return $text;
}
Your code is rather inefficient at present, and it's leading to some isuues. Let's look at the first method you posted:
protected function getStatisticData($data, $template){
$text = "";
if($data === false) {
return "We don't have any data in database";
}
foreach($data as $key => $value){
$data[$key] = $value;
$text .= $this->template_gen->getReplaceTemplate($data ,$template);
}
return $text;
}
The foreach loop is doing the same thing for each key/value pair (plus $data[$key] = $value is redundant as you are already getting each key and value in the foreach loop), so you could eliminate the loop, and replace it with something like this:
protected function getStatisticData($data, $template){
if ($data === false) {
return "We don't have any data in database";
}
return $this->template_gen->getReplaceTemplate($data, $template);
}
Similarly for getReplaceContent - you're basically using the array keys and array values as the search and the replacement values. You can use PHP's handy array_keys and array_values instead of building new arrays. The catch is that the array keys need to be surrounded by %, but that is easy to do using array_map - just define a function that will take in a string and add % to either end of it, and then array_map it to your array keys:
private function getReplaceContent($data, $template)
{
$addPercent = function( $i ){
return "%$i%";
};
return str_replace(
array_map( $addPercent, array_keys($data)), array_values($data), $template
);
}
Now, calling getStatisticData will return the template text with all the replaced data in it.
Sample input:
$arr = array(
'OrderNumber' => 1,
'Name' => 'Zahid',
'Total revenue' => '8363.38'
);
$template =
'<p>Name: %Name%<br>
Total revenue: %Total revenue%<br>
Order number: %OrderNumber%</p>';
Output of getStatisticData:
<p>Name: Zahid<br>
Total revenue: 8363.38<br>
Order number: 1</p>
EDIT: it is not clear from the OP what the input to getStatisticData is, but it looks like it is supposed to be an array of associative arrays. If this is so, the code for getStatisticData should be altered as follows:
protected function getStatisticData($dataArray, $template){
if ($dataArray === false) {
return "We don't have any data in database";
}
$text = "";
foreach ($dataArray as $aa) {
$text .= $this->template_gen->getReplaceTemplate($aa, $template);
}
return $text;
}
Sample input:
$arr =
[
[ 'OrderNumber' => 1,
'Name' => 'Zahid',
'Total revenue' => '8363.38'
],
[ 'OrderNumber' => 2,
'Name' => 'Paul',
'Total revenue' => '123.45'
],
[ 'OrderNumber' => 3,
'Name' => 'Jane',
'Total revenue' => '567.89'
],
];
Output:
<p>Name: Zahid<br>
Total revenue: 8363.38<br>
Order number: 1</p>
<p>Name: Paul<br>
Total revenue: 123.45<br>
Order number: 2</p>
<p>Name: Jane<br>
Total revenue: 567.89<br>
Order number: 3</p>
I'm assuming that the associative array $data has simply strings in it. I'm assuming that the $data array has its keys as the template placeholder and the value as the value to input.
If so, and if you're trying to get a single string, then consider doing this:
protected function getStatisticData($data, $template) {
if ($data === false) {
return "We don't have any data in the database.";
}
return this->template_gen->getReplaceTemplate($data, $template);
}
private function getReplaceContent($dataString, $content) {
content = '';
foreach ($dataString as $key => $value) {
$content .= str_replace("%{$key}%", $value, $content);
}
return $content;
}
You don't need quite as many foreach loops. One should do just fine.
Let's say I have this array,
$array = array(
'name' => 'hermet',
'emails' => array ('hermet#example.com',
'hermet#example.net');
);
So this way echo $array ['name'] == 'hermet' prints true. I would like to know if there is a function already embedded in PHP that let me do this:
echo $name == 'hermet'; // obviously 'false'
foreach ($array as $key => $value) {
$aux = $key;
$$aux = $value;
}
echo $name == 'hermet'; // now prints 'true'
It seems to work even with a multidimensional array but I don't know if PHP has already any function to do that.
Thank you in advance.
You might be looking for extract
$array = array(
'name' => 'hermet',
'emails' => array ('hermet#example.com',
'hermet#example.net')
);
extract($array);
var_dump($emails);
echo $name;
-- EDIT: If you are concerned about Paul's remark, supply EXTR_SKIP to the second argument of extract, that way it won't overwrite variable in case you've already defined it prior to calling extract.
$name = 'jason';
extract($array, EXTR_SKIP);
echo $name; // still 'jason'