This question already has answers here:
Unsetting array values in a foreach loop [duplicate]
(9 answers)
Closed 9 years ago.
I think the code is obvious:
foreach ($programs as $program) {
if ($program->name == 'foo') {
unset($program);
}
}
But it's not working! Isn't it possible to unset current property? Where's the problem? Is there any alternatives?
foreach ($programs as $property => $program) {
// ^-----------^ added
if ($program->name == 'foo') {
unset($programs->$property);
// ^---------^ added
}
}
Related
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)
php: cannot modify array in function?
(2 answers)
Closed 1 year ago.
I want to modify the array in the first function using the second function but it doesn't return the result where it should(first Function).
function first(){
$results = [
'programs' => [],
'events' => [],
];
second('program', $results);
print_r($results); // Don't get results here
}
function second($type,$arr){
$type .= 's';
if(array_key_exists($type,$arr)){
array_push($arr[$type],'title');
}
print_r($arr); // Get results here
}
first();
This question already has answers here:
Variable scope difference between PHP and C: block scope is not exactly the same?
(1 answer)
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 1 year ago.
I'm learning PHP and I encounter this piece of code:
$args = [
"msg" => "hello world!"
];
foreach ($args as $key => $value) {
$$key = $value;
}
echo $msg; // prints hello world!
Why can I acces $msg if it was created inside the for loop? Shouldn't it be out of context?
This question already has answers here:
How to Check for a Specific Type of Object in PHP
(4 answers)
Closed 5 years ago.
how do I know what kind of class is a variable in php:
if($var == \Collection)
echo "congratulations";
?>
Try instanceof:
if($var instanceof \Collection) {
You can use get_class($object):
Returns the name of the class of an object
or use instanceof in if:
if ($object instanceof Collection) {
...
}
This question already has answers here:
php is null when empty? [duplicate]
(10 answers)
Closed 5 years ago.
The following code gives TRUE,FALSE,FALSE,FALSE,
I dont understand the TRUE response on empty arrays.
Someone has an explanation?
$results=array();
// Case 1 : Empty array
$myArray=array();
array_push($results, ($myArray==null));
array_push($results, ($myArray===null));
// Case 2 : Non Empty array
$myArray=array(1);
array_push($results,($myArray==null));
array_push($results,($myArray===null));
//
foreach ($results as $result) {
if ($result) echo("TRUE,"); else echo ("FALSE,");
}
Response here : PHP treats NULL, false, 0, and the empty string as equal, see stackoverflow here php is null or empty?
... and empty arrays
Need to be very careful so
This question already has answers here:
Creating default object from empty value in PHP?
(18 answers)
Closed 8 years ago.
I am getting an warning message in my Wordpress :
"Creating default object from empty value in
/home/forthemde/public_html/wp-content/themes/mayadeep/admin/admin.php
on line 225"
Here the code on admin.php and:
function upfw_setup_theme_options(){
$up_options_db = get_option('up_themes_'.UPTHEMES_SHORT_NAME);
global $up_options;
//Check if options are stored properly
if( isset($up_options_db) && is_array($up_options_db) ):
//Check array to an object
foreach ($up_options_db as $k => $v) {
$up_options -> {$k} = $v;
}
else:
do_action('upfw_theme_activation');
endif;
}
add_action('upfw_theme_init','upfw_setup_theme_options',10);
add_action('upfw_admin_init','upfw_setup_theme_options',100);
Line 225 is here:
$up_options -> {$k} = $v;
I try to fix by myself, but no result. Please help me, really appreciate for any help.
Regards.
I had this problem too, recently, but this worked for me:
Just added $up_options = new stdClass(); after global $up_options;
Best regards,
http://shubhinfotech.com http://c-worx.com