PHP -更新多维数组中键的值



我有以下数组:

 $sections = array(
                0 => array(
                    'id' => 'betrothed_details',
                    'name' => 'About us',
                    'order' => '1',
                    'menu_name' => '',
                    'display' => '1'
                ),
                1 => array(
                    'id' => 'events',
                    'name' => 'Events',
                    'display' => '1',
                    'order' => '2',
                    'menu_name' => ''
                ),
                2 => array(
                    'id' => 'gallery',
                    'name' => 'Gallery',
                    'order' => '3',
                    'menu_name' => '',
                    'display' => '1'
                ),
            );

我创建了拖放排序,它以正确的顺序传递id,如

$ids = array('events','betrothed_details','gallery')

对于每个$ids数组,我需要在$sections数组中更改相应的order值。

$i = 1;
foreach ($ids as $id) {
  CHANGE THE ORDER VALUE HERE to $i
  $i++;
}

这是怎么做的?

试试这个:

<?php
$sections = array(
    0 => array(
        'id' => 'betrothed_details',
        'name' => 'About us',
        'order' => '1',
        'menu_name' => '',
        'display' => '1'
    ),
    1 => array(
        'id' => 'events',
        'name' => 'Events',
        'display' => '1',
        'order' => '2',
        'menu_name' => ''
    ),
    2 => array(
        'id' => 'gallery',
        'name' => 'Gallery',
        'order' => '3',
        'menu_name' => '',
        'display' => '1'
    ),
);
$ids = array('events', 'betrothed_details', 'gallery');
$sorted_sections = array();
foreach ($ids as $id) {
    foreach ($sections as $section) {
        if ($section['id'] == $id) {
            $sorted_sections[] = $section;
            break;
        }
    }
}
print_r($sorted_sections);

演示链接

相关内容

  • 没有找到相关文章

最新更新