相关疑难解决方法(0)

在PHP 7中全局启用'strict_types'

我目前正在将我的网站从PHP5迁移到PHP7,并且我已经开始使用添加的严格输入功能.但是,这需要我使用以下行开始所有文件:

<?php declare(strict_types=1);

// All other code here
// ...
Run Code Online (Sandbox Code Playgroud)

所以我想知道,是否有任何方法可以strict_types使用像php.iniapache配置文件那样全局启用,所以我不必每次都写这行,如果是这样,我怎么能启用它?

php apache variable-types php-7

37
推荐指数
3
解决办法
1万
查看次数

将逗号分隔的整数分解为intvals?

说我有一个像这样的字符串$thestring = "1,2,3,8,2".

如果我explode(',', $thestring),我得到一个字符串数组.如何将它分解为整数数组呢?

php integer explode

11
推荐指数
2
解决办法
1万
查看次数

在PHP中将字符串数组转换为整数数组

我跟着这个这个问题中的线索.

我试图将输入的数字流转换为整数数组.代码应该是自我解释的.

$handle = fopen("php://stdin","r");

print("Enter space separated numbers to be made to an array\n");

$numStream = fgets($handle);

print("Creating array from : {$numStream}\n");

//Using explode to create arrays
//now we have an array of Strings
$numArray = explode(" ", $numStream);

var_dump($numArray);

print(getType($numArray[0]));
print("\n");
array_walk($numArray, 'intval');
print(getType($numArray[1]));
print("\n");
var_dump($numArray);
print_r($numArray);
Run Code Online (Sandbox Code Playgroud)

我试图转换String数组,

array_walk($numArray, 'intval')
Run Code Online (Sandbox Code Playgroud)

最后两个打印块在转换之前和之后打印数组元素的类型.

两种情况下的输出都是字符串

string
string
Run Code Online (Sandbox Code Playgroud)

我想知道这里发生了什么?可能..

  1. 转换是错误的
  2. 如何检查类型是错误的

或者两者都可能.

添加完整的输入和输出,

$ php arrays/arrayStringToInteger.php 
Enter space separated numbers to be made to an array
1 1 …
Run Code Online (Sandbox Code Playgroud)

php arrays

3
推荐指数
1
解决办法
2万
查看次数

在PHP中将字符串转换为数组

我从ajax获取了字符串,并在url中将字符串传递为1,2,3,4,5,6

使用爆炸功能后,即$explode_id = explode(',', $request->data); 我得到的输出为[“ 1”,“ 2”,“ 3”,“ 4”,“ 5”,“ 6”]

但我希望数据为:[1,2,3,4,5,6]

我如何得到它?而且它不应该像[“ 1,2,3,4,5,6”],因为我想比较以下数据:

  $product_id = Product::where('name', 'like','%'.$main.'%')->where('id', $explode_id)->pluck('id');
Run Code Online (Sandbox Code Playgroud)

我在这里得到了答案。正在像URL中传递我要匹配的ID

$("input[type='checkbox']").click(function() {
  if ($(this).is(':checked')) {
   var days = $(this).val();
   $.ajax({
        type: "get",
        url: '/getcreatedat',
        data: { 'days': days },
        success: successFunc,
            error: errorFunc
        });
        function successFunc(data, status) {
          var pageURL = $(location).attr("href");
          window.location.href = pageURL+ '/' +data;
        }   
        function errorFunc(xhr, error) {
             console.log(error);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

下次我单击该功能时:url传递id的时间翻倍

php laravel

3
推荐指数
2
解决办法
1万
查看次数

将数组中的所有数字值转换为整数类型,否则将非数字值保留为字符串

给定一个数组,如何将从 key [1] 开始的数组从 string 类型转换为 int 类型?

Array
(
  [0] => "id"
  [1] => "6086220176"
  [2] => "6542925762"
  [3] => "6623113406"
  [4] => "6702782948"
)
Run Code Online (Sandbox Code Playgroud)

我已经检查了相关问题如何将数组值从字符串转换为整数?已经,但我想跳过第一个键“id”进行转换,而不是数组中的所有键!

php arrays integer casting

2
推荐指数
1
解决办法
2万
查看次数

PHP - 在list()参数中将字符串转换为int?

我正在改进我的代码,我有几个地方需要将字符串转换为整数,但list()函数的限制正在阻止我使用list().具体例子:

$x = '12-31-2010';

$explode = explode("-", $x);

// Need to do the following because e.g. echo gettype($explode[0]) ---> string

$month = (int)$explode[0];
$day   = (int)$explode[1];
$year  = (int)$explode[2];
Run Code Online (Sandbox Code Playgroud)

我想做什么(但得到一个致命的错误)让事情变得更加整洁:

list((int)$month, (int)$day, (int)$year) = explode("-", $x); // I want echo(gettype) ---> integer for each variable
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点,或者我能做到以下几点是最好的?

list($month, $day, $year) = explode("-", $x);

$month = (int)$month;
$day   = (int)$day;
$year  = (int)$year;
Run Code Online (Sandbox Code Playgroud)

php casting

2
推荐指数
1
解决办法
1152
查看次数

在php中将字符串数组转换为整数数组

我有一个数组:

$TaxIds=array(2) { [0]=> string(1) "5" [1]=> string(2) "10" } 
Run Code Online (Sandbox Code Playgroud)

我需要转换为:

$TaxIds=array(2) { [0]=> int(5) [1]=> int(10) } 
Run Code Online (Sandbox Code Playgroud)

简单的方法???

php arrays

1
推荐指数
2
解决办法
1800
查看次数

如何将带数字的字符串转换为具有整数元素的数组?

我有一个这样的字符串:'3,6,43,8'.

我可以轻松地将它转换为数组:

$newArray = explode( ',', $string );
Run Code Online (Sandbox Code Playgroud)

但由于我需要数组中的整数元素,我应该将每个数组元素转换为整数:

foreach( $newArray as $key => $sNumber ) {
    $newArray[ $key ] = intval( $sNumber );
}
Run Code Online (Sandbox Code Playgroud)

有没有办法直接将数组元素转换为整数而不是字符串?

php arrays

0
推荐指数
1
解决办法
157
查看次数

标签 统计

php ×8

arrays ×4

casting ×2

integer ×2

apache ×1

explode ×1

laravel ×1

php-7 ×1

variable-types ×1