コード例 #1
0
ファイル: script.php プロジェクト: salmander/array_level
function arrayLevel($arr)
{
    #echo 'Current $arr:'. print_r($arr, true) . PHP_EOL;
    $level = 1;
    if (is_array($arr)) {
        foreach ($arr as $val) {
            if (is_array($val)) {
                $level = arrayLevel($val) + 1;
            }
        }
    }
    return $level;
}
コード例 #2
0
ファイル: model.class.php プロジェクト: sujinw/webPHP
 public function addAll($data = NULL)
 {
     if (is_null($data)) {
         $this->add();
     }
     if (arrayLevel($data) > 1) {
         $sqls = "";
         foreach ($data as $v) {
             if (arrayLevel($v) == 1) {
                 //$result = $this->add($v);这等于轮询数据库,占用内存太多,优化如下
                 $fields = '';
                 $values = '';
                 foreach ($v as $k => $e) {
                     $fields .= '`' . $this->_safe_str($k) . '`,';
                     $values .= "'" . $this->_safe_str($v) . "',";
                 }
                 $fields = trim($fields, ',');
                 $values = trim($values, ',');
                 $sqls .= "INSERT INTO " . $this->table . " (" . $fields . ") VALUES (" . $values . ");";
             } else {
                 #$result = $this->addAll($v);
             }
         }
         echo $sqls;
         die;
     } else {
         $result = $this->add($data);
     }
     return $result;
 }
コード例 #3
0
ファイル: ScriptTest.php プロジェクト: salmander/array_level
 public function testArrayLevel6Array()
 {
     $arr1 = ["1", "2", "3" => ["hier2" => ["hier3" => ["2"]]], "4", "5", "6" => ["1", "2", "3", "4" => ["1", "2" => ["1", "2" => ["1", "2", "3" => ["1"]]]]]];
     $this->assertEquals(6, arrayLevel($arr1));
 }
コード例 #4
0
ファイル: Model.class.php プロジェクト: sujinw/webPHP
 public function addAll($data = NULL)
 {
     if (is_null($data)) {
         $this->add();
     }
     if (arrayLevel($data) > 1) {
         foreach ($data as $v) {
             // p($v);
             // p(arrayLevel($v));
             if (arrayLevel($v) == 1) {
                 // echo 1;die;
                 $result = $this->add($v);
             } else {
                 $result = $this->addAll($v);
             }
         }
     } else {
         $result = $this->add($data);
     }
     return $result;
 }