php - Arrays not working with echo -
i have code below in wordpress template generates array $meta
:
<?php global $wpdb; $query = ("select id, post_password, post_name $wpdb->posts post_password = 'listing'"); $listings = $wpdb->get_results( $query, object); foreach($listings $listing) { $meta = get_post_meta($listing->id,'' ,true); $meta = array_map(function($n) {return $n[0];}, $meta); echo "<br /> listing->id = $listing->id"; echo "<pre>"; print_r($meta); echo "</pre>"; $first_name = $meta['first_name']; $last_name = $meta['last_name']; ?> <tr> <td> <?php echo $meta['first_name']; ?> </td>
the print_r($meta) above prints out
array ( [first_name] => john [last_name] => jones [business_name] => [phone] => 208 324-6916 [email] => johnjones@yahoo.com [page_name] => [barcode_number] => [website] => [header] => [description_1] => [description_2] => [footer] => )
i want echo these values table cell. if write
<td> <?php echo $meta['first_name']; ?> </td>
i error: notice: undefined index: first_name in . . .
but if
$first_name = $meta['first_name']; ?> <td> <?php echo $first_name; ?> </td>
it works. know what's going on? thanks.
you might accidentally overwriting $meta variable somewhere between assignment , output. print full array
<td> <?php var_dump($meta); ?> </td>
and compare 1 defined. may surprised it's not you've set before or not array anymore. can happen implicitly, functions extract() etc.
Comments
Post a Comment