How do I copy data from 1d array to 2d array?
Suppose
there is an array namely a[10000] with values like
[ 1,2,3,55,66,77,88, ... ... , 9999]
values may be repeated ...
Now I want to convert this array which holds 10,000 values right now into 2 dimensional array
that has 100 rows and 100 columns.
After that
Is it possible to implement a SELECTION SORT on a 2 dimensional array?
for($i = 0; $i<100; $i++)
{
for($j = 0; $j<100; $j++)
{
$indice = $j + 100 * $i;
$newArray[ $i ][ $j ] = $oldArray[ $indice ];
}
}
You can try something like this:
assuming $a = array(1,2,3,4,5....1000)
$b = 0;
$c = 0;
foreach ($a as $new_number){
if ($b < 100){
$b ++;
} else {
$b = 1;
$c ++;
}
$new_array[$c][$b] = $new_number;
}
Define two loops and do some multiplication
for ($i=0; $i< 100; $i++) {
for ($j=0; $j< 100; $j++) {
$twoDarray[$i][$j] = $oneDarray[$i*100 + $j];
}
}
For sorting a multidimensional array you use array_multisort.
Here's the manual page: http://www.php.net/manual/en/function.array-multisort.php
Just more one variant:
foreach($a as $k=>$v){
$b[(int)($k/100)][$k%100] = $v;
}
Related
How do I return all possible combinations [12345], [12354] up to [54312], [54321] without having to run 120 for...loop as in the case of combining a 2-item array in the code below?
To return all possible combinations from the given array $word = [1,2],
//break the array into 2 separate arrays
$arr1 = $word[0]; $arr2 = $word[1];
//computer for first array item...each item will have 2 loops
for($i=0; $i<count($arr1); $i++){
for($j=0; $j<count($arr2); $j++){
$ret = $arr1[$i] . $arr2[$j]; array_push($result, $ret);
}
}
//computer for second array item..each item will have 2 loops
for($i=0; $i<count($arr2); $i++){
for($j=0; $j<count($arr1); $j++){
$ret = $arr2[$i] . $arr1[$j]; array_push($result, $ret);
}
}
//display the result
for ($i = 0; $i < count($result); $i++){
echo result([$i];
}
The above code works well.
But for a 5-item array [1,2,3,4,5], it will require about (5 items * 24 loops) = 120 loops.
As seen, you wanted to split 2 strings into chars and obtain all combination by 2 chars: first form blank1 and second from blank2.
Instead of doing the combination manually use a regular for-loop.
$result = array();
for ($i = 0; $i < count($blank1); $i++)
{
for ($j = 0; $j < count($blank2); $j++)
{
//set combination
$aux = $blank1[$i].$blank2[$j];
array_push($result, $aux);
}
}
//result should be populated with combination of 2
//just list it and use as need
for ($i = 0; $i < count($result); $i++)
{
echo $result[$i];
}
//same with stored or checking on db : use loops
For multiple combination, use more nested loops
eg: [blank1][blank2][blank1] - 3 combination
$result = array();
//1
for ($i = 0; $i < count($blank1); $i++)
{
//2
for ($j = 0; $j < count($blank2); $j++)
{
//3
for ($k = 0; $k < count($blank1); $k++)
{
//set combination
$aux = $blank1[$i].$blank2[$j].$blank1[$k];
array_push($result, $aux);
}
}
}
Same as any number you wanted ! It will be a little annoying if have to write many loops but note while can be used with an adequate algorithm. But for the moment just keep as simple as you can and get the desired result.
How to sort an array with a function manually in alphabetical order?
Without using automatic sort such as (sort, asort, usort, ...)
I've tried the code below so far but I feel like there is another way to do it
<?php
function sort_arrays(array $var) {
for ($i=0; $i < 4; $i++) {
print_r($var);
}
else {
return null;
}
}
sort_arrays($array = array("A_first","D_last","B_second","C_third"));
// take an array with some elements
$array = array('a','z','c','b');
// get the size of array
$count = count($array);
echo "<pre>";
// Print array elements before sorting
print_r($array);
for ($i = 0; $i < $count; $i++) {
for ($j = $i + 1; $j < $count; $j++) {
if ($array[$i] > $array[$j]) {
$temp = $array[$i];
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
echo "Sorted Array:" . "<br/>";
print_r($array);
$arr = ['c','a','d','b'];
$size =count($arr);
for($i=0; $i<$size; $i++){
/*
* Place currently selected element array[i]
* to its correct place.
*/
for($j=$i+1; $j<$size; $j++)
{
/*
* Swap if currently selected array element
* is not at its correct position.
*/
if($arr[$i] > $arr[$j])
{
$temp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $temp;
}
}
}
print_r($arr);
$arr = array('Apple', 'Banana', 'Chips', 'Dr.Pepper'); // Create a sorted array manually
print_r($arr); // prints a manually sorted array
My for loop should be adding 9 values to an array, but for some reason stops on 6. This only happens when I use the square bracket syntax to add the key and value to the array. Here's the code:
$sentences = $this->sentences($sentence);
$n = count($sentences);
echo $n;
$values = array();
for($i = 0; $i < $n; $i++){
$s1 = $sentences[$i];
for($j = 0; $j < $n; $j++){
$s2 = $sentences[$j];
$values[$i][$j] = $this->checkvalues($s1,$s2);
}
}
$sentences_dic = array();
$other = array();
$otherTwo = array();
for($i = 0; $i < $n; $i++){
$score = 0;
for($j = 0; $j < $n; $j++){
$score = $score+$values[$i][$j];
}
$other[$i] = $score;
$otherTwo[$i] = $sentences[$i];
$sentences_dic[($sentences[$i])]=$score;
var_dump($otherTwo);
}
//maybe need
return $sentences_dic;
I am not sure why this is happening. The array is only printing
Here is what it is printing. It should be printing the seventh, eigth and ninth terms but it isnt. All the terms it isn't adding were all able to print in the other and otherTwo arrays.
Array
(
[first] => banana
[second] => banana2
[third] => banana3
[fourth] => banana4
[fifth] => banana5
[sixth] => banana6
)
when it should be printing all 9 values and keys. I don't understand why I am able to add all sentences and scores to their full extent(9) to two seperate arrays, but when I try to use square bracket syntax it only goes to 6.
Given your output $sentences appears to be an array and has duplicates. Array keys must be unique and duplicate keys are overwritten.
You could do something like this for a multidimensional array:
if(isset($sentences_dic[$sentences[$i]])) {
$sentences_dic[$sentences[$i]][] = $score;
} else {
$sentences_dic[$sentences[$i]] = $score;
}
Or to sum the scores of duplicates:
if(isset($sentences_dic[$sentences[$i]])) {
$sentences_dic[$sentences[$i]] += $score;
} else {
$sentences_dic[$sentences[$i]] = $score;
}
I have code like this
$jumlahcolspan = array();//new array
$horizontaldeep = 5;
$level = array(5,4,3,8,7);//old array
for ($j = 0; $j < $horizontaldeep; $j++) {
$jml = 1;
for ($i = $j + 1; $i < $horizontaldeep; $i++) {
$jml = $level[$i] * $jml;
}
array_push($jumlahcolspan, $jml);
}
To put it simple, what I want to get is to multiply old array value which index start from $i+1 to the last and push it to another array.
So, its some thing like this
old array: [5, 4, 3, 8, 7]
new array: [4*3*8*7, 3*8*7, 7, 1]
I've tried this but it doesn't work also
for ($j = 0; $j < $horizontaldeep; $j++) {
$jml = 1;
for ($i = $j + 1; $i < $horizontaldeep; $i++) {
global $jml;
$jml = $level[$i] * $jml;
}
array_push($jumlahcolspan, $jml);
}
Tried this too but not work also.
for ($j = 0; $j < $horizontaldeep; $j++) {
array_push($jumlahcolspan, array_product(array_slice($level, $j+1)));
}
Note: now I'm reviewing my full code. May be something not right in my code.
I think the problem is related to $jml variable but I can't figure how to solve that. Can anyone help me?
One approach would be to use a Recursive Function to achieve that goal. The Recursive Function below demonstrates how. And, by the way, you may as well quick-test it here.
<?php
$oldArray = [5,4,3,8,7];
function arrayMatrixMultiply(array $old, array &$newArray=[]){
$result = 1;
foreach($old as $key=>$value){
if($key != 0){
$result*=$value;
}
}
$newArray[] = $result;
array_splice($old, 0, 1);
if(!empty($old)){
// JUST RECURSE TILL THE $oldArray BECOMES EMPTY
arrayMatrixMultiply($old, $newArray);
}
return $newArray;
}
$newArray = arrayMatrixMultiply($oldArray);
var_dump($newArray);
// PRODUCES::
array (size=5)
0 => int 672
1 => int 168
2 => int 56
3 => int 7
I am trying to find duplicated values/string in an array using for loop
<?php
$b=array('a','b','c','a','b');
$c=count($b);
$d=array();
for($i=0;$i<=($c-1);$i++)
{
for($j=1;$j<=($c-1);$j++)
{
if($b[$i]!=$b[$j])
{
$flag=1;
}
}
if($flag==1)
{
$d[$i]=$b[$i];
}
}
print_R($d);
?>
where is my mistake? I have used array $d to display non duplicate values.....
NOTE: I need to try this only with for loop - I know how to do it using array functions.
You should reverse your test, because there are almost always values, which are different from the one you're testing. And you must reset your $flag before the inner loop, otherwise it will always be true.
When you want to find unique values, you can just test against $d only. If the value is already in $d, skip it.
$c1 = count($b);
for ($i = 0; $i < $c1; $i++) {
$dup = 0;
$c2 = count($d);
for ($j = 0; $j < $c2; $j++) {
if ($b[$i] == $d[$j])
$dup = 1;
}
if (!$dup)
$d[] = $b[$i];
}
print_r($d);
If you want to find values, which don't have duplicates instead
for ($i = 0; $i < $c; $i++) {
$dup = 0;
for ($j = 0; $j < $c; $j++) {
if ($i != $j && $b[$i] == $b[$j])
$dup = 1;
}
if (!$dup)
$d[] = $b[$i];
}
function has_dupes($array){
$dupe = array();
foreach($array as $val){
if(++$dupe[$val] > 1)
return true;
}
return false;
}
could do something like this.. this would check for dupes, then u can print the uniques
Why are you making a simple task complex .. simply
$b = array('a','b','c','a','b');
var_dump(customCount($b));
Output
array (size=3)
'a' => int 2 //duplicate
'b' => int 2 //duplicate
'c' => int 1
Function Used
function customCount($array) {
$temp = array();
foreach ( $array as $v ) {
isset($temp[$v]) or $temp[$v] = 0;
$temp[$v] ++;
}
return $temp ;
}