I want to create an array of numbers: 10, 9,8...to 1. But when I echo $numbers, I get "Array" as the output as to the numbers.
There is probably a simple thing I missed, can you please tell me. thanks!
$numbers=array();
for ($i=10; $i>0; $i--){
array_push($numbers, $i);
}
echo $numbers;
To output a string:
echo implode(', ', $numbers);
for debugging purposes use print_r or var_dump.
No you are not missing anything. Of course $numbers is an array.
If you do print_r($numbers) than you see what elements are in the array.
It very much depends what you want to do with array in the end. You can for example also loop over the array values:
foreach($numbers as $number) {
//whatever you want to do
echo $number;
}
If you only want to print theses 10 numbers you can also just do:
for ($i=10; $i>0; $i--){
echo $i;
}
As I said it depends on what you want to do :)
Depends on what you want the output to look like.
For debugging purposes, this should work:
print_r($numbers);
For a "prettier" output:
foreach ($numbers as $key => $value)
echo $key . "=" . $value
First of all, you can create that array much easier using range()
$numbers = range( 10, 1 );
Secondly, only scalars can be echoed directly - complex values like objects and arrays are casted to strings prior to this. The string cast of an array is always "Array".
If you want to see the values, you have to flatten the array somehow, to which you have several options - implode() might be what you're looking for.
echo implode( ',', $numbers );
For debugging purposes you might like:
echo '<pre>' . print_r($numbers, true) . '</pre>';
As it's output is clear without having to look at page source.
Related
I have this function which basically passes different arrays but it can only pass arrays with a length of 5. I was wondering if there is a way to pass various array lengths through this function?
The output needs to only show one item at a time rather than all. I have 8 arrays with various lengths which is why I am making one general function to pass the arrays through.
function myFunction(&$array){
//shuffling array contents
shuffle($array);
//isolate an array
if(in_array($array[0], $array)){
echo $array[0];
}
elseif(in_array($array[1], $array)){
echo $array[1];
}
elseif(in_array($array[2], $array)){
echo $array[2];
}
elseif(in_array($array[3], $array)){
echo $array[3];
}
elseif(in_array($array[4], $array)){
echo $array[4];
}
}
I feel like I'm probably still missing something, but it looks like this could be simplified to:
function myFunction(&$array){
//shuffling array contents
shuffle($array);
//isolate an array
echo isset($array[0]) ? $array[0] : '';
}
Which would take arrays of any length (including zero), shuffle and echo one item.
Add an argument to your function, e.g. $len, and use a loop:
function myFunction(&$array, $len){
//shuffling array contents
shuffle($array);
//isolate an array
for ($i = 0; $i < len; $i++) {
if(in_array($array[i], $array)){
echo $array[i];
break;
}
}
}
This will do exactly the same as your if ... elseif ... elseif ... thing. I don't know what this is supposed to do though. I think his will always echo $array[0] so it could be simplified a lot.
Basically, you want to pass an array containing a variable number of arrays, and select one of these at random. Is that correct?
If so, then this should do it:
$key = array_rand ($array);
$member = $array[$key];
No need to test for existence of members, as they will always exist. In fact as you can see, there isn't a need for creating a custom function at all. ;)
I need your advice if there is a better (and faster) way to output duplicate array values and their count in php.
Currently, I am doing it with the below code:
The initial input is always a text string like this:
$text = "a,b,c,d,,,e,a,b,c,d,f,g,"; //Note the comma at the end
Then I get the unique array values:
$arrText = array_unique(explode(',',rtrim($text,',')));
Then I count the duplicate values in the array (excluding the empty ones):
$cntText = array_count_values(array_filter(explode(',', $text)));
And finally I echo the array values and their count with a loop inside a loop:
foreach($arrText as $text){
echo $text;
foreach($cntText as $cnt_text=>$count){
if($cnt_text == $text){
echo " (".$count.")";
}
}
I am wondering if there is a better way to output the unique values and their count without using a loop inside a loop.
Currently I have chosen this approach because:
My input is always a text string
The text string contains empty values and has a comma at the end
I need to echo only non empty values
Let me know your expert advices!
You can write your code to print the values a lot shorter (Also I wrote other things a bit shorter):
You don't need rtrim() or array_unique(), you only need explode() and with array_filter() you take care of the empty values. Then just use array_count_values() and loop through the values.
<?php
$text = "a,b,c,d,,,e,a,b,c,d,f,g,";
$filtered = array_filter(explode(",", $text));
$countes = array_count_values($filtered);
foreach($countes as $k => $v)
echo "$k ($v)";
?>
output:
a (2)b (2)c (2)d (2)e (1)f (1)g (1)
You shouldn't need to make two arrays as array_count_values keys are the value of the text.
$myArray = array_count_values(array_filter(explode(',',$text)));
foreach($myArray as $key => $value){
echo $key . ' (' . $value . ')';
}
I am still learning PHP and have a bit of an odd item that I haven't been able to find an answer to as of yet. I have a multidimensional array of ranges and I need to echo out the values for only the first & third set of ranges but not the second. I'm having a hard time just finding a way to echo out the range values themselves. Here is my code so far:
$array = array(
1=>range(1,4),
2=>range(1,4),
3=>range(1,4)
);
foreach(range(1,4) as $x)
{
echo $x;
}
Now I know that my foreach loop doesn't even reference my $array so that is issue #1 but I can't seem to find a way to reference the $array in the loop and have it iterate through the values. Then I need to figure out how to just do sets 1 & 3 from the $array. Any help would be appreciated!
Thanks.
Since you don't want to show the range on 2nd index, You could try
<?php
$array = array(
1=>range(1,4),
2=>range(1,4),
3=>range(1,4)
);
foreach($array as $i=>$range)
{
if($i!=2)
{
foreach($range as $value)
{
echo $value;
}
}
}
?>
Note: It's not really cool to name variables same as language objects. $array is not really an advisable name, but since you had it named that way, I didn't change it to avoid confusion
Do you want to do this?
foreach ($array as $item) {
echo $item;
}
You can use either print_r($array) or var_dump($array).
The second one is better because it structures the output, but if the array is big - it will not show all of it content. In this case use the first one - it will "echo" the whole array but in one row and the result is not easy readable.
Range is just an array of indexes, so it's up to you how to your want to represent it, you might want to print the first and the last index:
function print_range($range)
{
echo $range[0] . " .. " . $range[count($range) - 1] . "\n";
}
To pick the first and the third range, reference them explicitly:
print_range($array[1]);
print_range($array[3]);
here is one solution:
$array = array(
1=>range(1,4),
2=>range(1,4),
3=>range(1,4)
);
$i=0;
foreach($array as $y)
{ $i++;
echo "<br/>";
if($i!=0){ foreach($y as $x)
{
echo $x;
}
}
}
I have a number that is get from a web page, such that
$number=12-34-33-87-54-................ and so on.
$number may have two numbers like $number=12-34
or
it may have 3 numbers like $number=12-34-33
or
it may have more numbers like $number=12-34-33-87-54-.......... and so on.
I want to split it by '-' and want to store in an array.
like array(12,34,33,.....)
I used the code given below but it doesn't work.
<?php
$a=array();
list($a)=split('-',$number);
foreach($a as $v)
{
echo $v;
}
?>
Plz tell me how can i split this?
$parts = explode('-',$number);
Although split() is depracated, explode will do the trick for you
<?php
$a=explode('-',$number);
foreach($a as $v)
{
echo $v;
}
?>
It assumes that a , is the default, so you can use it on a CSV without having to specify.
explode will return this array without any interfere from your side
$returnedArray = explode('-',$number);
this should work fine for you
Php explode is what you need here.
$arrayOfTokens = explode('-',$number);
explode returns an array of string, as stated in the documentation provided.
I'm pretty new to PHP and want to write a "for" statement based on IDs of a page. The problem is that the IDs are not sequential (by that I mean they are not increasing by 1 each time ie. 1,2,3,4,5). Is there a way to use "for" to use specific numbers? For example (pseudocode)
for IDnumbers i = (1,5,7,23,28,34)
echo "function1(i)"
echo "function2(i)"
end
I hope that makes sense. I basically want functions associated with each post ID to be returned, but I want the IDnumbers to be specific. Sorry if this is a basic question!
edit: wow, that really was basic. Thanks guys!
// create an array
$numbers = array(1,5,7,23,28,34);
// loop over it
foreach($numbers as $number){
echo function1($number);
echo function2($number);
}
foreach (array(1,5,7,23,28,34) as $n) {
echo "function1($n)";
echo "function2($n)";
}
foreach( Array(1,5,7,23,28,34) as $i) {
// do stuff with $i
}
Store the numbers in an array and use a foreach.
$IDNumbers = array(1,5,7,23,28,34);
foreach($nums as $num)
{
echo function1($num);
echo function2($num);
}
Read more about php foreach here.
try something like this:
IDnumbers = array(1,5,7,23,28,34)
foreach IDnumbers as id
echo "function1(id)"
echo "function2(id)"
end
IF you MUST use FOR, then you can do this:
$IDNumbers = (1,5,7,23,28,34);
for($i = 0; $i < count($IDNumbers); $i++):
echo function1($IDNumbers[$i]);
endfor;
This is because even though you stored a number as the value in a numeric array, it still orders it sequentially in reference to the identifiers and where they were added. The value may be 7, but its pointer is 2 (0, 1, 2, 3, 4, 5 in this case)