Is there a way to walk dynamically into multidimensional arrays? - php

For example:
$size = 0;
$array = $array;
$size = 1;
$array = $array[x];
$size = 5;
$array = $array[x][x][x][x][x];
I got a $config array that can either have 1 dimension or many. Depending on setting of the var $size the elements I need walk gonna be on that position. If size = 1, I will be looking for $config[1]. If size = 2 I will be looking for $config[1][1] ...
Thanks,

$foo = $array;
for($i=0;$i<$size;++$i) {
$foo = $foo[x];
}

$array = $array[x][x][x][x][x];
for ($x = 0; $x < 5; $x++) {
if (!is_array($array[1])) break;
$array = $array[1];
}
You can make infinite loop and reach end of array.

Related

How to create array from variable php using loop for?

How to create array from variable php using loop for ?
i have many variable php like
$number_0 = 1;
$number_1 = 2;
$number_2 = 5;
$number_3 = 2;
$number_4 = 6;
i want to create array like this
$ar = array('1','2','5','2','6');
but using loop for like
for ($i=0;$i<5;$i++)
{
$number."_".$i ====> to array
}
Not a recomended way of doing things but:
$arr = array();
for($i=0;$i<5;$i++) {
$varName = 'number_'.$i;
$arr[] = $$varName;
}
for ($i = 1; $i <= 6; $i++)
$ar[] = $i;
for ($i=1;$i<7;$i++)
{
$ar[] = $i;
}

Counting the number of dimensions in an array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a way to find how how “deep” a PHP array is?
I am trying to write a method to count the number of dimensions of an array. The following gives me a correct count of dimensions
$array = array();
$array[0] = array();
$array[0][0] = 0;
$array[0][1] = array();
$array[0][1][0] = 10;
$array[0][1][1] = 11;
echo '<p>'.\utility\arrayTools\arrayTools::numberOfDimensions($array).'</p>';
//3 Dimensons
The second examples also gives me a correct count of the number of dimensions
$array = array();
$array[0] = array();
$array[0][0] = 0;
$array[0][1] = array();
$array[0][1][0] = 10;
$array[0][1][1] = 11;
$array[1] = 1;
$array[2] = 2;
//3 Dimensions
But the following example gives me too high of a count
$array = array();
$array[0] = array();
$array[0][0] = 0;
$array[0][1] = array();
$array[0][1][0] = 10;
$array[0][1][1] = 11;
$array[1] = 1;
$array[2] = 2;
$array[3] = array();
$array[3][0] = 30;
//Should still be 3 dimensions, but gives me 4
The method I am using is below
//Method
public static function numberOfDimensions($array)
{
if(func_num_args() === 2){
if(is_int(func_get_arg(1))){
$number_of_dimensions = func_get_arg(1);
}else{
throw new Exception('The second argumment must be an interger');
}
}else{
$number_of_dimensions = 0;
}
if(is_array($array) === TRUE){
$number_of_dimensions++;
if(self::isMultiDimensional($array) === TRUE){
foreach($array as $iteration){
$number_of_dimensions = self::numberOfDimensions($iteration,$number_of_dimensions);
}
return $number_of_dimensions;
}else{
return $number_of_dimensions;
}
}else{
return $number_of_dimensions;
}
}
I already know the problem is it is still adding for every multidimensional even though the count may be equal to or less then the number of dimensions. But what I can't figure out is how to get it to find the highest number of dimensions and stop counting
Here is a simpler version of your script
function numberOfDimensions($array) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
$d = 0;
foreach ( $it as $v )
$it->getDepth() >= $d and $d = $it->getDepth();
return ++ $d;
}
From another answer here on SO:
function array_depth($array) {
$max_depth = 1;
foreach ($array as $value) {
if (is_array($value)) {
$depth = array_depth($value) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
I havn't written any PHP in a very long while, so there may very well be mistakes in my code, excuse me in advance.
function numberOfDimensions($array,$so_far=0){
if !is_array($array){
return 0;
}
$max_dims = 0;
foreach($array as $element){
$element_dims = numberOfDimensions($array);
$max_dims = max($max_dims,$element_dims);
}
return $max_dims +1;
}
This function assumes it is being called with an array as parameter, thus the minimum depths is 1.
It then recursively looks at all child elements, and if the child is deeper than already known, it add the depth of the child to the calculated depth:
function numberOfDimensions($subject) {
// scalar value has depth 0
if(!is_array($subject)) return 0;
// array has min depth of 1
$depth = 1;
foreach ($subject as $element) {
if (is_array($element)) {
// is the sub array deeper than already known?
$sub_depth = numberOfDimensions($element);
if ($sub_depth >= $depth) {
$depth += $sub_depth;
}
}
}
return $depth;
}

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");

How do I select random values from an array in PHP?

I have an array of objects in PHP. I need to select 8 of them at random. My initial thought was to use array_rand(array_flip($my_array), 8) but that doesn't work, because the objects can't act as keys for an array.
I know I could use shuffle, but I'm worried about performance as the array grows in size. Is that the best way, or is there a more efficient way?
$result = array();
foreach( array_rand($my_array, 8) as $k ) {
$result[] = $my_array[$k];
}
$array = array();
shuffle($array); // randomize order of array items
$newArray = array_slice($array, 0, 8);
Notice that shuffle() function gives parameter as a reference and makes the changes on it.
You could use array_rand to pick the keys randomly and a foreach to gather the objects:
$objects = array();
foreach (array_rand($my_array, 8) as $key) {
$objects[] = $my_array[$key];
}
What about?:
$count = count($my_array);
for ($i = 0; $i < 8; $i++) {
$x = rand(0, $count);
$my_array[$x];
}
I just found this in our code and was hoping to find a more readable solution:
$rand = array_intersect_key($all, array_flip(array_rand($all, $count)));
You can get multiple random elements from an array with this function:
function getRandomElements(array $array): array
{
$result = [];
$count = count($array);
for ($i = 0; $i < rand(0, $count); $i++) {
$result[] = rand(0, $count);
}
$result = array_unique($result);
sort($result);
return $result;
}

Categories