Really simple php array issue - php

I'm not quite sure what I'm doing wrong, but it appears I'm having myself a brain fry trying to comprehend it..
$cards = array(range(1,52));
shuffle($cards);
echo $cards[0];
I get a array to string conversion error.
I've also tried a custom function to echo dependent on the input value and that isn't working either.

You're creating an array of arrays. range() already returns an array:
$cards = range(1,52);
shuffle($cards);
echo $cards[0];

The range function returns an array (http://php.net/manual/en/function.range.php), so the statement $cards = array(range(1,52)); has set $cards to be an array with exactly one element - an array containing the range of values from 1 to 52.
Thus when you try to echo $cards[0], you are trying to echo an element which is an array, which produces the error.
What you want to do is this:
$cards = range(1, 52);
shuffle($cards);
echo $cards[0];

Get rid of the array, range returns an array:
$cards = range(1,52);

Range already creates an array. You have create an array with one element that contains the array.
$cards =range(1,52);
shuffle($cards);
echo $cards[0];

Related

array_push won't give an array, prints out integer value

I'm doing a very simple php program with array_push, but it isn't working according to the documentation. Every time i try to print the value of the final array, it gives me an integer. Could someone please help me with this?
Here's my code:
<?php
$preArray = array('1','2','3','4','5','6','7','8');
$val = 10;
$array = array_push($preArray, $val);
print_r($array);
?>
This is what it outputs:
9
Thanks in advance for the help.
array_push() returns the new number of elements in the array. So if you're not interested in the number of elements in the array then just use:
array_push($preArray, $val);
The variable $preArray will contain the value pushed into it.
print_r($preArray);

PHP- Create an array with the matches of two arrays

I am trying to compare two non-associative arrays to create a new array with the matches.
This is what I have though:
//This array has several entries
$a_firstarray = Array();
//This array has less entries than the first array
$a_secondarray = Array();
//This array should contain the matches of the first array and the second array in no particular order
$a_mergedarray
for($i=0;$i <=count($a_firstarray);$i++){
for($a=0;$a <=count ($a_secondarray);$a++){
if($a_firstarray[$i] == $a_secondarray[$a]){
$a_mergedarray[] = $a_activecategory[$i];
}
}
}
It doesn't work for some reason. I am also sure that PHP has some sort of function that does this. Any ideas? thanks in advance.
This is known as the "intersection" of two arrays. PHP provides array_intersect.
use array_intersect.
$result = array_intersect($array1, $array2);
Are you looking for array_intersect()?
http://php.net/manual/en/function.array-intersect.php

Safety net for array_slice

I read that array_merge will return NULL if you try to merge an empty array and any other array. That's not what I hope to do. I am trying to merge an array with a new array that is actually a slice of another array. ($i is an integer).
$forgotten = array_slice($matches, $i) ;
$leftOvers = array_merge($leftOvers, $forgotten);
The question is, what does array_slice return when the index is not found? If it can return null, should I do something like this:
$forgotten = array_slice($matches, $i) || array();
Also, is there any difference between using array_merge like this, and pushing $forgotten into leftOvers?
if you use array_merge over an empty array and another array it will return an array composed with the elements of the not empty array. If you try to merge two empty arrays it will return an empty array.
array_slice($matches, $i)
array_slice returns an array with all the elements of $matches with index greater or equal $i, if there are no such elements it will return an empty array.
Using array_push:
array_push($leftOvers, $forgotten)
$result will enqueue an array as a value the end of $leftOvers.
Always try to read the php man when you use a new php function, also you could get all these answers just trying to execute the functions in a test.php file.

how to convert integer array to separated numbers in php

What would be the most simple way to convert an Array Integer to separate numbers?
Example:
array(2,4,6)
should result in:
num1=2,num2=4,num3=6
You have tried "list"?
list($num1, $num2, $num3) = $myArray;
See http://php.net/list for more details.
If you have associative array than You can use php built in function extract().
Example:
$abc = array('var1'=>2, 'var2'=>4, 'var3'=>6);
extract($abc);
echo $var1.$var2.$var3; //Outputs 246

PHP Single line array shuffle is not working

Very simple but I was wondering why this is not working.
I'm trying shuffle an array and output the results (in a single line structure)
this is my code :
echo shuffle(array("A","B","C"))[0];
Small tweak needed here ;)
Your basic logic is a little bit wrong. You're interested in only one value, I assume? To solve it with that logic in mind, you can do it like this:
echo array_rand(array_flip(['A', 'B', 'C']));
Try below code
$arr = array("A","B","C");
shuffle($arr);
echo $arr[0];
I know that this is not the best solution for you, but it works!
print_r( ( $b=array('A', 'B', 'C') ) && shuffle($b) ? next($b) : null );
How this works:
Assign the array to the variable $b
Shuffle the variable $b
If the shuffle() succeeded:
return the next element in the array
If the shuffle() failed:
return null
Some might think: "Why didn't he used the current() function?"
Well, it seems that the function shuffle simply changes the order of the keys, but the pointer is always pointing to the same element. This means that current() will always return 'A'.
Apparently, this behaviour changed on PHP 5.4 to set the pointer to the first element.
TRY THIS SIMPLE FUNCTION
$my_array = array("A","B","C","D","E");
shuffle($my_array);
print_r($my_array);
You will need to pass the array in a seperate variable. Also, shuffle() itself just returns a boolean value, so you need to return an array element instead of the output of the function.
$ar = array("A","B","C");
shuffle($ar);
echo $ar[0];

Categories