Can you add element to array with different data type?

Asked By 0 points N/A Posted on -
qa-featured

Can you possibly add an element to an array with different data type such as an array of string added with an integer at the last index of the array?

SHARE
Best Answer by Molina Kevin
Answered By 0 points N/A #89902

Can you add element to array with different data type?

qa-featured

This is so simple. You can add any type of value in a string variable whether it is array or simple variable. You can further change the type of value when you are getting it. There are built functions available to convert any type of value to other value like string to integer.

 

 

Answered By 0 points N/A #197208

Can you add element to array with different data type?

qa-featured

Hellow  Rounthugalog,

The functionality of array is different in different programming language.

But your wish can be done perfectly in all languages.I am giving example of using multiple data type in array  PHP language.

At first you have to write the following code into a text file .Then save it as file_name.php on localhost

C:xampphtdocs

Code for utilising of array in PHP

<?php

$Shifflett= array(

"safa" => "Laurel",

"Laurel" =>"safa",

20 => 500,

500 =>20);

var_dump($Shifflett);

?>

 

After saving the file as php you have  to run on browser it by addressing
as http://localhost/file_name.php
 
I hope your concept will be clear.
Shifflett Laurel
Best Answer
Best Answer
Answered By 0 points N/A #197211

Can you add element to array with different data type?

qa-featured

 

Greetings!
 
You can, but it might be a bad idea, there is probably other way to achieve what you want to achieve. But if you insist, I advise that if you create an object array, you can store any data type you want to store. Do not hesitate to do trial and error, though it might be a long process still it be better and clear because you are the one who figure it out. But if ever you want to use one collection for multiple data types, ensure that you check your data type before casting it, or provide error handling whenever your cast it.
 
Regards!

Related Questions