Has something here been kicked out of php? [duplicate] - php

This question already has answers here:
How can I update code that uses the deprecated each() function?
(12 answers)
Closed 11 months ago.
I had some code that has stopped working. I suspect the web server hotel php version has been changed as perfectly good working code no longer works. This is the code. It read in the number of each category from "$catcho" the user chose and created an array of them all. So if the user chose category 1, 3 and 4 the array "$whiskers" would have the values 1, 3 and 4 in it.
while (list ($key, $val) = #each ($catcho)) {
$whiskers[] = "$val"; // This builds a list of chosen cats
}
I have simplified the code because in the final version I don't actually read in all values as they are dependent upon other choices.
What other php code can I put in there instead of mine?
Thanks for any help you are able to give me.

as 'each' is now removed from php 8+
you can rewrite the construct as
foreach ($catcho as $val){
$whiskers[] = $val;
}

Related

PHP: fetch_assoc() into array causing null values [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I'm trying to fetch data from a MariaDB database of festivals and respective locations. When running:
while($row = $sth->fetch_assoc()){}
and iterating over $row while outputting the values, I get data as it is in the database.
However, when storing each row like so:
while($row = $sth->fetch_assoc()){
$results[] = $row;
}
And echoing the results as JSON (echo json_encode($results);)
I get this:
{"id":"0","name":null,"village":"0","startDate":"2019-01-16",
"endDate":"2019-01-23","message":null}
This is for an existing Linux server, which I do not manage (I'm using CPanel). PHP version is 5.4 and MariaDB 10.1.37.
So far, a lot of code samples on Stack Overflow and other websites are using
$results[] = $row;
for storing the results.
I'm returning to PHP after 3 years of Swift only programming... So I suspect this could be a simple issue to solve...
Thanks!
May be I'm bit too late for answering this question, but it may help someone stuck with this issue. Recently I faced a similar issue and it took me 3 days to understand that it is an issue with the utf-8 characters. Data stored in database are perfectly fine. But when it comes to return it via json_encode(), it shows no data at all as json_encode only supports utf-8 data(reference).
For your case the following method should work-
foreach ($results as &$r) {
$r['name'] = utf8_encode($r['name']);
//same for all other items
}

How to create a function finding the last key of an array using value PHP in different ways [duplicate]

This question already has answers here:
PHP - Get key name of array value
(9 answers)
Closed 4 years ago.
How to get the Cat by using the value Nap in array
["Dog" => "Bite", "Cat" => "Nap"]
and i want to get Cat using value Nap
$key = array_search('Nap', $array);
What is not clear in your question is if you already know the entry is the last entry, or if you're searching for a value that is somewhere in your array.
If you have PHP 7.3, and you know it's the last entry, you can use array_key_last() (see http://php.net/manual/en/function.array-key-last.php )
$key = array_search('Nap', $array); (as mentionned by #Sanu0786 )

PHP Syntax Error for simple web script [duplicate]

This question already has an answer here:
Populating associative arrays
(1 answer)
Closed 7 years ago.
Running into an issue with a simple PHP script and I can't seem to figure it out. States its on line 3 and I don't see it. Need a fresh set of eyes please.
<?php
$numArr = [];
for ($i=0;$i<5;$i++) {
array_push($numArr,mt_rand());
}
echo min($numArr);
?>
It's likely how you're initializing your array. The [] syntax is only available from PHP 5.4 and above.
From the PHP manual:
As of PHP 5.4 you can also use the short array syntax, which replaces
array() with [].
You can use array () instead.

Is there a way to get just one element from array that is returned as a function? [duplicate]

This question already has answers here:
Accessing an array element when returning from a function
(3 answers)
Closed 9 years ago.
Say I have a function/method that returns an array, let's call it ArrayReturner(). But I only want the first element, [0]. Right now I'm doing something like...
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
Is there a way to do that in one line without the need for the temporary $arrayReturned array?
Try:
$arrayReturned = reset(ArrayReturner());
Depends on PHP's version you use.
If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.
If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:
$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];

php random array - random again when doing while [duplicate]

This question already has answers here:
php random order from a foreach
(3 answers)
Closed 9 years ago.
Right now, every single time I do a while do, it goes from top to bottom of my array. How can I make it go through each value but in a random mode, not from top to bottom?
Here's what I have:
$xbb = array('avotf1',
'avotf2',
'avotf3',
'avotf4',
'avotf5',
'avotf6',
'avotf7',
'avotf8',
'avotf9',
'avotf11',
'avotf12',
'avotf13',
'avotf14',
'avotf15',
'avotf10');
foreach($xbb as $item)
{
echo "$item<br>";
}
How do I shuffle the random array that I have and still show all 15 values?
Shuffle it with shuffle():
shuffle($xbb);
Searching Google for php shuffle array will give you tons of results as well, by the way.

Categories