Asked  2 Years ago    Answers:  5   Viewed   124 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);
Friday, October 14, 2022

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

$data[$state]['cities'][] = $city;
Tuesday, November 29, 2022

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

Sunday, September 18, 2022
<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:

Wednesday, December 21, 2022

You need to use std::vector::push_back() instead:

while(cin >> x)
//         ^^^^^^^^^

and not std::vector::insert(), which, as you can see in the link, needs an iterator to indicate the position where you want to insert the element.

Also, as what @Joel has commented, you should remove the parentheses in your vector variable's definition.

std::vector<float> myVector;

and not

std::vector<float> myVector();

By doing the latter, you run into C++'s Most Vexing Parse problem.

Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages