0

I want to sort the second array($array2) based on first array($field['choices'])

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

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

The above code gives me the following output

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

Now the second array starts

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>";
    }
}

Desired output should be

Gyana Yagna
Gyana Yagna
Gyana Yagna
Festival
Festival
Retreat

Whereas I am getting the following output

Gyana Yagna
Gyana Yagna
Gyana Yagna
Retreat
Festival
Festival

Any help is highly appreciated. Thanks in advance.

1
  • Output is wrong because you are looping over the 2nd array hence $array2[3], is returning $v = "Retreat" which is then used by in_array($v,$field['choices']) function. Commented Apr 18, 2016 at 22:25

1 Answer 1

1

Here is a solution based on my comment:

$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>";
      }
}

}

And Output is same as your desired output.

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.