2013-05-02 41 views
1

此代碼會生成一個數組:從數組剝離不需要的數據在PHP

$size = sizeof($include_quotes); 
    for ($i=0; $i<$size; $i++) { 
    $quotes = $GLOBALS[$include_quotes[$i]]->quote($method); 
    if (is_array($quotes)) $quotes_array[] = $quotes; 
    } 
} 

如果我

print_r($quotes_array); 

我得到如下:

Array ([0] => Array ([id] => advshipper [methods] => Array ([0] => Array ([id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0) [1] => Array ([id] => 2-0-0 [title] => 1-2 working days [cost] => 3.2916666666667 [icon] => [shipping_ts] => [quote_i] => 1) [2] => Array ([id] => 4-0-0 [title] => 2-3 working days [cost] => 2.4916666666667 [icon] => [shipping_ts] => [quote_i] => 2) [3] => Array ([id] => 8-0-0 [title] => Click & Collect [cost] => 0 [icon] => [shipping_ts] => [quote_i] => 3)) [module] => Shipping [tax] => 20)) 

在某些情況下,我只想將0字段中的數據傳遞到代碼的下一部分。但是,使用

$sliced_quotes_array = array_slice($quotes_array,0,1); 

仍返回所有結果。

什麼是正確的方法得到的只是:

Array ([0] => Array ([id] => advshipper [methods] => Array ([0] => Array ([id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0) 

任何幫助非常感謝,因爲我已經嘗試了許多不同的方式,並沒有運氣呢。

使用下列仍返回相同的結果

$testarray = array(0 => $quotes_array[0]); 
print_r($testarray); 
+3

'$ wantedStuff = quotesArray [0];'? – christopher 2013-05-02 14:24:34

+0

你的Json數據中有嵌套數組,你指的是哪個數組索引? – dreamweiver 2013-05-02 14:27:56

+0

@ChrisCooney不,不起作用 – 2013-05-02 14:42:39

回答

0

已經做了一些閱讀上的陣列和一個位試用後,我發現了一個解決我的問題。

我用:

unset($quotes_array[0]['methods'][1]); 

通過改變指數方法後我才得以將降大任的航運選擇,我沒有要求,同時仍然保持功能。

0

爲什麼不直接使用數組構造並明確包括你所需要的:

array(0 => $quotes_array[0]); 
+0

您的意思是這樣的? $ testarray = array(0 => $ quotes_array [0]);但它仍然返回所有相同的結果,當我print_r新陣列。對不起,對於php和學習不太熱,因爲我沿着 – 2013-05-02 14:44:25

+0

這絕對不是應該發生的事情。你確定你正在打印正確的數組嗎? – Magnus 2013-05-02 14:48:45

+0

我會用更多信息編輯原始問題,以便我可以對其進行格式化。 – 2013-05-02 14:53:01

0

這裏是你的數組:

enter image description here

當你說「我只想要字段0中的數據被傳遞到代碼的下一部分「,你的意思是你只希望下一次傳遞這些數據,對嗎? :

Array (
    [0] => Array (
     [id] => advshipper 
     [module] => Shipping 
     [tax] => 20 
    ) 
) 

這是你想要的嗎?

$newArray = array(); 

foreach ($quotes_array[0] as $items) 
{ 
    if (!is_array($items)) 
    { 
     $newArray[] = $items; 
    } 

} 

$newArray將包含該數據。

UPDATE

好吧,疑難雜症。 你可以使用這個:

$newArray = $quotes_array[0]['methods'][0];

+0

我只需要[0] =>數組([id] => 1-0-0 [title] =>交易運輸[cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 – 2013-05-02 17:29:36

+0

更新了我的答案 – rinchik 2013-05-02 19:57:59

+0

這是完美的,我可以問你,我將如何去顯示所有除第一,即剝離上述結果,以便其餘的仍然在陣列 – 2013-05-02 22:03:05