Create an array from a for

1

I am trying to create the contents of an array from a for loop. I leave you with the code:

$prodlang = $wpdb->get_results("SELECT * FROM wp_post WHERE wp_pedmin!=0");
$tama= count($prodlang);

$product_min_qty = array( 

    for($i=0;$i<$tama;$i++){
        if ($i+1==$tama || $i+1>$tama){
            array( 'id' => $prodlang[$i]->ID , 'min' => $prodlang[$i]->wp_pedmin)
        } else {
            array( 'id' => $prodlang[$i]->ID , 'min' => $prodlang[$i]->wp_pedmin),      
        }
    }
);

I try to create the content of $product_min_qty but it dies in the script. What escapes me?

    
asked by akenateb 29.01.2017 в 13:54
source

3 answers

2

The code has several important errors:

  • You can not have a for loop within the definition of a array
  • It seems that you want to simulate a ternary operator, but you lack parts (specifically the most important one that would be the assignment)
  • Operations in PHP are separated with a semicolon, and not with a comma or nothing.

To solve these problems you should:

  • Define the variable $product_min_qty as an empty array
  • Create a loop and fill in the variable within that loop
  • The two parts of if..else are equal, you could simplify them directly or, if they are going to be different, use a ternary operator to define each part
  • As indicated by Genarito in your comment , the condition of the if can be simplified because the second part ( $i+1>$tama ) will always be false for the values of the loop for .

Something like this (I have not tried it and may contain errors):

$prodlang = $wpdb->get_results("SELECT * FROM wp_post WHERE wp_pedmin!=0");
$tama= count($prodlang);
$product_min_qty = array() 

for($i=0;$i<$tama;$i++){
    $product_min_qty[] = ($i+1 == $tama) ?
                         array( 'id' => $prodlang[$i]->ID , 'min' => $prodlang[$i]->wp_pedmin) :
                         array( 'id' => $prodlang[$i]->ID , 'min' => $prodlang[$i]->wp_pedmin);
}

As I put you in a comment that I deleted, I would recommend that while you are developing you use error_reporting(E_ALL); at the beginning of the page to see what errors you have, because it would be of great help to identify and solve problems such as shared code.

    
answered by 29.01.2017 в 16:58
1

No You can do a for within array , since this would generate a syntax error, you can do the following:

$prodlang = $wpdb->get_results("SELECT * FROM wp_post WHERE wp_pedmin != 0");
$tama= count($prodlang);

$product_min_qty = array(); // Inicializas tu array

for($i=0;$i<$tama;$i++){ // Realizas el for 
    if ($i+1==$tama || $i+1>$tama){
        // Haces un push de acuerdo a tus condiciones
        $product_min_qty[] = array( 'id' => $prodlang[$i]->ID , 'min' => $prodlang[$i]->wp_pedmin); 
    } else {
        // Haces un push de acuerdo a tus demás condiciones
        $product_min_qty[] = array( 'id' => $prodlang[$i]->ID , 'min' => $prodlang[$i]->wp_pedmin); 
    }
}

And now to see the result of this you can make a simple:

echo "<pre>";
print_r($product_min_qty);
echo "</pre>";
    
answered by 29.01.2017 в 16:55
1

I'll give you something similar to what you ask with an example

$array = call_user_func(function() use ($variablesaUsar) {
   // aqui pones el for rellenando un array temporal.
   return $arrayRelleno; // devuelves el array temporal y se asignara a tu     array antes creado llamado $array.
});
    
answered by 31.01.2017 в 09:31