I am looking to retrieve specific data from an array such as
$array[] = "test type: human; Including all the test; number of participants 2 persons + additional $50";
I am considering only the information of the following type:
output:
array[1] test type: human;
array[2] 2
I should able to display only these information in the output. Test type will be varying for each case and participants number will be different for other orders. I would like to get only the above basic information from the given array. I looked into exploding the given array by semi colon and later using the sub string to get the desired result. But looking for any efficient way to retrieve the data. Any help or suggestions is truly appreciated.
You seem to misunderstand array versus string.
Array
http://php.net/manual/en/language.types.array.php
String (in variable)
http://www.php.net/manual/en/language.variables.basics.php
You can use the variable and string approach you currently have, but in honesty you're setting yourself up for a world of pain by having what is affectively different keys and values in single variable/string. This is what arrays are for!
A string is a single entity, maybe many chars or words etc, but should be used when you want one thing. $TheName = "Bob";
An array is when you want to store, manipulate and make use of several strings or data sets in one go. ie a load of database results.
As such, it seems to me you need to use an array. For your code to be an array, you would do something such as:
$array = array(
"test type" => "human",
"number of participants" => "2 persons",
"additional" => "$50"
);
The KEY of the array is the first text in "", then you assign a VALUE to that KEY by using =>, then the next text in quotes is the VALUE. You separate each KEY and VALUE pair with a comma, the final KEY and VALUE pair in the array does not need a comma.
This above is created manually, however you can create this in a loop from a database query. Replacing the manually assigned KEY names as above with perhaps the database column name, and the VALUE would likely be the field's data.
As for your question "get specific data from array", you would then loop the array, echoing or assigning each KEY's VALUE to a variable.
eg
foreach ($array as $key => $value)
{
echo "The ."$key." is ".$value;
}
You really need to read some tutorials on the basics. This is a Q&A site and while we'll help you with questions you have, you do need to understand the basics of what you're coding before you can ask a question about it.
As with above, the answer then is not an answer to a question but a guide or tutorial that can already be found on Stack Exchange sites, forums and tutorials :)
I have two possible ways to get to your result for sure there are many others but its not the easyest.
if your STRING never changes you can use option 2, else option 1
<?php
$array[] = "test type: human; Including all the test; number of participants 2 persons + additional $50";
// option 1
if (preg_match_all('#test type:(.*);#Usi', $array[0], $a)) {
echo 'test type --> '.trim(implode($a[1]));
}
if (preg_match_all('#participants(.*)persons#Usi', $array[0], $a)) {
echo '<br />participants --> '.trim(implode($a[1]));
}
// end option 1
//just to show string as real array
$arr = explode(' ', $array[0]);
echo '<pre>';
var_dump($arr);
echo '</pre>';
echo '<br /><br /><br />';
// end show
//option 2 , only works if the array, see var_dump from above is always like that
echo 'test type --> '.substr($arr[2], 0, -1);
echo '<br />participants --> '.$arr[10];
// end option 2
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With