0

I have following array:

 $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'
                ),
            );

I created drag and drop sorting, wchich passes the id's in the correct order, like

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

For each of the $ids array I would need to change the appropriate order value in $sections array.

$i = 1;
foreach ($ids as $id) {

  CHANGE THE ORDER VALUE HERE to $i
  $i++;
}

How is this done?

2
  • first u do not need $i = 0; $i++; in foreach it just finishes in array's last variable Commented May 3, 2014 at 15:47
  • I edited the question to make it more clear why I have the $i in there.. Commented May 3, 2014 at 15:49

1 Answer 1

2

Try this:

<?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);

Demo Link

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.