Dynamic associative array?

I have returned an array

$header_html = array(1=>array('width'=>40,
                               'sort_case'=>23,
                               'title'=>'AxA'),
                      2=>array('width'=>50,
                               'sort_case'=>7,
                               'title'=>'B2B'),
                      3=>array('width'=>100,
                               'sort_case'=>12,
                               'title'=>'C12')
                      );

I want to get a new array that depends on $ header_array = array ('AxA', 'B2B', 'C12')

for examples:

 if have $header_array=array('C12','B2B','AxA').

the new $ header_html will be:

$header_html = array(
                      1=>array('width'=>100,
                               'sort_case'=>12,
                               'title'=>'C12'),                         
                      2=>array('width'=>50,
                               'sort_case'=>7,
                               'title'=>'B2B'),
                      3=>array('width'=>40,
                               'sort_case'=>23,
                               'title'=>'AxA')

                      );

etc.

Does anyone know how to do this?

+3
source share
4 answers

You can sort the array using a special comparison function using usort :

function cmp($a, $b) {
  // Sort via $a['title'] and $b['title']
}

usort($header_html, 'cmp');

The trick comes with a comparison function that does what you want. To just sort back by name, you can use:

function cmp($a, $b) {
  if ($a['title'] == $b['title'])
    return 0;

  // usually return -1 if $a < $b, but we're sorting backwards 
  return ($a['title'] < $b['title'] ? 1 : -1;
}
+2
source

In PHP 5.3, you can easily do this with a functor and usort.

class MyComparator {
  protected $order = array();

  public function __construct() {
    $values = func_get_args();
    $i = 0;
    foreach($values as $v) {
      $this->order[$v] = $i;
      $i++;
    }
  }

  public function __invoke($a, $b) {
    $vala = isset($this->order[$a['title']]) ?
      $this->order[$a['title']] : count($this->order);
    $valb = isset($this->order[$b['title']]) ?
      $this->order[$b['title']] : count($this->order);
    if($vala == $valb) return 0;
    return $vala < $valb ? -1 : 1;
  }
}

You can use it like this:

$sorter = new MyComparator('CCC', 'AAA', 'BBB');
usort($header_html, $sorter);
+2
source

, :

function mysort($a, $b)
{
  global $header_array;
  $pos1 = array_search($a["title"], $header_array);
  $pos2 = array_search($b["title"], $header_array);
  if ($pos1 == $pos2) { return 0; }
  return $pos1 < $pos2 ? -1 : 1;
}

$header_array = array("CCC", "BBB", "AAA");
usort($header_html, "mysort");

print_r($header_array);

: usort() true false ; .

+2

It looks like you want the function to return the elements of the array in the order specified in $header_array. If so, here is a hit:

function header_resort($header_array, $header_html) {
    foreach($header_array as $i => $val) {
        foreach($header_html as $obj) {
            if( $obj->title == $val )
                $header_html_new[$i] = $obj;
        }
    }
    return $header_html_new;
}
+1
source

Source: https://habr.com/ru/post/1760409/


All Articles