Make an array value variable (PHP)

Say I want an echo array, but I want to make a value in an array variable I echo, how would I do this?

Below is an explanation of what I will not do, but this is not the correct syntax.

$number = 0;
echo myArray[$number];
+3
source share
5 answers

I'm not sure what you mean. What you have does not work because you lack $in myArray:

$myArray = array('hi','hiya','hello','gday');
$index = 2;
echo $myArray[$index]; // prints hello
$index = 0;
echo $myArray[$index]; // prints hi

Unlike other languages, all types of PHP variables are preceded by a dollar sign .

+12
source

. , , .

$arrayStates = array('NY' => 'New York', 'CA' => 'California');

, :

echo $arrayStates['NY']; //prints New York

echo $arrayStates[1]; //prints California

, foreach .

foreach($arrayStates as $state) {
        echo $state;
}

, foreach -, . :

if(is_array($arrayStates)) {
    foreach($arrayStates as $state) {
            echo $state;
    }
}

, !

+4

You are almost there:

$number = 0;
$myArray = array('a', 'b')
echo $myArray[$number];   // outputs 'a'
+2
source
$myArray = array("one","two","three","four");
$arrSize=sizeof($myArray);

for ($number = 0; $number < $arrSize; $number++) {
echo "$myArray[$number] ";
}

// Output: one two three four
+1
source
$myArray = array('hi','hiya','hello','gday');

for($count=0;$count<count($myArray);$count++)
{
  $SingleValue = $myArray[$count];
  $AllTogether = $AllTogether.",".$SingleValue;
}
0
source

Source: https://habr.com/ru/post/1706212/


All Articles