如何在php

时间:2015-06-22 10:20:27

标签: php arrays sorting multidimensional-array

我有一个来自api的数组响应, 在每个数组项中,我都有它的参数(名称,id等)和另一个度量数组,在这个度量中有一个名为" sort"并且它有一个带有id的值,如何通过此度量值id对此数组进行排序?

示例数组

$response = array(
   [0] => array(
     'id'   => 26374,
     'name' => 'item 1',
     'cat'  => 'items_cat'
     'measures' => array(
        [0] =>  array(
            'name' => 'height',
            'value' => 3.5,
            'valueiD' => 35466
        ),
        [1] =>  array(
            'name' => 'width',
            'value' => 4.7,
            'valueiD' => 22466
        ),
        [2] =>  array(
            'name' => 'sort',
            'value' => 3,
            'valueiD' => 35466
        )
     ),
     [1] => array(
       'id'   => 26454,
       'name' => 'item 21',
       'cat'  => 'items_cat'
       'measures' => array(
          [0] =>  array(
              'name' => 'height',
              'value' => 6.5,
              'valueiD' => 23456
          ),
          [1] =>  array(
              'name' => 'width',
              'value' => 43.2,
              'valueiD' => 12443
          ),
          [2] =>  array(
              'name' => 'sort',
              'value' => 1,
              'valueiD' => 35466
          )
       ),
     [2] => array(
       'id'   => 26374,
       'name' => 'item 14',
       'cat'  => 'items_cat'
       'measures' => array(
          [0] =>  array(
              'name' => 'height',
              'value' => 12.5,
              'valueiD' => 32344
          ),
          [1] =>  array(
              'name' => 'width',
              'value' => 11.7,
              'valueiD' => 23445
          ),
          [2] =>  array(
              'name' => 'sort',
              'value' => 2,
              'valueiD' => 35466
          )
       )
);

我试图通过每一个并获得排序值来排序,我不知道如何完成它

foreach ($responseArr as $key => $single) {

    foreach($single->measures as $measure){
        if($measure->name == 'sort'){
            //get the value of this measure
            $sort[$key] = $measure->value
        }
    }
}

//than do something to sort the response array

1 个答案:

答案 0 :(得分:0)

我将您的输入数据转换为以下内容......

$responses = array
(
   0 => array
   (
     'id'   => 26374,
     'name' => 'item 1',
     'cat'  => 'items_cat',
     'measures' => array
     (
        0 =>  array('name' => 'height','value' => 3.5,'valueiD' => 35466),
        1 =>  array('name' => 'width','value' => 4.7,'valueiD' => 22466),
        2 =>  array('name' => 'sort','value' => 3,'valueiD' => 35466)
     )
   ),
   1 => array
   (
     'id'   => 26454,
     'name' => 'item 21',
     'cat'  => 'items_cat',
     'measures' => array
     (
        0 =>  array('name' => 'height','value' => 6.5,'valueiD' => 23456),
        1 =>  array('name' => 'width','value' => 43.2,'valueiD' => 12443),
        2 =>  array('name' => 'sort','value' => 1,'valueiD' => 35466)
     )
   ),
   2 => array
   (
     'id'   => 26374,
     'name' => 'item 14',
     'cat'  => 'items_cat',
     'measures' => array
     (
        0 =>  array('name' => 'height','value' => 12.5,'valueiD' => 32344),
        1 =>  array('name' => 'width','value' => 11.7,'valueiD' => 23445),
        2 =>  array('name' => 'sort','value' => 2,'valueiD' => 35466)
     )
   )
);

代码......

$adjusted = array();
foreach ($responses as $response)
{
  $id = $response['id'];
  $measures = $response['measures'];
  foreach ($measures as $measure)
  {
    if ( $measure['name'] == 'sort' )
      $sort = $measure['value'];
  }
  echo "Found item $id with sort $sort<br>\n";

  $adjusted[$sort] = $response;
}

ksort($adjusted);

echo "<br>\n";
foreach ($adjusted as $sort => $response)
  echo "Sorted item " . $response['id'] . " with sort $sort<br>\n";

输出......

Found item 26374 with sort 3
Found item 26454 with sort 1
Found item 26374 with sort 2

Sorted item 26454 with sort 1
Sorted item 26374 with sort 2
Sorted item 26374 with sort 3

您会注意到示例数据具有重复的ID值。