legend2---开发日志3(thinkphp的入口目录是public的体现是什么)

legend2---开发日志3(thinkphp的入口目录是public的体现是什么)

一、总结

一句话总结:需要深刻理解程序的入口和入口位置都在public目录这里,比如读写文件的初始目录都在这,获取js,css,图片资源

读写文件初始目录为public
获取js/css/图片的初始目录为public

1、php合并数组有哪些方法?

array_merge() ‘+'号 array_merge_recursive

array_merge(),‘+'号,和array_merge_recursive函数。

 1 $array1 = array(2,4,"color" => "red");
 2 
 3     $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
 4 
 5     $result = array_merge($array1, $array2);
 6 
 7     echo "----------------array_merge---------------".PHP_EOL;
 8 
 9     print_r($result);
10 
11     echo "----------------+++++++++++---------------".PHP_EOL;
12 
13     print_r($array1+$array2);
14 
15     echo "----------------array_merge_recursive---------------".PHP_EOL;
16 
17     print_r(array_merge_recursive($array1,$array2));

结果如下:

 1 ----------------array_merge---------------Array(
 2 
 3     [0] => 2
 4 
 5     [1] => 4
 6 
 7     [color] => green
 8 
 9     [2] => a
10 
11     [3] => b
12 
13     [shape] => trapezoid
14 
15     [4] => 4)----------------+++++++++++---------------Array(
16 
17     [0] => 2
18 
19     [1] => 4
20 
21     [color] => red
22 
23     [shape] => trapezoid
24 
25     [2] => 4)----------------array_merge_recursive---------------Array(
26 
27     [0] => 2
28 
29     [1] => 4
30 
31     [color] => Array
32 
33         (
34 
35             [0] => red
36 
37             [1] => green
38 
39         )
40 
41  
42 
43     [2] => a
44 
45     [3] => b
46 
47     [shape] => trapezoid
48 
49     [4] => 4)

对比array_merge和+以及array_merge_recursive结果的”color”的值我们可以看出:
1.对于相同的字符串索引,
array_merge则会用后面的值覆盖前面出现的值;
+会用前面出现过的值覆盖后面相同的key;
array_merge_recursive则会把相同的索引放到一个数组里面,增加数组的维度;
2.对于相同的数字索引,
array_merge则会给重复的值重建索引(索引值从0开始);
+仍然是用前面出现过的值覆盖后面的值;
array_merge_recursive和array_merge的处理方法一样。

2、php打乱一个数组?

shuffle()函数
1 <?php
2 $my_array = array("red","green","blue","yellow","purple");
3 
4 shuffle($my_array);
5 print_r($my_array);
6 ?>

3、php取一个数组的前几个?

array_slice()

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys ]] )

array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。

如果 offset 非负,则序列将从 array 中的此偏移量开始。如果 offset 为负,则序列将从 array 中距离末端这么远的地方开始。

如果给出了 length 并且为正,则序列中将具有这么多的单元。如果给出了 length 并且为负,则序列将终止在距离数组末端这么远的地方。如果省略,则序列将从 offset 开始一直到 array 的末端。

 

array_slice(array23)array,2,3)此段代码表示从array数组中从第二个开始,取三个值;

array_slice(array3)array,3)此时代码表示从array数组中第三个之后的所有数组。

 1 [php] view plain copy
 2 <?php 
 3 $input = array("a", "b", "c", "d", "e"); 
 4    
 5 $output = array_slice($input, 2);      // returns "c", "d", and "e" 
 6 $output = array_slice($input, -2, 1);  // returns "d" 
 7 $output = array_slice($input, 0, 3);   // returns "a", "b", and "c" 
 8    
 9 // note the differences in the array keys 
10 print_r(array_slice($input, 2, -1)); 
11 print_r(array_slice($input, 2, -1, true)); 
12 ?>

 

4、thinkphp数据库大于多少并且小于多少写法?

仿照多字段写法 多字段之间默认用and

因为多字段之间默认用的and,所以同字段可以仿照多字段的写法

$map_1['b_day_ts']=['>=',$beginThreeDaysBefore];
$map_1['b_day_ts']=['<=',$beginToday];

5、thinkphp使用redirect时出现trim() expects parameter 1 to be string, array given问题的原因是什么?

redirect参数不能是数组

原因是数组不能用trim方法,因为你把数组传进了他期望为字符串的参数里面

原因是参数不能传数组:第四行参数传数组不行

1 //步骤二:获取推荐题目并且按照推荐顺序算法排序
2 $question_list=appindexmodellogchoose_questionGetRecommendQuestion::getRecommendQuestion($map_category);
3 //dump($question_list);die;
4  $this->redirect('index/blog.do_question/index',$question_list);

6、选题目和做题目页面的逻辑?

【选题目页】不做找题目数据操作 【做题目页】通过模型找题目操作数据 好处是【选题目页】不必传题目数据到【做题目页】

选题目页提供选题目页的页面,然后直接跳转到做题目页的控制器,做题目页的控制器通过参数获取题目传到自己的视图。

不要在选题目也生成题目数据然后做题目页获取题目数组,这样无论是传做题目页的控制器还是视图都不方便

7、如何把题目列表数据保存到系统?

做缓存 file_put_contens() file_get_contents()
 1 <?php
 2 namespace appindexcontrollerlog;
 3 use appindexcontrollerBase;
 4 //直接访问链接为:http://legend.com/index/blog.do_question/index.html
 5 class DoQuestion extends Base
 6 {
 7     public function index()
 8     {
 9         $question_list=[];
10         $question_list_info=[];//题目列表信息数组
11         if (request()->isget()){
12             $dataIn=input('get.');
13             //dump($dataIn);
14 
15             //步骤一:当为推荐修炼方式时
16             if(isset($dataIn['blog_recommend'])){
17                 //1.1、确定文件中的$question_list数据有没有过期,如果没有过期,就取文件中的数据,如果过期了,就重新生成
18                 $question_list=appindexmodellogchoose_questionEntrance::recommend($dataIn);
19                 $result=file_put_contents('record/question_list_record.php',json_encode($question_list));
20                 dump($result);
21                 $result2=file_get_contents('record/question_list_record.php');
22                 dump($result2);die;
23                 $question_list_info['question_num']=count($question_list);
24                 $question_list_info['xiulian_type']=$dataIn['xiulian_type'];
25                 $question_list_info['blog_recommend']=$dataIn['blog_recommend'];
26                 //dump($question_list);die;
27                 //$question_list
28             }

8、thinkphp直接在控制器中$result=file_put_contents('question_list_record.php',json_encode($question_list));question_list_record文件会出现在哪个目录下

程序入口目录public目录下

直接出现在public目录下,需要深刻理解程序的入口和入口位置都在public目录这里

9、thinkphp直接在控制器中$result=file_put_contents('record/question_list_record.php',json_encode($question_list));会在public的record子目录下出现question_list_record.php

会 文件路径里面是左斜线

会,但是要注意这里是左斜线,右斜线的话不行

10、thinkphp直接在控制器中$result2=file_get_contents('record/question_list_record.php');取得是哪个路径下的文件?

public/record目录

直接是public目录下,public/record目录下的,thinkphp程序的入口就是public目录

二、内容在总结中

1、相关知识

 

2、代码

 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/10644035.html