Cannot use string offset as an array problem caused by not using curly brace brackets around variable

By Benjamin Melançon
on 09 Aug
0 comments

Key words and phrases

string offset curly brackets braces

Tags

Description

Why this weirdness?

cannot use string offset as array
Fatal error: Cannot use string offset as an array in /sites/jonassoftware/www/sites/all/modules/field_placement/field_placement.module on line 706

<?php
function field_placement_node_save(&$node) {
 
$nid = $node->nid;
 
$type = $node->type;
 
$type_definition = content_types($type);
 
$cck_fields = array_keys($type_definition['fields']);
 
$delta = 0// used in hack below, if hack survives will replace with foreach
 
foreach($cck_fields as $field_name) {
// hacky solution
   
if ($field_values = field_placement_hack_pass_field_values()) {
dsm('patyblack');
dsm($node->$field_name);
dsm($field_values);
dsm($field_values[$field_name]);
//      if (isset($node->$field_name[$delta]['field_placement'])) {
//        $node->$field_name[$delta]['field_placement'] = $field_values[$field_name]['field_placement'];
//      }
     
if (isset($node->$field_name[$delta])) {
//        $node->$field_name[$delta]['field_placement_imagecache'] =
       
$test = $field_values[$field_name]; // ['field_placement_imagecache'];
     
}
    }
// end hacky solution
?>

#

patyblack

#

Array
(
    [0] => Array
        (
            [flags] => Array
                (
                    [delete] => 0
                )

            [alt] => 
            [title] => drupalgeonames_crop.jpg
            [filename] => drupalgeonames_crop.jpg
            [filepath] => sites/default/files/product_images/drupalgeonames_crop_8.jpg
            [filemime] => image/jpeg
            [filesize] => 85652
            [fid] => 148
            [field_placement] => 
            [field_placement_imagecache] => 
        )

)

#

Array
(
    [field_placement] => 2
    [field_placement_imagecache] => 5
)

Problem identified:

<?php
dsm
('failure: ');
dsm($node->{$field_name}[$delta]);
?>

second line there printed blank-- no array.

new version with brackets:

<?php
function field_placement_node_save(&$node) {
 
$nid = $node->nid;
 
$type = $node->type;
 
$type_definition = content_types($type);
 
$cck_fields = array_keys($type_definition['fields']);
 
$delta = 0// used in hack below, if hack survives will replace with foreach
 
foreach($cck_fields as $field_name) {
// hacky solution
   
if ($field_values = field_placement_hack_pass_field_values()) {
      if (isset(
$node->{$field_name}[$delta]['field_placement'])) {
       
$node->{$field_name}[$delta]['field_placement'] = $field_values[$field_name]['field_placement'];
      }
      if (isset(
$node->{$field_name}[$delta]['field_placement_imagecache'])) {
       
$node->{$field_name}[$delta]['field_placement_imagecache'] = $field_values[$field_name]['field_placement_imagecache'];
      }
    }
// ...
 
}
// ...
}
?>

 

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.