I have an array in the database. When I used print_r($variable_name), I got the array like this
Array
(
[0] => Array
(
[attribute_name] => Disk space,Color,Processor
)
)
So to get the value of attribute_name I tried this
foreach($attributes_name as $key=>$val) {
echo $val['attribute_name'];
}
Here I got the result like Disk space,Color,Processor. But I want the result should come like a list
<li>Disk Space</li>
<li>Color</li>
<li>Processor</li>
So can someone tell me how to do this?
Try this :
<?php
$arr = array(array("attribute_name" => "Disk space,Color,Processor"));
foreach($arr as $val) {
$resultArr = explode(",",$val['attribute_name']);
foreach($resultArr as $value){
echo "<li>".$value."</li>";
// Add id in li
echo "<li id='".str_replace(" ","_", strtolower($value))."'>".$value."</li>";
}
}
?>
Related
So I'm trying to find the details for a particular item in one array from a different array:
foreach($json_items['result']['items'] as $item)
{
foreach($items_all['items_game']['items'] as $schemaItem)
{
echo $Schema[$item['defindex']];
if($item['defindex'] == $Schema[$item['defindex']])
{
echo "works";
echo $schemaItem['name'].'<br />';
break;
} else {
//echo "not";
}
}
}
defindex is a unqiue ID for the item, Schema is a database type array of item info
but Schema is designed like this:
[1] => Array
(
[name] => Anti-Mage's Glaive
[prefab] => default_item
So 1 here would be the defindex for this item in the Schema array
What can I do so can compare them and get out the info such as name and prefab for the item? Thanks.
Is this inside of a loop through the array? If so, you could try:
foreach ($item as $key => $value){
// Do compare here, $key would be the array index $value the value.
}
You could access the array in $Schema
$item = $Schema[$item['defindex']];
$item_name = $item['name'];
$item_prefab = $item['prefab'];
I have an array stored in my database. So when I try : print_r($arrayname),
It showed the result like this:
Array
(
[0] => Color,Processor
[attribute_name] => Color,Processor
)
I want to get the values of [attribute_name] => Color,Processor .
So far I made the foreach loop like this :
foreach ($arraylist as $name) {
echo $name['attribute_name];
}
But it showing the result like cc. So can someone kindly tell me how to get the values from database?
Actually you don't need to use foreach
You can access it like this.
echo $arraylist["attribute_name"];
Please try this
foreach ($arraylist as $key => $name) {
if($key == 'attribute_name')
echo $name;
}
I have the following array, I need to display the names on a form.
How can I do this via foreach() loop?
What I am trying is:
Array
(
[0] => Array
(
[to_user_name] => John
)
[1] => Array
(
[to_user_name] => Mike
)
)
foreach( $myArray as $subArray ) {
echo $subArray["to_user_name"];
}
It's not clear how you want to use those values in your form, but just echo the values wherever you'd need them, e.g.,
foreach( $myArray as $subArray ) {
echo "<input type=\"text\" name=\"user_name\" value=\"" . $subArray["to_user_name"] . "\">";
}
I was treating it like a single array and then i realized it is a multidimensino array and the following worked, i hope this helps someone else too
foreach ($messages->getMessage('recipient_names') as $section => $items ){
foreach ($items as $key => $value){
echo "$value, ";
}
}
to see the content you can use
print_r($your_array);
For developing purpose you need to use for/foreach loop
foreach($your_array as $array_temp)
{
foreach($array_temp as $item)
{
echo $item;
}
}
I want to store an array of checklist into a string in a database. Below this is the code in the interface which display a list of room with a dropdown checklist for each room.
while($rows = mysql_fetch_array($query)){
<!-- Apply dropdown check list to the selected items -->
<script type="text/javascript">
$(document).ready(function(){$("#<?php echo $bnum; ?>").dropdownchecklist( { width: 300 } );});
</script>
<!-- This is the JQuery Dropdown checklist !-->
<select id="<?php print $bnum;?>" multiple="multiple" name="dietdata[]" >
<option value="0"></option>
<?php
//query drop-down list
$sqlakh="select diet_id,diet_name from bmsdb.diettypes";
$resultakh=mysql_query($sqlakh);
while($rowsakh= mysql_fetch_array($resultakh)) { ?>
<option value='<?php echo $rowsakh['diet_id'].'|'.$bnum; ?>'><?php echo $rowsakh['diet_name']; ?>
</option>
<?php }
}//end while ?>
</select>`
When I submit this form in the server side, this is what I do to extract the data from the array of the dropdown checklist:
$data2 = $_POST['data2']; //total no of data
$dietdata= $_POST['dietdata']; //diet data
$roomlist= $_POST['data3']; //patient room number
for($k=0; $k<=sizeof($data3); $k++){
$dietarray= $dietdata[$k];
$separatediet= (explode('|',$dietarray));
$output_array[]=array("diet"=>$separatediet['0'],"room"=>$separatediet['1']);
for($j=0; $j<=sizeof($data3); $j++){
if($output_array[$k][room]== $roomlist[$j]){
$rekod[$output_array[$k][room]]= $output_array[$k][diet];
}
}
}print_r($rekod);
The print_r output when I submitting the form is like this:
Array ( [501] => 3 [502] => 4 [] => )
Then I extract the array using implode to separate the room and the diet type.
Array ( [0] => 2|501 [1] => 3|501
[2] => 3|502 [3] => 4|502 )
What I want is for the array to be like this. Is there any way for me to make the array to be like this, or a better structure to use?
Array ( [501] => 2,3) //2,3 is the diet type group together after being extracted from the array above
[502] => 3,4 )
Assuming you're keeping all the code up to the point where you're imploding to generate your final array:
Array ( [0] => 2|501 [1] => 3|501
[2] => 3|502 [3] => 4|502 )
You could create the array you're asking for like this:
$out = array(); // The final output array
foreach ($your_array as $item) {
$vals = explode('|', $item);
if (!isset($out[$item[1]])) $out[$item[1]] = array();
$out[$item[1]][] = $item[0];
}
foreach ($out as &$item) {
$item = implode(',', $item);
}
This is my final code. Which generata output like this
Array ( [501] => 2,3,4 [502] => 3,4 [503] => 5,6 )
Thanks to Hibiscus for your help.
$out = array(); // The final output array
foreach ($status as $item) {
$vals = explode('|', $item);
if (!isset($out[$vals[1]])) $out[$vals[1]] = array();
$out[$vals[1]][] = $item[0];
}
$item=array();
foreach ($out as &$item)
{
$item = implode(',', $item);
}
$diet = array();
$data3 = $_POST['data3'];
for($h=0; $h<=$data2; $h++)
{
$data3 = $_POST['data3']; //patient bnum
$data3 = $data3[$h];//bnum
if(!empty($out[$data3]))
{
$diet[$data3] = $out[$data3];
}
} print_r($diet);
I am trying to build an array and I am looping through the values of an XML document, I have everything pulling out great using xpath, here's my code:
function parseAccountIds($xml) {
$arr = array();
foreach($xml->entry as $k => $v) {
$acctName = $v->title;
$prop = $v->xpath('dxp:property');
foreach($prop as $k1 => $v1) {
if($v1->attributes()->name == "ga:accountId")
$acctId = (string) $v1->attributes()->value;
else if($v1->attributes()->name == "ga:profileId")
$profileId = (string) $v1->attributes()->value;
}
echo "profile id ".$profileId;
echo "<BR>";
echo "acctName ".$acctName;
echo "<BR>";
$subArray = array($acctName => $profileId);
print_r($subArray);
$arr[] = array($acctId => $subArray);
}
print_r($arr);
return json_encode($arr);
}
The most important bit is where I print_r subArray. I can see acctName and profileId print, but then subArray is empty. For Example:
profile id 45580
acctName accountName1
Array
(
)
profile id 4300
acctName accountName2
Array
(
)
profile id 4338
acctName accountName3
Array
(
)
How are these values not being inserted? I've been looking at the code for a while now, and I'm a bit confused.
Any suggestions would really help,
Thanks!
try this:
$subArray[$acctName] = $profileId;
instead of
$subArray = array($acctName => $profileId);
$v->title is actually a SimpleXMLObject still!
I forgot to cast it as a string, when I tried to make it the index in the array, it freaked out, geez I spent a whole hour on this!
Thanks for your suggestions guys :P