Asked  7 Months ago    Answers:  5   Viewed   46 times

If I define an array in PHP such as (I don't define its size):

$cart = array();

Do I simply add elements to it using the following?

$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;

Don't arrays in PHP have an add method, for example, cart.add(13)?



Both array_push and the method you described will work.

$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc

//Above is correct. but below one is for further understanding
$cart = array();
    $cart[] = $i;  
echo "<pre>";
echo "</pre>";

Is the same as:

$cart = array();
array_push($cart, 13);
array_push($cart, 14);

// Or 
$cart = array();
array_push($cart, 13, 14);
Wednesday, March 31, 2021
answered 7 Months ago

The same way you add to an array when the key is not a concern:

$data[$state]['cities'][] = $city;
Wednesday, March 31, 2021
answered 7 Months ago

In your foreach loop, call $img->setAttribute('class', 'someclass');. This should do the trick. See more at

Then you need to save the modified document back using $article_header = $doc->saveXml();.

Wednesday, March 31, 2021
answered 7 Months ago

Not exactly an answer to your question ...but: Keep the array, don't pollute the (global) variable namespace.

$englishMessages = array('helloWorld'=>'Hello World');
$spanishMessages = array('helloWorld'=>'Hola Mundo');

// wrap this in a nice function/method
$lang = $englishMessages;
// then use $lang for the output
'<span id="some">'. $lang['helloWorld'] .'</span>';

Some variations on the same theme:

function getMessages($language) {
  static $l = array(
    'en'=> array('helloWorld'=>'Hello World'),
    'es' => array('helloWorld'=>'Hola Mundo')
  // <-- add handling reporting here -->
  return $l[$language];

$lang = getMessages('en');
echo '<span id="some">'. $lang['helloWorld'] .'</span>';


function __($language, $id) {
  static $l = array(
    'en'=> array('helloWorld'=>'Hello World'),
    'es' => array('helloWorld'=>'Hola Mundo')
  // <-- add error handling here -->
  return $l[$language][$id];

echo '<span id="some">'. __('es', 'helloWorld') .'</span>';

You might also be interested in

Saturday, May 29, 2021
answered 5 Months ago
<form method='post' id='userform' action='thisform.php'> <tr>
    <td>Trouble Type</td>
    <input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Three'>3
    </td> </tr> </table> <input type='submit' class='buttons'> </form>

if (isset($_POST['checkboxvar'])) 

You pass the form name as an array and then you can access all checked boxes using the var itself which would then be an array.

To echo checked options into your email you would then do this:

echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want

Please keep in mind you should always sanitize your input as needed.

For the record, official docs on this exist:

Tuesday, June 1, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :