PHP returns a "notice" whenever an array index is undefined.
How can I turn these notices off? Or even better, correctly code for them?
Example:
$job_db_ready = array(
"email" => $this->profile['email'],
"company" => $job['company']['name'],
"position" => $job['title'],
"industry" => $job['company']['industry'],
"start_date_month" => $job['startDate']['month'],
"start_date_year" => $job['startDate']['year'],
"end_date_month" => $job['endDate']['month'], // Sometimes endDate undefined
"end_date_year" => $job['endDate']['year'], // Sometimes endDate undefined
"is_current" => $job['isCurrent']
);
This array will return
Severity: Notice
Message: Undefined index: endDate
1) Better Coding Style :Check if your variable is defined first with isset before assigning it to a key in the array. Like this
isset($job['endDate']['month']) // Do something about it
2) Turning of error: Use this in your script before doing something weird
error_reporting(0);
1 is recommended 2 is NOT
Turning of notices is a bad practice as they are there to alert you to possible errors on your part. Instead, check to see if they are defined and if they aren't assign a default value (including null or an empty string):
"end_date_month" => (isset($job['endDate']['month']) ? $job['endDate']['month'] : ''),
"end_date_year" => (isset($job['endDate']['year']) ? ($job['endDate']['year'] : ''),
Put the following at the top of your PHP script:
error_reporting( 0 );
See error_reporting on the PHP docs for further information.
Related
Writing:
define ("MYARR", array(
'TITLE' => "MY TITLE",
) );
And giving:
print_r(MYARR);
No error is returned. It is all ok! I display:
Array
(
[TITLE] => MY TITLE
)
But if i write:
define ("MYARR['TITLE']", "MY TITLE");
I don't get a error, but giving:
print_r(MYARR);
I get:
Warning: Use of undefined constant MYARR - assumed 'MYARR'
And giving:
echo MYARR['TITLE'];
I get this two warning:
1) Warning: Use of undefined constant MYARR
2) Warning: Illegal string offset 'TITLE'
About first warning, it not is correct, becouse as is possible to see above it is declared!
I think which is a bug of PHP. From a side is allowed declare constant of array but of other side not allowed. What is your opinion?
This is the print_r from the $form_data variable / array in php log.
[28-Sep-2018 18:04:03 UTC] Array
(
[cfdb7_status] => unread
[meno] => data
[email] => data
[telefon] => phone
[meno-ucastnika__1] => data
[email-ucastnika__1] => data
[meno-komory__1] =>
[registracne-cislo__1] =>
[_wpcf7_groups_count] => Array
(
[emails] => 1
)
[obchodne-meno] => obchod
[obchodne-sidlo] => fs
[ico] => 50426508
[dic] => dic
[icdph] => icdicko
)
How can I get the value of _wpcf7_groups_count key?
If I want email I simply wrote $form_data['email']. Everything goes like this except _wpcf7_groups_count.
$form_data['_wpcf7_groups_count']
$form_data['_wpcf7_groups_count'][0]['emails']
$form_data['_wpcf7_groups_count']['emails']
Anything from above doesn't work. The first is giving me an illegal offset.
From the data you posted(*),
$form_data['_wpcf7_groups_count']['emails']
should work, and yield 1.
Note that the parent key value is an array, so if the form data goes through some sort of templating engine, the problem might lie there.
I find it strange that you get an error for the first method, and not for the others: in PHP, if you can't reference an array key, you cannot reference any of the descendants, and you still get the error from the parent key. This is what makes me suspect that something else is afoot.
==========
(*) I assumed that you have deleted some information while keeping the formatting. Otherwise writing
[meno-komory__1] =>
[registracne-cislo__1] =>
could possibly be interpreted as a nested key. I saw no 'Array', so I assumed there was just some data missing. But next time write it explicitly to avoid any ambiguity:
[meno-komory__1] => "(redacted)",
[registracne-cislo__1] => "(array, redacted)",
This is my array:
Array
(
[0] => Array
(
[id] => 1387
[form_id] => 2
[date_created] => 2018-05-17 21:34:37
----> [67] => a:1:{i:0;a:7:{s:17:"wp_attachement_id";i:5828;s:9:"mime_type";s:9:"video/mp4";s:9:"file_path";s:89:"/home/xxx/public_html/wp-content/uploads/2018/05/703c94b2b97a6479113d9e3020952891.mp4";s:5:"title";s:3:"xxx";s:11:"description";s:3:"xxx";s:8:"video_id";s:6:"QRSTUV";s:9:"video_url";s:43:"https://www.youtube.com/watch?v=QRSTUV";}}
[36] => New Harpshire
[34] => Borristown
)
)
The following will print the video id:
$undid = unserialize($entry['0']['67']);
echo $undid['0']['video_id'];
The problem is the number '67' does not remain constant. How do I retrieve that video_id if I have no idea what '67' will be?
I whipped up a quick Demo for you.
Ideally, you'd find a way to set the key to a semantic name instead of '67'. But if you can't, and it's always different, then your best bet is to check for the expected value.
Since you're expecting a serialized array, we can check for serialized values by running them through the unserialize() function. Note: This will trigger E_NOTICE level errors, so you'll probably want to suppress those with #unserialize() (the # suppresses errors). While it's generally a bad idea to suppress errors, these are errors we're expecting to trigger, so they should be safe to ignore.
So, as we pass things through unserialize(), we can check if it sets $undid to a truthy value. Once it does, we need to make sure we have the correct serialized information, so we can see if $undid[0]['video_id'] is set, and if it is, echo that value (or whatever else you want to do with it), and then break the foreach loop, since it no longer needs to run once our conditions have been met.
It's not exactly the most beautiful solution, but unless you can figure out how to name that key something semantic like file_upload_array, your best bet is loop through the values until you find the one you need. As long as you don't have an unthinkable amount of fields, this should work fairly quickly.
$entry = [
[
'id' => 1387,
'form_id' => 2,
'date_created' => '2018-05-17 21:34:37',
'67' => 'a:1:{i:0;a:7:{s:17:"wp_attachement_id";i:5828;s:9:"mime_type";s:9:"video/mp4";s:9:"file_path";s:89:"/home/xxxxxxx/public_html/wp-content/uploads/2018/05/703c94b2b97a6479113d9e3020952891.mp4";s:5:"title";s:3:"xxx";s:11:"description";s:3:"xxx";s:8:"video_id";s:6:"QRSTUV";s:9:"video_url";s:43:"https://www.youtube.com/watch?v=QRSTUV22222";}}',
'36' => 'New Harpshire',
'34' => 'Borristown',
]
];
foreach( $entry[0] as $key => $val ){
if( $undid = #unserialize( $val ) ){ // unserialize returns E_NOTICE warnings on non-serialized items. # suppresses that.
if( isset( $undid[0]['video_id'] ) ){ // Make sure this is the correct serialized field
echo $undid[0]['video_id'];
break;
}
}
}
I'm trying to see if you can assign an array value during creation to a value in the same array.
If I'm declaring and initializing an array like this:
$this->array = array(...);
Can I do something like this?
$this->array = array
(
'value1' => 'hello',
'value2' => $this->array['value1'],
);
I've tried it already and I get back
Notice: Undefined index: value1 in /Sites/website/config.php on line 329
I've gotten around this problem already but now out of interest I want to see if theres actually a way to do this.
It has to be during creation and declared like above, not through
$array['value1'] = 'foo';
$array['value2'] = $array['value1'];
or
$common = 'foo';
$array = array('value1' => $common, 'value2' => $common);
Is this technically impossible or is there any way?
I don't think it's possible. The array has to be parsed before it can be assigned, so at the time $this->array['value1'] is being calculated, $this->array is still null.
Basically, $this->array['value1'] cannot be be accessed before $this->array['value2'] is set, which needs to access $this->array['value1'], which cannot be accessed before . . .
The reason you're getting Notice: Undefined index: value1 in /Sites/website/config.php on line 329 is because when you're defining your array the value you're trying to access has not yet been defined because you haven't reached the end of the initial function.
In the following code:
$this->array = array
( //this is the start of the array
'value1' => 'hello',
'value2' => $this->array['value1'],
); //this is the end of the array statement.
The array is not defined until you reach the end of the statement.
The reason the other methods work is because you're using multiple steps to access an already existing variable.
I think that is not possible, assign the element when the array starts
you can set as false and before the action that set the value as your code.
example :
$common = 'foo';
$array = array(
'value1' => $common,
'value2' => $common
);
or you can use the function array_combine
I suddenly stuck here:
$source = (object) array(
'field_phone' => array(
'und' => array(
'0' => array(
'value' => '000-555-55-55',
),
),
),
);
dsm($source);
$source_field = "field_phone['und'][0]['value']";
dsm($source->{$source_field}); //This notation doesn't work
dsm($source->field_phone['und'][0]['value']); //This does
dsm() is Drupal developer function for debug printing variables, objects and arrays.
Why $source object doesn't understand $obj->{$variable} notation?
Notice: Undefined property: stdClass::$field_phone['und']['0']['value']
Because your object does not have a property that is named "field_phone['und'][0]['value']". It has a property that is named "field_phone" which is an array which has an index named "und" which is an array which has an index 0 and so on. But the notation $obj->{$var} does not parse and recursively resolve the name, as it shouldn't. It just looks for the property of the given name on the given object, nothing more. It's not like copy and pasting source code in place of $var there.