简体   繁体   中英

Place values from multidimensional array into a new array

I have been tasked with alphabetizing some values ($highschool) that I've outputted with a php for loop on a Drupal site. My understanding is that I need to put these values into a new array in order to sort through them properly in this instance.

<select id="high_school" name="high_school_name" required="">
    <option value="">High School*</option>

    <?php 
        $highschool_info = field_get_items('node', $node, 'field_high_school_info');
        for ($i = 0; $i < count($highschool_info); $i++) {
        $value = field_view_value('node', $node, 'field_high_school_info', $highschool_info[$i]);
        $field_collection = $value['entity']['field_collection_item'][key($value['entity']['field_collection_item'])];
        $highschool = render($field_collection['field_high_school']['#items'][0]['value']);
    ?>

    <option value="<?php print render ($highschool); ?>"><?php print render ($highschool);?></option>
    <?php } ?>
</select>

My thinking was that I'd be able to accomplish my goal of putting all instances of $highschool into an array ($highschool_array), but I think what happens is every new instance of $highschool overwrites the previous one so that the array just contains the final instance of $highschool.

        $value = field_view_value('node', $node, 'field_high_school_info', $highschool_info[$i]);
        $field_collection = $value['entity']['field_collection_item'][key($value['entity']['field_collection_item'])];
        $highschool = render($field_collection['field_high_school']['#items'][0]['value']);

        $highschool_array= array();

        $highschool_array[] = $highschool;

What's the simplest way to accomplish what I'm after? For clarification- let's say from my above loop outputs four option values from the $highschool variable: First Highschool, Second Highschool, Third Highschool and Fourth Highschool. How can I put all of these values into the $highschool_array, so that I can eventually sort them so they run in this order: First Highschool, Fourth Highschool, Second Highschool, Third Highschool.

Also, this is what $highschool_info looks like when printed.

Array
(
[0] = Array
    (
        [value] = 2718
        [revision_id] = 189531
    )

[1] = Array
    (
        [value] = 2719
        [revision_id] = 189532
    )

[2] = Array
    (
        [value] = 2720
        [revision_id] = 189821
    )

)
$highschool_array = array_multisort($highschool_info, SORT_ASC, SORT_STRING);

array_multisort() looks to be your best bet. Look at examples 2 and 3 in the PHP doc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM