php code to store array data
$series_ids = [];
for ($i = 0, $iMax = count($j_decode); $i < $iMax; $i++) {
$series_ids[] = $j_decode[$i]["series_id"];
$s_id=$series_ids[];
}
echo $s_id;
PS I don't want to use print_r. I need to display the result in table
I want to initializing a 2 dimensions Array in PHP and I don't know How to declare a 2d array of size (1,N) in php with all values as Zeros?
$Orders_C = array(1,N);
I am not sure if this is correct syntax or not.
PHP doesnot have 2d array ,but instead it has array of array. You can use the code below to initialize as you said.
$Orders_C=array_fill(0,1, array_fill(0, N,0));
Here the array_fill first return an array filled with 0 from index 0 to N.And again the same array is filled to the new array upto index 1.Hence you will get an array within array.
Generic solution for any count of columns and rows could be:
$maxRows = 5;
$maxCols = 5;
$orders = [];
for ($col = 0; $col < $maxCols; $col++) {
for ($row = 0; $row < $maxRows; $row++) {
$orders[$col] = $row;
}
}
And because you want 2d array like (1, N) then you can simplify it
$orders = [];
for ($i = 0; $i < $maxRows; $i++) {
$orders[0][] = 0;
}
I use the array_rand PHP function with an array. I use it with a data fixture function wich load a set of data in a loop like this:
$random_values = array();
for ($i = 0; $i < 20; $i++) {
$random_values[] = array_rand(["1","2","3","4","5"]);
}
My result is quite always "1" in the $random_values array, the native
PHP function seems not really random, Is there another stuff to do to
improve the randomization of my algorithm ?
Notice I already know there is an official documentation here, http://php.net/manual/fr/function.array-rand.php.
How's it going? So, with array_rand, it actually returns a random key within an array. Your current code does not put the random key value into the array you want to randomize. ie echo $random_value[$random_key]... See example below, hope this helps
$random_values = array("1","2","3","4","5");
for ($i = 0; $i < 20; $i++) {
$key = array_rand($random_values);
echo $random_values[$key] . "\n";
}
I am trying to use a for loop where it looks through an array and tries to make sure the same element is not used twice. For example, if $r or the random variable is assigned the number "3", my final array list will find the value associated with wordList[3] and add it. When the loop runs again, I don't want $r to use 3 again. Example output: 122234, where I would want something along the lines of 132456. Thanks in advance for the help.
for($i = 0; $i < $numWords; $i++){
$r = rand(0, $numWords);
$arrayTrack[$i] == $r;
$wordList[$r] = $finalArray[$i];
for($j = 0; $j <= $i; $j++){
if($arrayTrack[$j] == $r){
# Not sure what to do here. If $r is 9 once, I do not want it to be 9 again.
# I wrote this so that $r will never repeat itself
break;
}
}
Edited for clarity.
Pretty sure you are over complicating things. Try this, using array_rand():
$final_array = array();
$rand_keys = array_rand($wordList, $numWords);
foreach ($rand_keys as $key) {
$final_array[] = $wordList[$key];
}
If $numWords is 9, this will give you 9 random, unique elements from $wordList.
See demo
$range = range(0, $numWords - 1); // may be without -1, it depends..
shuffle($range);
for($i = 0; $i < $numWords; $i++) {
$r = array_pop($range);
$wordList[$r] = $finalArray[$i];
}
I do not know why you want it.. may be it is easier to shuffle($finalArray);??
So ideally "abcdefghi" in some random order.
$letters = str_split('abcdefghi');
shuffle($letters);
var_dump($letters);
ps: if you have hardcoded array $wordList and you want to take first $n elements of it and shuffle then (if this is not an associative array and you do not care about the keys)
$newArray = array_slice($wordList, 0, $n);
shuffle($newArray);
var_dump($newArray);
You can try array_rand and unset
For example:
$array = array('one','two','free','four','five');
$count = count($array);
for($i=0;$i<$count;$i++)
{
$b = array_rand($array);
echo $array[$b].'<br />';
unset($array[$b]);
}
after you have brought the data in the array, you purify and simultaneously removing the memory array
Ok... I have NO idea why you are trying to use so many variables with this.
I certainly, have no clue what you were using $arrayTrack for.
There is a very good chance I am mis-understanding all of this though.
<?php
$numWords=10;
$wordList=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$finalArray=array();
for ($i=0; $i<$numWords; $i++) {
start:
$r=rand(0,$numWords);
$wordChoice=$wordList[$r];
foreach ($finalArray as $word) {
if ($word==$wordChoice) goto start;
}
$finalArray[]=$wordChoice;
}
echo "Result: ".implode(',',$finalArray)."\n";
I load a xml-file which contains several arrays.
$array0 = (.......)
$array1 = (.......)
...
$arrayN = (.......)
In my HTML-part I have a DIV to display each array. To do it write all arrays in one masterarray:
for ( $i = 0; $i < 1; $i++) $masterArray[] = ${'array'.$i};
and can access each array
$Titel = $masterArray[0] [$i] [$kat] [0] ["TitelD"];
This works if I have more than one array in my xml-File. If I have only one the masterarray rests empty. Why that? If I copy a second in the xml-File it works... (with displaying the second!)
What can I do to get only one array in the masterarray?
This XML works:
and when changing the url to this xml-file (in the same html/php-file) the DIV is empty:
change this
for ( $i = 0; $i < 1; $i++)
to
for ( $i = 0; $i <= 1; $i++)