基于另一个数组排序数组

时间:2016-04-18 21:59:12

标签: php arrays

我想基于第一个数组($ field ['choices'])对第二个数组($ array2)进行排序

or updated fiddle link here, [Link][1]


  [1]: https://jsfiddle.net/zeasts/ynyybte7/1/

上面的代码给出了以下输出

<a href="https://portal.azure.com/#blade/Microsoft_Azure_Compute/CreateMultiVmWizardBlade/internal_bladeCallId/anything/internal_bladeCallerParams/{"initialData":{},"providerConfig":{"createUiDefinition":"URL_ENCODED_LINK TO_createUiDefinition.json"}}">[Preview createUiDefinition.json]</a>

现在第二个数组启动

$field_name = "field_52f8fcdd21cb6";
$field = get_field_object($field_name);

foreach( $field['choices'] as $k=>$v) {
    echo $k."==".$v."<br>";
}

所需的输出应为

Gyana Yagna==Gyana Yagna
Festival==Festival
Retreat==Retreat
Performance==Performance
Swaranjali==Swaranjali

而我得到以下输出

echo "<br><br><br>";
$array2 = array("Gyana Yagna","Gyana Yagna","Gyana Yagna","Retreat","Festival","Festival");

foreach( $array2 as $k=>$v)
{
    if(in_array($v,$field['choices']))
    {
        echo $array2[$k]."<br>";
    }
    else
    {
        echo $array2[$k+1]."<br>";
    }
}

非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

以下是基于我评论的解决方案:

$field['choices']= array('Gyana Yagna', 'Festival', 'Retreat');


echo "<br><br><br>";
$array2 = array("Gyana Yagna","Gyana Yagna","Gyana Yagna","Retreat","Festival","Festival");

foreach( $field['choices'] as $k=>$v)
{
foreach($array2 as $k2=>$v2){
    if($v==$v2)
      {
          echo $array2[$k2]."<br>";
      }
}

}

输出与您想要的输出相同。