Why to declare array before setting it (php)? [duplicate] - php

This question already has answers here:
Is it necessary to declare PHP array before adding values with []?
(13 answers)
Closed 5 years ago.
I dont know why people declare array before loops and etc..:
$new= array(); // <----- why this is needed ?
foreach($something as $v){
$new[] = $v;
}
Why to declare the array before setting its value? (In other languages, i.e. C# and JAVA it is needed, but why in PHP?)

You're not setting its value, you're pushing a new element onto the array. But there needs to be an empty array to push onto.

Related

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 )

Create new variables in a loop (PHP)? [duplicate]

This question already has answers here:
PHP - How Can I Create Variable Names Within a Loop?
(6 answers)
How do I dynamically create the variable name in a PHP loop?
(1 answer)
how to change php variable name in a loop?
(5 answers)
Declare variables on the fly using for loop
(5 answers)
Create new variables with foreach loop
(3 answers)
Closed 4 years ago.
I have a problem with PHP. I'd like to create a new variable each time, my loop runs through. For example: If my loop runs for the first time, it should create a new variable called $loop1, if it runs the second time $loop2 with a different value, third loop - $loop3...
Here is a reference of my code, any help is appreciated!
while ($x < count($lines)) {
$info
x++;
}
You can create variables dynamically using braces in PHP, e.g.:
${"loop" . $x} = "some value";
(Reference: PHP "variable variables")
But note that this might be a bad idea in real-world application.
It might be more appropriate to use an array or a map instead, e.g.
$loopVars = []; // create a new array
while (...) {
$loopVars[] = "some value"; // add a new array element
}
I guess you are searching for something like an array.
$data = array();
while ($x < count($lines))
{
$data[] = ... whatever you want to store
}

Use array values as new multidimensional array keys [duplicate]

This question already has answers here:
Using a string path to set nested array data [duplicate]
(8 answers)
Closed 4 years ago.
Edit (after downvote): There is a similar question here > Using a string path to set nested array data
However I didn't find that question when searching for an answer due to the way it's worded, and I'm sure this will happen for other people, so this question may act as a useful gateway to that question and it's answers.
I'm sure I'm missing something obvious, but I can't think how to do this: I have an array containing one or more items:
array('value1', 'value2');
I need to use these values as the keys in a multidimensional array :
array['value1']['value2'] = 'somevalue';
How do I do this?
You can use a nice recursion here:
function nestArray($items, $value) {
return $items ?
array($items[0] => nestArray(array_slice($items, 1), $value))
: $value;
}
$array = array('value1', 'value2');
print_r(nestArray($array, 'somevalue'));

How do I append a value to an array within an array in PHP? [duplicate]

This question already has answers here:
How to add elements to an empty array in PHP?
(8 answers)
Closed 5 years ago.
Given this PHP array:
$options['systems'] = array(1, 2, 3)
How would I append the value 4 to the $systems array within the $options array?
You could use array_push to push additional items like so:
array_push($options['systems'], 4);
Or the shorthand version:
$options['systems'][] = 4;
You can use php array_push function. Like this. array_push($options['systems'],4);
you can read the detail of array_push from below link.array_push manual

Php Unset All Keys [duplicate]

This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.

Categories