本文的目的是介绍php–切片数组的一部分并按值x引发数字键的详细情况,特别关注php切割数组的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解php–切片数组的一部分并
本文的目的是介绍php – 切片数组的一部分并按值x引发数字键的详细情况,特别关注php 切割数组的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解php – 切片数组的一部分并按值x引发数字键的机会,同时也不会遗漏关于angular – 在Ionic Storage键/值对中更新数组的一部分、c# – 将byte []数组的一部分复制到PDFReader中、javascript检查元素是否包含数组的一部分、PHP array_slice():截取数组的一部分的知识。
本文目录一览:- php – 切片数组的一部分并按值x引发数字键(php 切割数组)
- angular – 在Ionic Storage键/值对中更新数组的一部分
- c# – 将byte []数组的一部分复制到PDFReader中
- javascript检查元素是否包含数组的一部分
- PHP array_slice():截取数组的一部分
php – 切片数组的一部分并按值x引发数字键(php 切割数组)
$ar = [0 => 'a',1 => 'b',2 => 'c',3 => 'd',4 => 'e',5 => 'f',6 => 'g'];
和定义的偏移量为4($offset = 4).现在,您想要从偏移量开始切片该数组的一部分.
$slice = array_slice($ar,$offset,null,true);
而且您不仅要保留原始密钥,而且实际上将它们提高1,因此结果将是:
Array ( [5] => e [6] => f [7] => g )
代替
Array ( [4] => e [5] => f [6] => g )
当然,你可以循环遍历数组(foreach,array_walk)并重新分配所有键,例如:
$new_ar = []; $raise_by = 1; // Could be any other amount foreach ($slice as $key => $val) { $new_ar[$key + $raise_by] = $val; }
但有没有办法在没有额外的外部循环和(重新)分配密钥的情况下做到这一点?像“在x位置切割数组并用x 1开始键”之类的东西?
编辑/可能的解决方案:
受到评论的启发,除了Brian在How to increase by 1 all keys in an array?的评论之外,我还看到了两种可能的解决方案
静态,短期和基本:
array_unshift($ar,0); $result = array_slice($ar,$offset + 1,true);
更灵活,但性能可能更差:
$shift = 1; $slice = array_slice($ar,true); $ar = array_merge(range(1,$offset + $shift),array_values($slice)); $result = array_slice($ar,$offset + $shift,true);
优点是可以将键移动任意值.
解决方法
$ar = [0 => 'a',6 => 'g']; $offset = 4; $slice = array_slice($ar,true); $slice = array_combine(range($offset+1,count($slice)+$offset),array_values($slice));//<--------check this echo "<pre>"; print_r($slice);
angular – 在Ionic Storage键/值对中更新数组的一部分
键:settingsJSON
值:{toggleDates:false,toggleIndicator:false,toggleRepayments:false}
有没有办法更新数组的一部分(即设置toggleDates为true)而不覆盖存储在值中的其余数组?
我试过了:
let settingstemp = JSON.stringify({toggleDates: true}); this.storage.set('settingsJSON',settingstemp)
但是这会将整个数组更新为{toggleDates:true}.
我也尝试过:
this.storage.set('settingsJSON.toggleDates',true)
但这只是创建一个名为settingsJSON.toggleDates的新键/值.
解决方法
// Get the entire data this.storage.get('settingsJSON').then(valueStr => { let value = JSON.parse(valueStr); // Modify just that property value.toggleDates = true; // Save the entire data again this.storage.set('settingsJSON',JSON.stringify(value)); });
c# – 将byte []数组的一部分复制到PDFReader中
How do you refill a byte array using SqlDataReader?
所以我有一个设置大小的字节数组,对于这个例子,我会说新字节[400000].在这个数组中,我将放置不同大小的pdf(小于400000).
伪代码将是:
public void Run() { byte[] fileRetrievedFromDatabase = new byte[400000]; foreach (var document in documentArray) { // Refill the file with data from the database var currentDocumentSize = PopulateFileWithPDFDataFromDatabase(fileRetrievedFromDatabase); var reader = new iTextSharp.text.pdf.PdfReader(fileRetrievedFromDatabase.Take((int)currentDocumentSize ).ToArray()); pageCount = reader.NumberOfPages; // DO ADDITIONAL WORK } } private int PopulateFileWithPDFDataFromDatabase(byte[] fileRetrievedFromDatabase) { // DataAccessCode Goes here int documentSize = 0; int bufferSize = 100; // Size of the BLOB buffer. byte[] outbyte = new byte[bufferSize]; // The BLOB byte[] buffer to be filled by GetBytes. myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess); Array.Clear(fileRetrievedFromDatabase,fileRetrievedFromDatabase.Length); if (myReader == null) { return; } while (myReader.Read()) { documentSize = myReader.GetBytes(0,null,0); // Reset the starting byte for the new BLOB. startIndex = 0; // Read the bytes into outbyte[] and retain the number of bytes returned. retval = myReader.GetBytes(0,startIndex,outbyte,bufferSize); // Continue reading and writing while there are bytes beyond the size of the buffer. while (retval == bufferSize) { Array.copy(outbyte,fileRetrievedFromDatabase,retval); // Reposition the start index to the end of the last buffer and fill the buffer. startIndex += retval; retval = myReader.GetBytes(0,bufferSize); } } return documentSize; }
以上代码的问题是,当我尝试访问PDF阅读器时,我一直收到“未找到重建预告片.原始错误:未找到PDF startxref”错误.我相信这是因为字节数组太长并且尾随0.但由于我正在使用字节数组,所以我不会在LOH上不断构建新对象,我需要这样做.
那么我如何获得我需要的数组并将其发送到PDFReader呢?
更新
所以我查看了源代码,发现我的实际代码中有一些变量令人困惑.我基本上在循环的每次迭代中重用fileRetrievedFromDatabase对象.由于它通过引用传递,它被清除(设置为全零),然后填充PopulateFileWithPDFDataFromDatabase.然后,此对象用于创建新PDF.
如果我没有这样做,将在每次迭代中创建一个新的大字节数组,并且Large Object Heap会变满并最终抛出OutOfMemory异常.
解决方法
>将您的缓冲区视为circular buffer,其中包含两个开始和结束位置的索引.
需要一个写在outByte中的最后一个字节的索引,当你到达那个索引时你必须停止读取.
>只需读取与数据数组中相同的字节数,以避免读入不属于同一文件的缓冲区的“未知”部分.
换句话说,不要将bufferSize作为最后一个参数,而是使用data.Length.
// Read the bytes into outbyte[] and retain the number of bytes returned. retval = myReader.GetBytes(0,data.Length);
如果数据长度为10且你的outbyte缓冲区为15,那么你应该只读取data.Length而不是bufferSize.
但是,我仍然没有看到你如何重复使用outbyte“缓冲区”,如果那就是你正在做的事情……我根本就没有根据你在答案中提供的内容进行跟踪.也许你可以准确地澄清什么是重复使用.
javascript检查元素是否包含数组的一部分
var ALLOW_subnet = ['192.168.1.','192.168.2.','192.168.3.','192.168.4.'];
我可以使用我自己的功能获取PC Client的IP地址:
getIPClient() var ipclient = input.getIPClient();
我的问题是如何检查客户端IP是否在我允许的子网内,我试图使用indexOf()函数,但结果是错误的.
例如:
if IP Client is 192.168.1.115 => allow if IP Client is 192.168.5.115 => deny.
解决方法
Array#some
,并检查ALLOW_subnet的一部分是否在位置0的ip内.
function check(ip) { return ALLOW_subnet.some(function (a) { return !ip.indexOf(a); }); } var ALLOW_subnet = ['192.168.1.','192.168.4.']; console.log(check('192.168.1.115')); console.log(check('192.168.5.115'));
ES6与String#startsWith
function check(ip) { return ALLOW_subnet.some(a => ip.startsWith(a)); } var ALLOW_subnet = ['192.168.1.','192.168.4.']; console.log(check('192.168.1.115')); console.log(check('192.168.5.115'));
PHP array_slice():截取数组的一部分
PHP array_slice() 函数用来截取数组,也就是从数组中提取出一个片段,语法如下:array array_slice ( array $arr,int $start [,int $length = NULL [,bool $preserve_keys = false ]] )
参数说明:- arr 表示要截取的数组。
-
start 表示开始截取的位置(下标):
- 如果 start 为正数,则从前往后截取。
- 如果 start 为负数,则从距离 arr 末端 -start 的位置开始,从后往前截取。例如 -2 意味着从数组的倒数第二个元素开始。
-
length 是可选参数,表示截取长度:
- 如果 length 为正数,那么表示截取的元素个数;
- 如果 length 为负数,那么截取的片段将终止在距离数组末端 length 的位置;
- 如果省略,那么将从 start 位置开始,一直截取到数组的末尾。
- preserve_keys 是可选参数,规定是否保留原来的键名,默认为 false,也即不保留;如果设置为 true,将保留原有的键名。
返回值:返回截取后的子数组。
使用示例如下:
<?PHP $info = array(2=>"C语言中文网",4=>"http://c.biancheng.net/","PHP","JavaScript","Java","Python","C++"); //截取得到 ["PHP","JavaScript","Java","Python","C++"] print_r( array_slice($info,2) ); //截取得到 ["JavaScript","Python"] print_r( array_slice($info,-2,3) ); //截取得到 ["C语言中文网","http://c.biancheng.net/","PHP"] print_r( array_slice($info,3) ); //保留数组的原始键名 print_r( array_slice($info,3,true) ); ?>执行以上程序的输出结果为:
Array
(
[0] => PHP
[1] => JavaScript
[2] => Java
[3] => Python
[4] => C++
)
Array
(
[0] => Python
[1] => C++
)
Array
(
[0] => C语言中文网
[1] => http://c.biancheng.net/
[2] => PHP
)
Array
(
[2] => C语言中文网
[4] => http://c.biancheng.net/
[5] => PHP
)
关于php – 切片数组的一部分并按值x引发数字键和php 切割数组的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于angular – 在Ionic Storage键/值对中更新数组的一部分、c# – 将byte []数组的一部分复制到PDFReader中、javascript检查元素是否包含数组的一部分、PHP array_slice():截取数组的一部分的相关信息,请在本站寻找。
本文标签: