代码之家  ›  专栏  ›  技术社区  ›  ForgivenIT

对于输出php、excel,将多维数组转换为不同的多维数组

  •  -1
  • ForgivenIT  · 技术社区  · 6 年前

    (我的条件可能不正确(也许我为什么在谷歌没有结果),所以请随时改正我的条件)。 我需要改变一个输出的数组

    {"Source":["BKD060818","BKD060818",],"Qty":["15","23"],"ISBN":["9780805079333","9780143118978",],
    "Title":["    ","    ",],"Author":["Boyle","Thurston Clarke"],"Price":["2.50","3.43"],
    "Guide":["a","f",]}
    

    {"Source":"BKD060818","Qty":15, "ISBN":"9781452128450", "Title":" ","Author":"Boyle","Price":"2.50","Guide":"a"},
    {"Source":"BKD060818","Qty":23, "ISBN":"9780143118978", "Title":" ","Author":"Thurston Clarke","Price":"3.43","Guide":"f"}
    

    代码

    if(!empty($guides)){
        foreach($guides as $key=>$value) {
    
            $isbn10Array[$i] = $row['isbn10'];
            $isbn13Array[$i] = $row['isbn13'];
    
            if(!empty($value)) {
                $guideArray[$i] = $key;
                $qtyArray[$i] = $value;
    
                switch ($guideArray[$i]) { 
                }//leftout code here that is large switch statment
    
                $guideRefArray[$i] = $guideRef;   
                $poArray[$i] = $po;
                $i++;
             }
        }//FOREACH
    }//NOT EMPTY GUIDES      
    }//END OF WHILE LOOP //never started a while loop here?
    
    $ships = array('Source'=>$poArray,
                               'Qty'=>$qtyArray,
                               'ISBN'=>$isbn13Array,
                               'Title'=>$titleArray,
                               'Author'=>$authorArray,
                               'Price'=>$PriceArray,
                               'Guide'=>$guideArray,
    
                              );
    

    $ships输出我现在看到的是将它移动到

    $decision[$i] = array('Source'=>$po,
                               'Qty'=>$qty,
                               'ISBN'=>$isbn13,
                               'Title'=>$title,
                               'Author'=>$author,
                               'Price'=>$Price,
                               'Guide'=>$guide,
    
                              );
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Kisaragi    6 年前

    问题中发布的JSON无效,如果这是一个拼写错误,您实际上拥有有效的JSON,可以执行以下操作:

    $json = '{
        "Source": ["BKD060818", "BKD060818"],
        "Qty": ["15", "23"],
        "ISBN": ["9780805079333", "9780143118978"],
        "Title": ["    ", "    "],
        "Author": ["Boyle", "Thurston Clarke"],
        "Price": {
            "0": "2.50",
            "27": "3.43"
        },
        "Guide": ["a", "f"]
     }';    
    
    
    $array = json_decode($json, true); 
    
    foreach($array as $index=>$value){
        $i = 0;
        while($i <= count($array[$index])-1){
            if(($i == 1)&& ($index == "Price")){
                $new[$i][$index] = $array[$index][27]; //because one of the indexes of price is 27?
            }else{
                $new[$i][$index] = $array[$index][$i];      
            }
            $i++;
        }
    }
    
    var_dump($new); 
    output is 
    array(2) {
       [0]=>
         array(7) {
         ["Source"]=>
         string(9) "BKD060818"
         ["Qty"]=>
         string(2) "15"
         ["ISBN"]=>
         string(13) "9780805079333"
         ["Title"]=>
         string(4) "    "
         ["Author"]=>
         string(5) "Boyle"
         ["Price"]=>
         string(4) "2.50"
         ["Guide"]=>
         string(1) "a"
       }
       [1]=>
         array(7) {
         ["Source"]=>
         string(9) "BKD060818"
         ["Qty"]=>
         string(2) "23"
         ["ISBN"]=>
         string(13) "9780143118978"
         ["Title"]=>
         string(4) "    "
         ["Author"]=>
         string(15) "Thurston Clarke"
         ["Price"]=>
         string(4) "3.43"
         ["Guide"]=>
         string(1) "f"
       }
    }
    

    然后可以将数组编码回json,如下所示:

    $array1 = json_encode($new[0]); 
    $array2 = json_encode($new[1]);