Support

Account

Home Forums Add-ons Repeater Field Can't create array with repeater sub_field

Solved

Can't create array with repeater sub_field

  • Hi,
    I am trying to create an array out of get_sub_field values.

    This: $value = get_sub_field(‘fee’);
    with print_r gives me this: 2500300035004000
    Its actually: 2500 3000 3500 4000

    When i try to make it as an array doing this: $value = explode(“, “, get_sub_field(‘fee’));
    It give me this:
    Array ( [0] => 2500 ) Array ( [0] => 3000 ) Array ( [0] => 3500 ) Array ( [0] => 4000 )

    What i need is a single array with all the values like this:

    Array
        [0] => 2500
        [1] => 3000
        [2] => 3500
        [3] => 4000
    )

    Please help out i have tried from foreach, while, for loops with array_merge to array_combine but nothing seems to do the trick.

    What am i doing wrong here?
    Thanks.

  • When you want to put values in an array, first you need to create an empty array and then use array syntax to add the values

    
    $value = array();
    
    // later
    $value[] = get_sub_field('fee');
    
  • Hi, I have tried this:

    $value = array();
    // later
    $value[] = get_sub_field('fee');
    print_r($value);

    but its still giving me this:
    Array ( [0] => 2500 ) Array ( [0] => 3000 ) Array ( [0] => 3500 ) Array ( [0] => 4000 )

  • There isn’t enough code or information to tell what’s going on here. What kind of field is sub field? What is the actual code your using for your loop and building the array?

  • The sub field is just a text field which is a repeater within a repeater.

  • Then there is no reason it should be creating an array of arrays if done right.

    Can you supply a more expanded snippet of the code that you’re actually using?

  • Hi i managed to create the single array:

    $addmore_deadline_cat_fees = array();
    if (have_rows('add_more_deadlines')):
    while (have_rows('add_more_deadlines')) : the_row();
    
        $array = array();
    
        if (have_rows('categories_and_fees')):
        while (have_rows('categories_and_fees')) : the_row();
        
            $array_val = get_sub_field('fee');
        
            array_push($array,$array_val);
        
        endwhile;endif;
        
        $addmore_deadline_cat_fees = array_merge($addmore_deadline_cat_fees, $array);
    endwhile;endif;
    print_r($addmore_deadline_cat_fees);
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Can't create array with repeater sub_field’ is closed to new replies.