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)?

 Answers

89

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();
for($i=0;$i<=5;$i++){
    $cart[] = $i;  
}
echo "<pre>";
print_r($cart);
echo "</pre>";

Is the same as:

<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);

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

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

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

In your foreach loop, call $img->setAttribute('class', 'someclass');. This should do the trick. See more at http://docs.php.net/manual/en/domelement.setattribute.php

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

Wednesday, March 31, 2021
 
Sauleil
answered 7 Months ago
94

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>';

or

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 http://docs.php.net/gettext

Saturday, May 29, 2021
 
ioleo
answered 5 Months ago
54
<form method='post' id='userform' action='thisform.php'> <tr>
    <td>Trouble Type</td>
    <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>

<?php 
if (isset($_POST['checkboxvar'])) 
{
    print_r($_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: http://php.net/manual/en/faq.html.php#faq.html.arrays

Tuesday, June 1, 2021
 
Francisunoxx
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 :