Accessing Arrays Whitout Quoting the Key

Accessing arrays whitout quoting the key

Use the latter variant $array['key']. The former will only work because PHP is tolerant and assumes the string value key if there is no constant named key:

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. […] This is wrong, but it works. The reason is that this […] has an undefined constant (bar) rather than a string ('bar' - notice the quotes).

See also Array do's and don'ts.

Now in opposite to accessing arrays in plain PHP code, when using variable parsing in double quoted strings you actually need to write it without quotes or use the curly brace syntax:

[…] inside a double-quoted string, it's valid to not surround array indexes with quotes so "$foo[bar]" is valid. See the above examples for details on why as well as the section on variable parsing in strings.

So:

// syntax error
echo "$array['key']";

// valid
echo "$array[key]";
echo "{$array['key']}";

PHP Array access without quotes

What happens here, is that PHP sees a constant called test. If the constant is defined, the value is returned, it isn't defined, PHP falls back to the string "test". For example:

$array = array("A" => "Foo", "B" => "Bar", "C" => "Baz")
define("B", "C");

echo $array[A]; // The constant "A" is undefined,
// so PHP falls back to the string "A",
// which has the value "Foo".
echo $array["B"]; // The result is "Bar".
echo $array[B]; // The value of constant "B" is the string "C".
// The result is "Baz".

It's for backwards compatibility and you should never use it. If you'll turn on notices, you'll see that PHP complains about it.

Using arrays keys without quotes, how to fix it on big project?

I use Notepad++ which has a search and replace in files feature (Ctrl + Shift + F). With regular expression mode on, you could use

Search:

\$my_array\[([^\'\"]+)\]

Replace

\$my_array\[\'$1\'\]

The search looks for anything within the array key square brackets where there is not already a " or ' character, which would indicate the declaration is already valid.

Select the directory of your project then hit "Replace in Files". Ensure your entire project is backed up first in case something goes wrong.

How can I access an array value without using array[key]?

There are a couple of ways to do this.

First: You could use for..of loop. for..of doesn't use the square bracket notation and doesn't give the index directly.

for (let element of list) {
console.log(element);
}

Second: The other way is what @Rajesh has mentioned: ForEach.

list.ForEach((element) => {
console.log(element)
});

Echo Array without key using index

You can use the array_values() function to achieve your goal:

// Extract the values of the array and re-use as indexed array
$array = array_values($array);
echo $array[0]['Company'];

// If you want to keep your associative array as well then do this
$array = array_merge($array, array_values($array));
echo $array[0]['Company'];
// OR
echo $array['ifour consultancy 123']['Company'];

Do PHP array keys need to we wrapped in quotes?

The first one will use the value of $name as key while the second will use the literal string '$name' as key.

Change value of PHP Array without using the keys

You have to use nested loop.

foreach($data as $key => $value) {
foreach( $value as $k => $v ) {
if( $k === 0 ) {
$data[$key][$k] = 'some value here';
}
}
}


Related Topics



Leave a reply



Submit