doushe2513
2018-08-09 16:14
浏览 102
已采纳

如何使用foreach重建用逗号分隔的一组数组?

I'm using ACF Pro to get all my choices or specific field. I want to store these choice values into an array structure like this...

$mstars = array( 
    'relation' => 'OR',
array (
    'key' => 'product_owner',
    'value' => 'Bob Ross',
    'compare' => 'LIKE'
),
array(
    'key' => 'product_owner',
    'value' => 'Michael Jackson',
    'compare' => 'LIKE'
),
array(
    'key' => 'product_owner',
    'value' => 'Tom Hanks',
    'compare' => 'LIKE'
)

...Bob, Micheal and Tom are the only things coming from my select choices. The 'key' and 'compare' can be manully placed in my loop sense that never changes. How should I go about building that? This is what I have so far...

//I'm getting the ACF key to find my specific field
$field_key = "field_5b5b34f4ed085";
//I now have field data
$field = get_field_object($field_key);

//initiate an array
$mstars = [];
//loop through all my choices of this specific field
foreach($field['choices'] as $mkey => $mvalue) {
    //how do I add 'key' => 'product_owner', to each sub array?
    //how can I add Bob, Michael and Tom as values to each sub array?
    //how do I add compare => 'LIKE' to each sub array?
}

//I could then use $mstars[] where needed for get_posts() arguments

Many thanks on how to do this!

A var_dump of my choices looks like this...

var_dump($field['choices']);

array(6) { 
  ["Michael Jackson"]=> string(16) "Michael Jackson" 
  ["Bob Ross"]=> string(12) "Bob Ross" 
  ["Tom Hanks"]=> string(13) "Tom Hanks"
}

图片转代码服务由CSDN问答提供 功能建议

我正在使用ACF Pro获取所有选项或特定字段。 我想将这些选择值存储到这样的数组结构中......

  $ mstars = array(
'relation'=>'OR',
array(  
'key'=>'product_owner',
'value'=>'Bob Ross',
'比较'=>'LIKE'
),
array(
'key'=>  ;'product_owner',
'value'=>'Michael Jackson',
'比较'=>'LIKE'
),
array(
'key'=>'product_owner',
  'value'=>'Tom Hanks',
'比较'=>'LIKE'
)
   
 
 

... Bob, Micheal和Tom是我选择的唯一选择。'key'和'compare'可以放在我的循环意义上,永远不会改变。我该如何构建呢?这就是我所拥有的 到目前为止...

  //我正在获取ACF键以查找我的特定字段
 $ field_key =“field_5b5b34f4ed085”; 
 //我现在有字段 data 
 $ field = get_field_object($ field_key); 
 
 //启动一个数组
 $ mstars = []; 
 //循环遍历我对此特定字段的所有选择
foreach($ 字段['choices']为$ mkey =>  $ mvalue){
 //如何添加'key'=>  'product_owner',每个子数组?
 //如何将Bob,Michael和Tom作为值添加到每个子数组?
 //如何添加compare =>  'LIKE'到每个子数组?
} 
 
 //然后我可以使用$ mstars [],其中get_posts()参数需要
   
 
 

许多 感谢如何做到这一点!

我选择的 var_dump 看起来像这样......

   var_dump($ field ['choices']); 
 
array(6){
 [“Michael Jackson”] =>  string(16)“Michael Jackson”
 [“Bob Ross”] =>  string(12)“Bob Ross”
 [“Tom Hanks”] =>  string(13)“Tom Hanks”
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dptdb84606 2018-08-09 16:26
    已采纳

    Assuming $mvalue is a string that contains the name you want:

    foreach($field['choices'] as $mkey=>$mvalue){
        $mstars[] = array(
         'key'=> 'product_owner',
         'value'=> $mvalue,
         'compare'=> 'LIKE'
        );
    }
    
    $mstars['relation'] = 'OR';
    
    打赏 评论
  • dpzr52746 2018-08-09 16:27

    I think you need something like this.

    // Since you always want this, I will add it now as a fixed value.
    $mstars = ['relation' => 'OR'];
    //loop through all my choices of this specific field
    foreach($field['choices'] as $mkey => $mvalue) {
        //how do I add 'key' => 'product_owner', to each sub array?
        //how can I add Bob, Michael and Tom as values to each sub array?
        //how do I add compare => 'LIKE' to each sub array?
        // Make the element you want to add
        $entry = [
            'key' => 'product_owner',
            'value' => $mkey,
            'compare' => 'LIKE'
        ];
        // Add it to the array.
        $mkey[] = $entry;
    }
    

    One point you might consider. You are storing the 'relation' and the actual choices as siblings. i.e. they are at the same level. You might want to consider a slightly different structure so that you can definitively get to your choice list. Instead of adding the choices directly into $mstars, put them into a known key inside of $mstars.

    [
        'relation' => 'OR',
        'data' => [
            ['key' => 'product_owner', 'value' => 'Sally', 'compare' => 'LIKE'],
            ['key' => 'product_owner', 'value' => 'Helen', 'compare' => 'LIKE'],
            ['key' => 'product_owner', 'value' => 'Faria', 'compare' => 'LIKE'],
        ]
    ]
    

    Now you can walk through $mstars['data'] and it will only contain your data. Just a suggestion.

    Also note that [] and array() are now equivalent in PHP. [] is shorter.

    打赏 评论
  • donglaoping9702 2018-08-09 16:40

    You can simply achieve your expected output by the following:

    $field = [
        'choices' => [
            'Michael Jackson' => 'Michael Jackson',
            'Bob Ross' => 'Bob Ross',
            'Tom Hanks' => 'Tom Hanks'
        ]
    ];
    
    $mstars = ['relation' => 'OR'];
    
    foreach ($field['choices'] as $value) {
        $mstars[] = [
            'key' => 'product_owner',
            'value' => $value,
            'compare' => 'LIKE'
        ];
    }
    
    echo '<pre>';
    print_r($mstars);
    

    Yields:

    Array
    (
        [relation] => OR
        [0] => Array
            (
                [key] => product_owner
                [value] => Michael Jackson
                [compare] => LIKE
            )
    
        [1] => Array
            (
                [key] => product_owner
                [value] => Bob Ross
                [compare] => LIKE
            )
    
        [2] => Array
            (
                [key] => product_owner
                [value] => Tom Hanks
                [compare] => LIKE
            )
    
    )
    
    打赏 评论

相关推荐 更多相似问题