Site Owners Forums - Webmaster Forums

Site Owners Forums - Webmaster Forums (http://siteownersforums.com/index.php)
-   PHP / mySQL (http://siteownersforums.com/forumdisplay.php?f=10)
-   -   How to use associative array in php? (http://siteownersforums.com/showthread.php?t=100193)

noahwilson 08-01-2013 01:09 PM

How to use associative array in php?
 
Hello Guys
How to use associative array in php? please let me help and share you suggestion.
Thanks.

mridul 11-05-2013 11:49 PM

$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

and you will get output like this:-
$cars1="Volvo";
$cars2="BMW";
$cars3="Toyota";

lampdev112 12-18-2013 05:38 AM

Creating Associative Arrays gives you a good way to use labels as the key in the array items instead of using numbered keys that increment automatically. This way you can use a word that has meaning to you as key.

This is how we create an Associative Array holding item names(labels) and their prices:
// create an associative array that is a cart holding items and prices
$cart_array = array(
"cereal" => "5.00",
"coffee beans" => "2.50",
"bananas" => "3.50",
"onion" => "1.00",
"lettuce" => "2.40",
"tomato" => "1.00",
);
// and here is how you can output any key's value you choose
echo "Bananas cost $" . $cart_array["bananas"] . " at this store.";
echo "

";
// And here is how you can print the whole array for viewing and debugging
print_r($cart_array);
?>

Die_heart 01-02-2014 12:14 AM

// create an associative array that is a cart holding items and prices
$cart_array = array(
"cereal" => "5.00",
"coffee beans" => "2.50",
"bananas" => "3.50",
"onion" => "1.00",
"lettuce" => "2.40",
"tomato" => "1.00",
);
// and here is how you can output any key's value you choose
echo "Bananas cost $" . $cart_array["bananas"] . " at this store.";
echo "

";
// And here is how you can print the whole array for viewing and debugging
print_r($cart_array);
?>

MichaelHolloman 01-13-2014 10:09 PM

Associative arrays use named keys that you assign to them. For example:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
You can also use a loop through associated array.
To get any assistance related to IT support you can contact with Network Fish London. They will provide you excellent Tech Support.

Luca tall 01-16-2014 11:06 PM

Examples of Associative array:

$course = array ("C++"=>"Rs.1400", "Java"=>"Rs.1600","PHP"=>"Rs.2400","Netwoking"=>"Rs.3000");
echo "c++ course is" .$course['C++'];
?>

Here key= C++ = Rs 1400.
Key can either be integer or a string.

alligatortek001 03-17-2014 02:49 AM

// create an associative array that is a cart holding items and prices
$cart_array = array(
"cereal" => "5.00",
"coffee beans" => "2.50",
"bananas" => "3.50",
"onion" => "1.00",
"lettuce" => "2.40",
"tomato" => "1.00",
);
// and here is how you can output any key's value you choose
echo "Bananas cost $" . $cart_array["bananas"] . " at this store.";
echo "

";
// And here is how you can print the whole array for viewing and debugging
print_r($cart_array);
?>

tomsmith 04-15-2014 03:28 AM

// create an associative array that is a cart holding items and prices
$cart_array = array(
"cereal" => "5.00",
"coffee beans" => "2.50",
"bananas" => "3.50",
"onion" => "1.00",
"lettuce" => "2.40",
"tomato" => "1.00",
);
// and here is how you can output any key's value you choose
echo "Bananas cost $" . $cart_array["bananas"] . " at this store.";
echo "

";
// And here is how you can print the whole array for viewing and debugging
print_r($cart_array);
?>
check this code and reply with output http://tutorialterminal.blogspot.in/

kevinkrieger 05-26-2014 08:56 PM

$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);

var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>


All times are GMT -7. The time now is 10:11 AM.


Powered by vBulletin Copyright © 2020 vBulletin Solutions, Inc.