automatically generate function for - php

i have a simple code
data :
$data1 = array('1','2','3','4');
$data2 = array('1','2','3','4');
$data3 = array('1','2','3');
Logic :
for($a = 0; $a < count($data1); $a++){
for($b = 0; $b < count($data2); $b++){
for($c = 0; $c < count($data3); $c++){
echo $data1[$a].$data2[$b].$data3[$c].'<br>';
}
}
}
in this sample total data is 3, if i have 4 data how to build logic will generate automatically
Above code is static and fixed only for 3 array inputs if there is a condition and the $data would be more than 3 then it will not work, so how can I use the code for more or less than 3 data variables
For example, If the input data is like,
$data1 = array('1','2','3','4');
$data2 = array('1','2','3','4');
$data3 = array('1','2','3');
$data4 = array(5,6);
$data5 = array(7,8);
Then how to use the loops in that case.

What about having an array of those arrays instead and then you iterate through that array?
i.e.
$data = array(
array('1','2','3','4'),
array('1','2','3','4'),
array('1','2','3')
);
I guess you know how to do the rest...

Related

loop append variable in php?

I dont know if this is possible but can you create a variable loop?
I want to create many variables in an if condition
if($session[hello]){
$array1 = array("");
$array2 = array("");
$array3 = array("");
$array4 = array("");
$array5 = array("");
}
but since I have more I want to write it like this:
if($session[hello]){
for($a = 1; $a <= 5; $a++){
$array + $a = array("");
}
But its not working and I cant figure out how this might be possible.
is there an alternative?
thanks
You can try this -
if($session[hello]){
for($a = 1; $a <= 5; $a++){
${'array' . $a} = array("");
}
}
Don't why you want it this way but using an array would be better.
if($session[hello]){
for($a = 1; $a <= 5; $a++){
$array[$a] = array("");
}
}
And access it like $array[1] or $array[2] etc.

PHP stucture struct[array()]

I try to find a structure in php that will do this job:
$a = array(1,2);
$b = array(3,4);
$c = array();
$c[$a] = 100;
$c[$b] = 200;
$i = $c[$a] + $c[$b];
echo $i;
As you can see I have used arrays but this seems not to be correct because I get this:
Warning: Illegal offset type in C:\xampp\htdocs....... on line .....
What is the proper structure I should use. Can this job be done with arrays? Thanks in advance!
In PHP array keys can only be integers or strings. You could achieve what you want with a multi-dimensional array
$a = array(1,2);
$b = array(3,4);
$c = array();
$c[$a[0]][$a[1]] = 100;
$c[$b[0]][$b[1]] = 200;
$i = $c[$a[0]][$a[1]] + $c[$b[0]][$b[1]];
echo $i;

Sort array by other one - PHP

Ive got 2 arrays, first one contains values of objects, and second one contains their IDs.
In this form:
$values[0] applies to $ids[0]
$values[1] applies to $ids[1]
I need to sort first array (using sort() ) from lowest to highest (values are ints) - That's not problem.
Problem is, that When I sort array with values, I will lost ID of that value.
My question is: How to make that
If $values[0] turns to $values[5], automatically turn $ids[0] to $ids[5]
Thanks
Update:
Content of $values and $ids:
$values[0] = 1.5;
$values[1] = 2.4;
$values[2] = 15.7;
$values[3] = 11.7;
$values[4] = 4.8;
$values[5] = 0.4;
$ids[0] = 1;
$ids[1] = 2;
$ids[2] = 3;
$ids[3] = 4;
$ids[4] = 5;
$ids[5] = 6;
Combine the arrays first, then sort by key:
$newArr = array_combine($ids, $values);
ksort($newArr);
It sounds like you're looking for array_combine():
Example
<?php
$ids = array(2, 1, 3); // IDs
$values = array(a, b, c); // Values
$array = array_combine($ids, $values); // Combine arrays as ID => Value
ksort($arrays); // Sort new array
print_r($array); // Echo array
Output
Array
(
1 => b,
2 => a,
3 => c,
)
Follow the code below... have not tested it ... but it must work.... Easy to understand..
<?php
$count = count($values);
for($i = 0; $i<$count; $i++)
{
if($i == 0)
{
$sort1 = $values[$i];
$sort2 = $ids[$i];
$temp = 0;
}
if($sort1 > $values[$i])
{
$sort1 = $values[$i];
$sort2 = $ids[$i];
$temp_val = $values[$temp];
$temp_id = $ids[$temp];
$values['temp'] = $values[$i];
$ids['temp'] = $ids[$i];
$temp = $i;
$values[$i] = $temp_val;
$ids[$i] = $temp_id;
}
}
?>

Invert matrix numbers - loop

First, i have these values.
$Arr1 = array(1/1, 1/2, 3/1);
$Arr2 = array(1/1, 4/1);
$Arr3 = array(1/1);
and i need an output with 3 arrays like these:
$a1 = array (1/1, 1/2, 3/1);
$a2 = array (2/1, 1/1, 4/1);
$a3 = array (1/3, 1/4, 1,1);
What i am trying is :
for ($i=0; $i<count($Arr1); $i++) {
${"a".$i} = array(
//here, the number of array elements depends to the length of $a1
);
}
Any help ? thanks
I think this image helps to understand the problem:
First off, using a 2D array will make your life a lot easier.
So first, initialize your values like this:
$matrix_size = 3;
$matrix = array();
for($i = 0; $i < $matrix_size; $i++){
$matrix[$i] = array_fill(0, $matrix_size, null);
}
$matrix[0][0] = 1/1;
$matrix[0][1] = 1/2;
$matrix[0][2] = 3/1;
$matrix[1][1] = 1/1;
$matrix[1][2] = 4/1;
$matrix[2][2] = 1/1;
Then you can run a loop like this:
foreach($x = 0; $x < $matrix_size; $x++){
foreach($y = 0; $y < $matrix_size; $y++){
if(is_null($matrix[y][x]) && !is_null($matrix[x][y])){
$matrix[y][x] = 1/$matrix[x][y];
}
}
}
I'm sure there is a much more efficient way to do this, but this is a start for you to explore.

2 One Dimensional arrays to 1 Two Dimensional array

I have two arrays like this
array x [Firefox,IE,Chrome,Opera]
array y [40,30,25,5]
Required final [[Firefox,40],[IE,30],[Chrome,25],[Opera,5]]
I need this in PHP.I think I can run a for loop and do something like this .
final23 [0][i] = x[i];
final23 [1][i] = y[i];
Is there any better way or built in function in PHP ?
$result = array();
$size = max(count($x), count($y));
for ($i = 0; $i < $size; $i++) {
$result[] = array(
isset($x[$i]) ? $x[$i] : null,
isset($y[$i]) ? $y[$i] : null
);
}
$x = array("Mozzila","IE","Firefox","Opera");
$y = array(40,30,25,5);
$final = array();
$i = 0;
foreach($x as $a){
$final[] = array($a,$y[$i]);
$i++;
}
Learn about Associated array.
$arr = new Array("40"=>"FireFox","30"=>"IE","25"=>"Chrome","5"=>"Opera");

Categories