我有一個問題,我需要將explode(":",$productsOrdered)
轉換爲多級數組。將字符串分解爲多級數組
我需要這樣的:
$productsOrdered = "name:quantity:priceWithoutVat:vatAmount:priceWithVat:lineTotal:name:quantity:priceWithoutVat:vatAmount:priceWithVat:lineTotal:name:quantity:priceWithoutVat:vatAmount:priceWithVat:lineTotal:";
變成這樣:
$productList = array(
array(name,quantity,priceWithoutVat,vatAmount,priceWithVat,lineTotal),
array(name,quantity,priceWithoutVat,vatAmount,priceWithVat,lineTotal),
array(name,quantity,priceWithoutVat,vatAmount,priceWithVat,lineTotal),
);
但我需要它的工作無論多少產品是如何在$productsOrdered
變量。 我用:
$product = explode(":",$product);
但我不知道如何將其轉換成我需要的是一個多層次的數組。
不可能的。 explode()只能返回一個數組。如果你想要多維數組,你將不得不在循環中運行多個爆炸。 –
是已知的子數組中的項目數量? – EGN
如果這些只是相同的字符串,爲什麼不計算它們有多少,就像substr_count($ productsOrdered,'name:quantity:priceWithoutVat:vatAmount:priceWithVat:lineTotal'); 只需創建有序數組? –