admin管理员组

文章数量:1323157

I am creating a custom word press theme and i am little stuck in one situation, what i want is to echo the specific values of an array in the form of bordered tables. For example i want to display | location | qualification | date |

here below is my code

 <td class="row-2 row-email"><?php  $release_edu_qual = get_post_meta( get_the_ID(), '_candidate_education', true );
                      print_r($release_edu_qual); 
 ?></td>

and this is the output of the above code:

Array ( [0] => Array ( [location] => Stanford University [qualification] => School of Arts & Sciences [date] => 2012-2015 [notes] => Maximus faucibus non non nibh. Cras luctus velit et ante vehicula, sit amet commodo magna eleifend. Fusce congue ante id urna porttitor luctus. ) [1] => Array ( [location] => University of Pennsylvania [qualification] => School of Design [date] => 2010-2012 [notes] => Phasellus vestibulum metus orci, ut facilisis dolor interdum eget. Pellentesque magna sem, hendrerit nec elit sit amet, ornare efficitur est. ) [2] => Array ( [location] => Massachusetts Institute of Technology [qualification] => [date] => 2006-2010 [notes] => Suspendisse lorem lorem, aliquet at lectus quis, porttitor porta sapien. Etiam ut turpis tempor, vulputate risus at, elementum dui. Etiam faucibus. ) )

I am creating a custom word press theme and i am little stuck in one situation, what i want is to echo the specific values of an array in the form of bordered tables. For example i want to display | location | qualification | date |

here below is my code

 <td class="row-2 row-email"><?php  $release_edu_qual = get_post_meta( get_the_ID(), '_candidate_education', true );
                      print_r($release_edu_qual); 
 ?></td>

and this is the output of the above code:

Array ( [0] => Array ( [location] => Stanford University [qualification] => School of Arts & Sciences [date] => 2012-2015 [notes] => Maximus faucibus non non nibh. Cras luctus velit et ante vehicula, sit amet commodo magna eleifend. Fusce congue ante id urna porttitor luctus. ) [1] => Array ( [location] => University of Pennsylvania [qualification] => School of Design [date] => 2010-2012 [notes] => Phasellus vestibulum metus orci, ut facilisis dolor interdum eget. Pellentesque magna sem, hendrerit nec elit sit amet, ornare efficitur est. ) [2] => Array ( [location] => Massachusetts Institute of Technology [qualification] => [date] => 2006-2010 [notes] => Suspendisse lorem lorem, aliquet at lectus quis, porttitor porta sapien. Etiam ut turpis tempor, vulputate risus at, elementum dui. Etiam faucibus. ) )

Share Improve this question edited Sep 2, 2020 at 21:41 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Sep 2, 2020 at 10:36 fyn matt 881fyn matt 881 32 bronze badges 1
  • $release1=$release_edu_qual[0]; print_r($release1); see the result – Fakhar alam Commented Sep 2, 2020 at 11:21
Add a comment  | 

2 Answers 2

Reset to default 2

The previous answer is wrong, to access the array elements, you need to get it by the key:

$location = $release_edu_qual[0]["location"];

In the above code, we're getting the location of the array that's first (zero based) in the initial array.

So to list all the array data you want from that initial array you could use something like:

<table>
    <thead>
        <tr>
            <th>Location</th>
            <th>Qualification</th>
            <th>Date</th>
        </tr>
    </thead>
    <tbody>
        <?php
        
        foreach( $release_edu_qual as $item ){
            echo '<tr>';
            echo    '<td>' . $item["location"]      . '</td>';
            echo    '<td>' . $item["qualification"] . '</td>';
            echo    '<td>' . $item["date"]          . '</td>';
            echo '</tr>';
        }
        
        ?>
    </tbody>
</table>

To get a specific item in the object when already inside a specific array.

$location = $release_edu_qual->location;

To get all of a certain value use a foreach loop like this.

foreach ($release_edu_qual as $i) {
    $this_location = $i->location;
    print $this_location;
}

本文标签: pluginsget specific value of a arrayPHP