logo
down
shadow

How to break a long array into smaller arrays?


How to break a long array into smaller arrays?

By : Erisin
Date : November 22 2020, 10:31 AM
it should still fix some issue So, I have tried using array_slice and either I don't know what I am doing with it, or I need another solution. I have this long array that is being returned from a database. I need to break it into smaller arrays of sequential six elements so as to apply the right prices with the correct items. I realize that there is no data in many of the array elements. That is ok. Those represent where there isn't a price for a particular size with the particular item. If someone can point me in the right direction, it will be greatly appreciated. Here is my array from a print_r. , This should work for you:
code :
<?php

    //As an example
    $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

    //Splits the array into groups of 6
    $array = array_chunk($array, 6);
    print_r($array);

?>
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [1] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
            [3] => 10
            [4] => 11
            [5] => 12
        )

)


Share : facebook icon twitter icon
How to break an array into a specified number of smaller arrays?

How to break an array into a specified number of smaller arrays?


By : Yasser Khacha
Date : March 29 2020, 07:55 AM
around this issue Use the array_chunk function: http://php.net/manual/en/function.array-chunk.php
code :
$numberOfSmallerArrays = 4;
$arrayOfSmallerArrays = array_chunk($largeArray, ceil(count($largeArray) / $numberOfSmallerArrays));
Break up big array into smaller arrays, or break up a big text string into smaller strings - javascript

Break up big array into smaller arrays, or break up a big text string into smaller strings - javascript


By : user3587878
Date : March 29 2020, 07:55 AM
To fix this issue To split up a string, use: string.split. If you want to split a string which is separated by a comma, use string.split(',').
When you want to split a string in parts of 10 characters, use:
code :
var string = "15dsck3c rando mstring esjldf bjk";
var match = string.match(/[\S\s]{10}|[\S\s]{0,9}$/g); //{10} = 10 characters;
var listOfWords = Array.apply(null, match);
var arr = ["big", "small", "huge", "wall", "of", "text", "etc", "etc"];
var newList = [];
for(var i=0, len=arr.length; i<len; i+=10){
    newList.push(arr.slice(i));
}
//newList is an array consisting of smaller arrays, eg 
// [["big", <9 other elements>], [<10 elements>], ...]
How to Break Array into Smaller Arrays?

How to Break Array into Smaller Arrays?


By : Kelly
Date : March 29 2020, 07:55 AM
hop of those help? I need to break an array into smaller arrays using a deliminator such as a space, not using a certain numbers of chars (therefore array_chunk will not work for this and these answers How to break an array into a specified number of smaller arrays? is not relevant for the same reason).
code :
php> $array1 = array("One two three","four five six","seven eight nine");

php> =array_map(function($a){return explode(' ',$a);},$array1)
array(
  0 => array(
    0 => "One",
    1 => "two",
    2 => "three",
  ),
  1 => array(
    0 => "four",
    1 => "five",
    2 => "six",
  ),
  2 => array(
    0 => "seven",
    1 => "eight",
    2 => "nine",
  ),
)
php> $array1 = array("One two three","four five,six","seven,eight nine");

php> =array_map(function($a){return preg_split('/[ ,]/',$a);},$array1)
array(
  0 => array(
    0 => "One",
    1 => "two",
    2 => "three",
  ),
  1 => array(
    0 => "four",
    1 => "five",
    2 => "six",
  ),
  2 => array(
    0 => "seven",
    1 => "eight",
    2 => "nine",
  ),
)
How do I break an array into smaller arrays PHP

How do I break an array into smaller arrays PHP


By : user2877854
Date : March 29 2020, 07:55 AM
wish helps you This is what I want to do. I have an array that I then want to split up into all the elements that it has. For example I have:
code :
<?php
$originalArray = array(1,2,3,4,5);
$temp = array();
$i=1;
foreach ($originalArray as $elem) {
  $temp['array' . $i] = $elem;
  $i++;
}
extract($temp);
print_r($array1);echo '<br/>';
print_r($array2);echo '<br/>';
print_r($array3);echo '<br/>';
print_r($array4);echo '<br/>';
print_r($array5);echo '<br/>';
Break an array into smaller arrays, using a specific character as a break point

Break an array into smaller arrays, using a specific character as a break point


By : Fatima Zawar
Date : October 13 2020, 07:00 PM
hope this fix your issue you can iterate over your array and fill a temp-array with the content, when you find your special split-character, you can push the temp array into a result-array, empty the temp-array and then continue the loop.
code :
let data = [ 3, 8, 18, '-', 19, 3, 8, 20, 19, 3, 8, '-', 22 ];

let result = [];
let temp = [];

for (let item of data) {
  if (item == '-') {
    result.push(temp);
    temp = [];
  } else {
    temp.push(item);
  }
}
result.push(temp);

console.log(result);
Related Posts Related Posts :
  • How to add Smarty templating engine to CodeIgniters 3 framework?
  • Am I maxing out possible data insert
  • Redirect to homepage without changing address bar URL
  • What is the best user-Id Value for a MySql "users" table
  • PHP $_COOKIE is only available in one directory
  • php regular expressions documantation
  • cannot import too large sql file to mysql
  • not getting appilcation/json in content-type in header
  • Only the first data display from my database but other shows but not on a table
  • Is it possible to have a print button link as the content of the mail
  • how to extract full mail address in imap php
  • Alternative to output buffering to put eval'd code into a variable
  • how to run wordpress php snippet mysql update command from ssh and/ultimately cron
  • how to get xml tag by name
  • ModSecurity maximum post limits (PCRE limit errors)
  • Call controller/action in event listener
  • How to improve performance of contacting WebService?
  • PHP nest variable in echoed string that contains a HTML tag in the end
  • Multiple Ajax request for PHP framework
  • Debug Info from Moodle Plugin
  • passing variables through page
  • Passing PHP $result data to Javascript
  • cakePHP File Download was not found or not readable
  • batch waiting until script finishes
  • PHP variables and anchors in URL
  • php eTag generation using php
  • How to check whether the array is an Integer or Not?
  • Is it possible to install Doctrine without PEAR or Composer? If so, how?
  • Call view script of (parent) Abstract controller Zend
  • Curl PHP cannot display amazon
  • Symfony, getters and setters vs magic methods
  • Using 'continue' PHP instruction outside the loop
  • AJAX POST return data not appearing
  • Can I query relations using an INNER JOIN instead of two queries in Eloquent?
  • Looping through dynamic form fields and inserting into database
  • My php code can't select mysql auto_increment value
  • Store Angularjs form data in database using php
  • I want to run my sh file continuously even if I close my Putty connection
  • file_get_contents equivalent for gzipped files
  • Include PHP file with jQuery
  • php curl headers do not return from website?
  • How to find out, if facebook ID is a user, group or page
  • Connect webhost database to android database
  • preg_match get div content with class
  • Upload multiple files in Laravel 4
  • Count array numbers in multidimensional array
  • PHP Date diff with a difference
  • Search Customer by custom field in Netsuite
  • Is it possible to hide/encode/encrypt php source code and let others have the system?
  • list items to be displayed using php code and array
  • check if row exists mysqli
  • PHP errors loading MySQL
  • setup PostgreSQL with Laravel in MAMP
  • PHP - CodeIgniter Notifications
  • Encrypting a password column in a SQL database
  • New to PHP, trying to extract information from another website
  • JavaScript AmChart to Image for Email
  • Is javascript validation enough to keep my forms secure?
  • Regex extract variables from [shortcode]
  • Download a .mp3 link and save it to computer with PHP
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org