Daher verstehe ich nicht, warum das nicht funktioniert. Ich verwende WordPress und ein Plugin namens ACF, um einige Daten zu füllen. Ich durchlaufe mein Repeater-Feld, um die Daten so auszuspucken:
<?php
$links = get_field('footer_links'); // spits out the array
if($links) {
foreach ( $links as $link ) {
$logo = $link['logo'];
$link = $link['link'];
$text = $link['text'];
echo '<div class="link">';
echo ' <a href="'.$link.'"><img src="'.$logo.'" /><p>'.$text.'</p></a>';
echo '</div>';
}
}
// Logo spits out a image path
// link spits out the URL path
// text SHOULD just spit out the title, however throws PHP warning
?>
Für die Variable $text
Ich erhalte eine PHP-Warnung.
Warnung: Unzulässiger String-Offset ‘text’ in…
Warum sind meine anderen Variablen — $logo
, $link
werfen Sie diese Warnung nicht? Sie werden auf die gleiche Weise wie die anderen erstellt $text
Variable.
Ich habe versucht, das gesamte Repeater-Feld neu zu erstellen und den Namen usw. zu ändern, ohne Erfolg.
Hier ist mein print_r($links);
Array
(
[0] Array
(
[logo] http://domainname.com/imagepath
https://stackoverflow.com/questions/22099529/php-warning-illegal-string-offset http://.....
[text] Text1
)
[1] Array
(
[logo] http://domainname.com/imagepath
https://stackoverflow.com/questions/22099529/php-warning-illegal-string-offset http://.....
[text] Text2
)
[2] Array
(
[logo] http://domainname.com/imagepath
https://stackoverflow.com/questions/22099529/php-warning-illegal-string-offset http://.....
[text] Text3
)
)
Lahmer Hack:
if(!isset($link['text'])) { $link['text'] = ''; }
– Scott Arciszewski
28. Februar 2014 um 15:43 Uhr
Also was macht
print_r($links)
oderprint_r($link)
innerhalb der Schleife geben Sie?– sjagr
28. Februar 2014 um 15:46 Uhr
@Scott Das ist nicht die Frage. Die Frage ist, warum es diesen Fehler auslöst.
– GuyT
28. Februar 2014 um 15:46 Uhr
Versuchen
$k = array_keys($link); var_dump($link);
um zu sehen, ob $link[‘text’] wird für jede Schleife definiert.– Scott Arciszewski
28. Februar 2014 um 15:48 Uhr