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);
Related
I have a string containing that is being returned from an API which is dynamic, an example of the string is below:
[[[1473261033000,3.7933],[1473312464000,2.0295],[1473313206000,2.0844],[1473313505000,1.4888],[1473313805000,1.3003],[1473314105000,1.1164]]]
I cant really do anything with it like this so i would like to split it into 2 arrays, one for the first number and one for the second, like so:
Array1
1473261033000
1473312464000
1473313206000
1473313505000
1473313805000
1473314105000
Array2
3.7933
2.0295
2.0844
1.4888
1.3003
1.1164
Would someone help me out on how i could go about this? its driving me nuts.
You need to first decode the JSON string into an array, then use array_column to extract the two arrays.
This will require PHP >=5.5, but if you require a lower version then you should be able to find a backwards compatible version of the function on the documentation page.
<?php
$str = '[[[1473261033000,3.7933],[1473312464000,2.0295],[1473313206000,2.0844],[1473313505000,1.4888],[1473313805000,1.3003],[1473314105000,1.1164]]]';
$json = json_decode($str, true)[0];
$first = array_column($json, 0);
$second = array_column($json, 1);
print_r($first);
print_r($second);
See https://eval.in/637961 for a demo.
Well! json_decode() turns it to an array. Whether it's any use to you depends on what you want to do with the data really, doesn't it?
I'm wondering why you want to split it in two arrays. Isn't it easier to keep it in one?
<?php
$val = '[[[1473261033000,3.7933],[1473312464000,2.0295],[1473313206000,2.0844],[1473313505000,1.4888],[1473313805000,1.3003],[1473314105000,1.1164]]]';
$myData = json_decode($val);
var_dump($myData);
?>
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];
I'm trying to use array_search to find the key of a value in an array, I have the following simple code:
$current_user_existing_shopping_bag_string = '1446784/-/3/-/£797.00(_)902982/-/4/-/£148.80(_) ';
$current_user_existing_shopping_bag_array = explode('(_)', $current_user_existing_shopping_bag_string);
$key = array_search($current_user_existing_shopping_bag_array, '902982');
echo $key;
However I have no idea why this doesn't return the key of the value in the array, it should though. I've been trying various solutions for hours now and still no luck.
Anybody able to give me a pointer why this doesn't return the key for the value in the array?
Thanks
It is because array_search compares strings using === operator, not regex or strpos of any kind.
You search for 902982 but string is 902982/-/4/-/£148.80, and therefore they are not equal in any way.
For what you want to achieve, you can use preg_grep:
$result = preg_grep('/' . preg_quote($search) . '/', $current_user_existing_shopping_bag_array);
Then you can get the keys you need from the resulting array.
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];
I have an array on which I did array_count_values, and then an arsort.
$a example: $a = array('ten','ten','ten','three','two',one','ten','four','four');
I want to get the first element, and tried $a[0] but this did not work.
What is the correct syntax to get the first element please?
EDIT - added array
EDIT2 - Also, underlying array not allowed to be changed because their is code following that uses the associative array...
depending on whether or not you want to alter the array, you can use array_slice() or array_shift()
you can use array_values as
$a = array_values($a);
var_dump($a[0])
Same as in your last question.
key($a); // or each() for the first key=>value pair
Or alternatively:
$k = array_keys($a);
print $k[0];
ok I used this:
$a = array('ten','ten','ten','three','two','one','ten','four','four');
$bb= array_count_values ($a);
arsort($bb);
echo reset($bb);
It only works for getting the first element...Thank you all.
These are the ways of getting the first element:
$next = $arr[0];
$next = array_shift($arr);
$next = current($arr); // if the internal cursor is at position 0