2016-05-15 104 views
-1

我具有一個陣列,它是這樣如何從兩個或更多陣列

Array 
(
    [sch_name] => example 
    [sch_degree] => example 
    [sch_field] => example 
    [sch_grade] => example 
    [sch_from_year] => 2008 
    [sch_to_year] => 2008 
    [sch_desc] => example 
) 

&更同一陣列像上面,現在,當我另一個陣列添加到它,它看起來像示出創建多維數組下面

Array 
(
    [0] => Array 
     (
      [sch_name] => example 
      [sch_degree] => example 
      [sch_field] => example 
      [sch_grade] => example 
      [sch_from_year] => 2008 
      [sch_to_year] => 2008 
      [sch_desc] => example 
     ) 

    [1] => Array 
     (
      [sch_name] => example 
      [sch_degree] => example 
      [sch_field] => example 
      [sch_grade] => example 
      [sch_from_year] => 2008 
      [sch_to_year] => 2008 
      [sch_desc] => example 
     ) 

) 

現在的問題是在第三陣列被附加到[1]數組像下面所示

Array 
(
    [0] => Array 
     (
      [sch_name] => example 
      [sch_degree] => example 
      [sch_field] => example 
      [sch_grade] => example 
      [sch_from_year] => 2008 
      [sch_to_year] => 2008 
      [sch_desc] => example 
     ) 

    [1] => Array 
     (
      [0] => Array 
       (
        [sch_name] => example 
        [sch_degree] => example 
        [sch_field] => example 
        [sch_grade] => example 
        [sch_from_year] => 2008 
        [sch_to_year] => 2008 
        [sch_desc] => example 
       ) 

      [1] => Array 
       (
        [sch_name] => example 
        [sch_degree] => example 
        [sch_field] => example 
        [sch_grade] => example 
        [sch_from_year] => 2008 
        [sch_to_year] => 2008 
        [sch_desc] => example 
       ) 

     ) 

) 

我想新的陣列將被追加爲[2] &如果增加更多的數組,那麼它應該是這樣的[3]

這是我如何做到這一點

$post = array(); 
$post['sch_name'] = test_input($sch_name); 
$post['sch_degree'] = test_input($sch_degree); 
$post['sch_field'] = test_input($sch_field); 
$post['sch_grade'] = test_input($sch_grade); 
$post['sch_from_year'] = test_input($sch_from_year); 
$post['sch_to_year'] = test_input($sch_to_year); 
$post['sch_desc'] = test_input($sch_desc); 

$newArray = array($post, $array_from_db); 

$ array_from_db是從數據庫中提取正如我在問題開始部分所示

+0

你爲什麼不使用'array_push'? –

+0

如果'$ array_from_db'包含兩個子數組,並且新數組是'$ post',那麼只需試試'array_push($ array_from_db,$ post);'$ array_from_db'將被更新。 –

+0

當我嘗試$ newArray = array_push($ in,$ post); print_r($ newArray);出口;它打印8 –

回答

1

根據我們的討論,我們處於這個位置,你完全受益。 discussion-between-frayne-konok-and-sagar-singh,假設你有這樣的數據庫數組:

Online Check,必須檢查。

$array_from_db = array(
     array(
      "sch_name" => "example", 
      "sch_degree" => "example", 
      "sch_field" => "example", 
      "sch_grade" => "example", 
      "sch_from_year" => "2008", 
      "sch_to_year" => "2008", 
      "sch_desc" => "example" 
     ), 
     array(
      "sch_name" => "example", 
      "sch_degree" => "example", 
      "sch_field" => "example", 
      "sch_grade" => "example", 
      "sch_from_year" => "2008", 
      "sch_to_year" => "2008", 
      "sch_desc" => "example" 
     ) 
    ); 

新話題:

$post = array(); 
$post['sch_name'] = "example"; 
$post['sch_degree'] = "example"; 
$post['sch_field'] = "example"; 
$post['sch_grade'] = "example"; 
$post['sch_from_year'] = "2008"; 
$post['sch_to_year'] = "2008"; 
$post['sch_desc'] = "example"; 

array_push($array_from_db, $post); 

print_r($array_from_db); 
1

如果要將值附加到數組中,只需使用$array[] = $newArray;這會將新項目添加爲最後一項;

$post = array(); 
$post['sch_name'] = test_input($sch_name); 
$post['sch_degree'] = test_input($sch_degree); 
$post['sch_field'] = test_input($sch_field); 
$post['sch_grade'] = test_input($sch_grade); 
$post['sch_from_year'] = test_input($sch_from_year); 
$post['sch_to_year'] = test_input($sch_to_year); 
$post['sch_desc'] = test_input($sch_desc); 

$array_from_db[] = $post; 
1

您只需要一個容器數組來包含來自數據庫的所有數組。但是

$newArray = array($post, $array_from_db); 

這將創建一個新的數組,而不是使用容器。

爲您的代碼:

// declare a container 
$container = array(); 
$post = array(); 
$post['sch_name'] = test_input($sch_name); 
$post['sch_degree'] = test_input($sch_degree); 
$post['sch_field'] = test_input($sch_field); 
$post['sch_grade'] = test_input($sch_grade); 
$post['sch_from_year'] = test_input($sch_from_year); 
$post['sch_to_year'] = test_input($sch_to_year); 
$post['sch_desc'] = test_input($sch_desc); 


// push $post and $array_from_db to the $container 
array_push($container,$post,$array_from_db); 

順便說一句,如果你只是想增加新的元素添加到陣列中,你可以簡單地使用:

$container[] = $myElement; 

它還保存調用一個函數(array_push )。