I’m looking for an alternative for define(‘name’, array) as using an array in define gives me this error: Constants may only evaluate to scalar values in … The array I’m mentioning contains strings only. Answer From php.net… The value of the constant; only scalar and null values are allowed. Scalar values are integer, float, string or boolean values. It is
Tag: arrays
Check if specific array key exists in multidimensional array – PHP
I have a multidimensional array e.g. (this can be many levels deep): I am trying to loop through it to see if a certain key exists: But it finds nothing. Is there an error in the loop? Answer I played with your code to get it working :
Access, transpose, and join data from a .csv file
I have a CSV File which is laid out as so: My code: This produces three strings, which are needed to plot a graph. However it is likely that I will be adding more columns to the csv file, so I need a way of looping around to avoid having to manually add new lines each time. I cannot seem
json with no index after unset encode array in php
I hope anyone can help me. My JSON isn’t indexed (i.e. there isn’t any key above any element) but after running unset() to remove an element with PHP the output JSON appears like this: How you can see there is a key before the element of JSON. I know that this behavior is caused by unset (PHP json_encode as object
How do I map an associative array to html element attributes?
I’m building a basic form building class to speed my workflow up a bit and I’d like to be able to take an array of attributes like so: $attributes = array( “type” => “text”, “id” …
how to concatenate two array element values in php by special charcter?
i have two arrays given below i want to concatenate two array element values by special character.given output below: Answer Try with array_map like this
create a PHP function that works for simple arrays and nested arrays
I am trying to create a function that works on both simple arrays and nested arrays. So far, the function looks like this: It works fine for simple arrays – for example: But it fails with the message “Warning: htmlspecialchars() expects parameter 1 to be string, array given…” for nested arrays – for example: What modifications should be made to
PHP Difference between array() and []
I’m writing a PHP app and I want to make sure it will work with no errors. The original code: Would the following work with no errors or is not recommended for some reason? Are there any difference? I’ve looked again the data about array() and the short array method with square brackets [] in PHP.net but I’m not sure.
Removing successive duplicate occurrences in an array
Is there any way that I can remove the successive duplicates from the array below while only keeping the first one? The array is shown below: What I want is to have an array that contains: Answer You can just do something like: Assuming you’re not manipulating that array in between you can use current() it’s more efficient than counting
Unsupported operand types
I am working on a shopping cart function for a website and have stumbled across this error: Fatal error: Unsupported operand types in … on line xx I think this may be because I am performing some math between a variable and a value within an array. What I am not sure of is how to perform math on a