Home > Net >  Populate global array with vars from while loop
Populate global array with vars from while loop

Time:01-20

my goal: populate an array with values from while loop. Before adding them to the array I want to check if they exists. The structure should look like this:

Array
(
    [0] => Array
        (
            [0] => "<p>text</p>"
            [1] => "<p>more</p>"
            [2] => "<p>thing</p>"
        )

    [1] => Array
        (
            [0] => "<p>text</p>"
            [1] => "<p>more</p>"
            [2] => "<p>thing</p>"
        )

)

This works like this:

$i = 1;
$content = array();
$output = array();
while ($i <= 2) : 
    $text = '"<p>text</p>"';
    $more = '"<p>more</p>"';
    $some = '"<p>thing</p>"';
    $content[] = [$text, $more, $some];
    $i  ;
endwhile;
$output[] = array(
    'content' => $content
);
print_r($content);

But if I try to check if an value exists and add it to the array

$i = 1;
$content = array();
$output = array();
while ($i <= 2) : $i  ;
    $text = '"<p>text</p>"';
    $more = '"<p>more</p>"';
    $some = '"<p>thing</p>"';
    if ($text) :
        $content[] = [$text];
    elseif ($more) :
        $content[] = [$more];
    elseif ($some) :
        $content[] = [$some];
    endif;
endwhile;
$output[] = array(
    'content' => $content
);
print_r($content);

I only get the first value added

Array
(
    [0] => Array
        (
            [0] => "<p>text</p>"
        )

    [1] => Array
        (
            [0] => "<p>text</p>"
        )

)

UPDATE:

The answer from Sri GnS works in this case. I thought I could adapt his solution for my special case. Unfortunately it doesnt work there.

Im using Wordpress with ACF and going through several repeater fields. I will ask a new question on wordpress.stackexchange. Thanks!

CodePudding user response:

Try using the following code to add all the values to the array:

$i = 1;
$content = array();
$output = array();
while ($i <= 2) : 
    $text = '"<p>text</p>"';
    $more = '"<p>more</p>"';
    $some = '"<p>thing</p>"';
    $content[] = [$text, $more, $some];
    $i  ;
endwhile;
$output[] = array(
    'content' => $content
);
print_r($content);

If you still want to check if the value exists before adding it, you can use the empty() function to check if the variable is empty or not.

$i = 1;
$content = array();
$output = array();
while ($i <= 2) : 
    $text = '"<p>text</p>"';
    $more = '"<p>more</p>"';
    $some = '"<p>thing</p>"';
    if(!empty($text)) $content[] = $text;
    if(!empty($more)) $content[] = $more;
    if(!empty($some)) $content[] = $some;
    $i  ;
endwhile;
$output[] = array(
    'content' => $content
);
print_r($content);

Note that the above example doesn't add the values to a nested array, but just adds them to a single dimension array. If you want to add the values to a nested array, you can make use of the [] operator to create a new array inside the existing array

$i = 1;
$content = array();
$output = array();
while ($i <= 2) : 
    $text = '"<p>text</p>"';
    $more = '"<p>more</p>"';
    $some = '"<p>thing</p>"';
    $inner_content = array();
    if(!empty($text)) $inner_content[] = $text;
    if(!empty($more)) $inner_content[] = $more;
    if(!empty($some)) $inner_content[] = $some;
    $content[] = $inner_content;
    $i  ;
endwhile;
$output[] = array(
    'content' => $content
);
print_r($content);

CodePudding user response:

In the second example, you're only adding the first value, $text, to the $content array because the if statement only checks if $text is truthy, not if $more or $some are truthy. To check if all three variables are truthy and add them to the $content array, you can use the array_filter() function to remove any empty or false values, and then use the array_push() function to add the remaining values to the $content array:

$i = 1;
$content = array();
$output = array();
while ($i <= 2) :
    $text = '"<p>text</p>"';
    $more = '"<p>more</p>"';
    $some = '"<p>thing</p>"';
    $values = array_filter(array($text, $more, $some));
    array_push($content, $values);
    $i  ;
endwhile;
$output[] = array(
    'content' => $content
);
print_r($content);

Output:

Array
(
    [0] => Array
        (
            [0] => "<p>text</p>"
            [1] => "<p>more</p>"
            [2] => "<p>thing</p>"
        )

    [1] => Array
        (
            [0] => "<p>text</p>"
            [1] => "<p>more</p>"
            [2] => "<p>thing</p>"
        )

)
  • Related