Skip to content
Advertisement

Tag: arrays

Alternative for define array php

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

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

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

Advertisement