操纵数组

时间:2018-10-10 08:04:13

标签: php arrays

我正在使用这段代码来生成数组。这是一个while循环,其中$povezani得到一个连接的SKU数组,该数组应该与$ transfer_items数组合并,但必须以我在底部显示的方式进行合并。这样,我现在无法获得所需的结果。您能帮我如何获得所需的阵列吗?

$transfer_items= array();
//Get calculation items in array
if($stmt = $conn -> prepare("SELECT article_no, mpc, vpc FROM dpb.calculation_items WHERE calculation_id = ? AND used_for_purchase = 1")) {
   $stmt -> bind_param("i", $calculation_id);
   $stmt -> execute();
   $stmt -> store_result();
   $stmt -> bind_result($article_no, $mpc, $vpc);
   while($stmt -> fetch()) {
        $povezani = zamjena_broja($article_no);

        $transfer_items[] = array('article_no' => $povezani,
                                  'mpc' => $mpc,
                                  'vpc' => $vpc);
   }
   $stmt -> close();
}

这是我得到的结果:

    array (size=6)
  0 => 
    array (size=3)
      'article_no' => string '967 65 02-16' (length=12)
      'mpc' => string '33000.00' (length=8)
      'vpc' => string '26400.00' (length=8)
  1 => 
    array (size=3)
      'article_no' => string '967 66 57-02' (length=12)
      'mpc' => string '35600.00' (length=8)
      'vpc' => string '28480.00' (length=8)
  2 => 
    array (size=3)
      'article_no' => string '967 32 96-01' (length=12)
      'mpc' => string '7000.00' (length=7)
      'vpc' => string '5600.00' (length=7)
  3 => 
    array (size=3)
      'article_no' => 
        array (size=2)
          0 => string '952 71 57-52' (length=12)
          1 => string '952 71 57-58' (length=12)
      'mpc' => string '1950.00' (length=7)
      'vpc' => string '1560.00' (length=7)
  4 => 
    array (size=3)
      'article_no' => string '966 62 89-01' (length=12)
      'mpc' => string '5500.00' (length=7)
      'vpc' => string '4400.00' (length=7)
  5 => 
    array (size=3)
      'article_no' => string '967 32 96-01' (length=12)
      'mpc' => string '7000.00' (length=7)
      'vpc' => string '5600.00' (length=7)

给我一​​个问题的结果在数组3上,它看起来应该像这样:

    3 => 
        array (size=3)
          'article_no' => string '952 71 57-52' (length=12)              
          'mpc' => string '1950.00' (length=7)
          'vpc' => string '1560.00' (length=7)
   4 =>
       array (size=3)
          'article_no' => string '952 71 57-58' (length=12)
          'mpc' => string '1950.00' (length=7)
          'vpc' => string '1560.00' (length=7)

如何从位置3的数组中提取子数组并获得上面显示的所需结果?

函数zamjena_broja

function zamjena_broja($kataloski_broj){

    global $conn;
    include("db_conn.php");

    $kataloski_broj_data = array();

    //Traženje zamjenskog broja
    $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj
                     FROM dpb.nav_replacement_numbers
                     WHERE glavni_broj = '$kataloski_broj'
                     OR pocetni_broj = '$kataloski_broj'";
    $rezultat_zamjena = mysqli_query($conn, $upit_zamjena) or die (mysqli_error($conn));
    $broj = mysqli_num_rows($rezultat_zamjena);
    while($row = mysqli_fetch_array($rezultat_zamjena)){
        $kataloski_broj_data[] = $row["zamjenski_broj"];
        $kataloski_broj_data[] = $row["pocetni_broj"];

    }

    if ($broj > 0) {  
        $kataloski_broj = array_map("unserialize", array_unique(array_map("serialize", $kataloski_broj_data)));
        //var_dump($kataloski_broj);
    } else {
        $kataloski_broj;
    }      
    return $kataloski_broj;

}

2 个答案:

答案 0 :(得分:1)

由于函数zamjena_broja可以返回一个数组,因此您需要在while循环中处理该数组。

while($stmt -> fetch()) {
    $povezani = zamjena_broja($article_no);

    if ( is_array($povezani) ) {
        foreach ($povezani as $one) {
            $transfer_items[] = array('article_no' => $one,
                                      'mpc' => $mpc,
                                      'vpc' => $vpc);
        }
    } else {
        $transfer_items[] = array('article_no' => $one,
                                  'mpc' => $mpc,
                                  'vpc' => $vpc); 
    } 
}

答案 1 :(得分:0)

根据以上答案,您可以尝试使用.jar来实现:

array_map