QPnoclue
0
Q:

php new array


<?php

$cars = array("Volvo", "BMW", "Toyota");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

?>
 
8
<?php 
$fruits = array("Apple", "Banana", "Orange");
echo '<pre>';
print_r($fruits);
echo '</pre>';
?>
11
#Arrays

<?php
    #array is a variable that holds multiple values
    /*
    Three types of arrays
        - Indexed
        - associative
        - Multi-dimensional
    */
    // Indexed array is the most common and easiest
    $people = array('Kevin', 'Jeremy ', 'Sara');
    $ids = array(23, 55, 12);
    $cars = ['Honda', ' Toyota', 'Ford'];  // also an array
    //add to an array
    $cars[3] = ' Audi';
    // you can use empty brackets and it will be added to the last one
    $cars[] = ' Chevy';

    echo $people[1];
    echo $ids[1];
    echo $cars[1];    
    echo $cars[3];
    echo $cars[4];
    echo count($cars);
    //you can also print the entire array
    print_r($cars);
    //to look at data type
    echo var_dump($cars);

    //Associative Arrays key pairs
    $people1 = array('Kevin' => 35, 'Jeremy' => 23, 'Sara' => 19);
    $ids1 = array(23 => 'Kevin', 55 => 'Jeremy', 12 => 'Sara');
    echo $people1['Kevin'];
    echo $ids1[23];
    //add to these types of arrays
    $people1['Jill'] = 44;
    echo $people1['Jill'];
    print_r($people1);
    var_dump($people1);

    //Multi-Dimensional Arrays aka an array within an array
 
    $cars2 = array(
        array('Honda',20,10),
        array('Toyota',30,20),
        array('Ford',23,12)
    );

    echo $cars2[1][0];

    
?>
5
$healthStructureData = [
            'product' => $product,
            'website_data' => $website_data,
            'total_keywords' => $total_keywords,
            'certificate_status' => $certificate_status,
            'total_user_websites' => $total_user_websites,
        ];

foreach ($healthStructureData as $key => $value) {
  $$key = $value;
}
1
$array = ['cheese', 'ham', 'potato'];

var_dump($array);
6

 <?php array(); ?>
 
0

New to Communities?

Join the community