In my zend application i have a form that contains an array of elements like that:
ini1[0]
ini1[1]
...
To get they values i use:
$value = $form->ini1->getValue();
echo $value[0];
echo $value[1];
...
But i don't know how to set values to each elements of this array.
There's any way?
================================================================================
Code of element creation
$element['ini1'] = new Zend_Form_Element_Text('ini1');
$element['ini1']->setAttrib('maxLength', '5')
->setAttrib('class', 'horaTurno')
->setValue('00:00');
I'm creating a manual form, so in my form.phtml i have a for loop that create 7 elements like this:
for($i = 0; $i < 7; $i++){
echo $this->form->ini1
->setAttrib('name', 'ini1['. $i .']')
->setAttrib('id', 'ini1['. $i .']');
}
With a foreach-loop (PHP.net: foreach) you can iterate over your array and set a value on each item:
foreach($value as $item) {
$item->setValue('yourValue');
}
Related
I am having an php session array like
["cart"]["123"] = "Biscuit"
["cart"]["124"] = "Jam"
If I want to access the 2nd element I will access array_values($_SESSION["cart"])[$i]
where $i runs in for loop. If I want to get the values "123" and "124", how can i achieve it in a for loop with only "cart" and $i..?
foreach($_SESSION['cart'] as $key => $value)
{
echo $key; // your 123 or 124 key
}
This is an associative array easiest and best way is foreach because have to deal with key-value pairs rather than indexes(numbers).
foreach($arr['cart'] as $key => $val){
echo "$key<br/>";
}
I used a variable to hold values($arr)
But you can try for loop also:
$keys = array_keys($arr['cart']);
for ($keyindex = 0; $keyindex < count($keys); $keyindex++) {
$key = $keys[$keyindex];
echo $key."<br>";
}
I have converted Excel data into Php array using toArray()..
actually my result is
My question how to get value from this Multidimensional array? and also how to i get header and value from array in separately?
you can access value, by following the path of the array. so in your case :
$yourArrayName['Worksheet'][0][0], will return SNo
Get one value:
print_r($your_array['Worksheet'][0]);
Get values with loop:
foreach ($your_array as $key => $value)
{
echo $key; // 'Worksheet'
print_r $value;
}
If you are trying to extract all the values of specific key then you should use for or foreach or while loop...
its something like
<?php
$array = YOUR ARRAY;
$c=count($array);
for ( $i=0; $i < $c; $i++)
{
echo $array[$i]['StudentName'];
}
?>
else if you want to get a specific value manually You can easily traverse to your value as
$array = YOUR ARRAY
echo $ara['Worksheet'][0]['StudentName']
How do I delete the whole line from an array? When the delete-button is pressed it should delete the whole line.
my array looks like that:
$liste[0][0] = email-user1
$liste[0][1]= password-user1
$liste[1][0] = email-user2
$liste[1][1]= password-user2
So if I delete the user one, the user2 should just take the place from user1(which should just disappear).
if (isset($_GET['delete'])){
$id=key($_GET['delete']);
for ($i = 0; $i < count($liste); $i++){
if ("$i"=="$id"){
unset($liste[$id][0]);
unset($liste[$id][1]);
unset($liste[$id][2]);
}
else{
}
}
update
I'm using array_splice($liste, $id, 1); now but everytime I try to save it to the file I get an error: implode(): Invalid arguments passed. For saving it to the file, I use the following function:
function saveDataToFile($fileName, $liste){
$file=fopen($fileName,"w");
for ($i = 0; $i < count($liste); $i++) {
$zArray=$liste[$i];
$zeile=implode("|", $zArray);
if(strlen($zeile) > 0){
$zeile=$zeile."\r\n";
fwrite($file, $zeile);
}
}
fclose($datei);
}
Try the below code:
$liste[0][0] = "email-user1";
$liste[0][1]= "password-user1";
$liste[1][0] = "email-user2";
$liste[1][1]= "password-user2";
$liste[2][0] = "email-user3";
$liste[2][1]= "password-user3";
unset($liste[1]); // say you want to delete this row
$new_arr = $liste;
unset($liste);
$i=0;
foreach($new_arr as $value){
$liste[$i] = $value;
$i++;
}
You can use array_splice() method:
array_splice($liste, $id, 1);
$liste[0][0], $liste[0][1] and $liste[0][2] are in real nothing else than a value array(value, value, value) (the inner array) which is assigned to $liste[0] (the outer array)
unsetting this (outer) array value $liste[0] is enough:
unset($liste[$id]);
If you care about the keys of this array (I see you loop from 0..n), you need to reindex your array using:
$liste = array_values($liste);
This will make your array behaving more like arrays normally do in other programming languages
Good practice is to use foreach instead of for. In this case you don't need to reindex:
for ($liste as $key=>$value){
if ("$key"=="$id"){
unset($liste[$key]);
}
But anyway you don't have to loop through an array just for finding a key. It's enough doing this:
if (isset($liste[$id])) { /* optional: check if the key exists */ }
unset($liste[$id]);
I have these arrays and i want to loop through every array by creating a dynamic variable that put their name in foreach loop automatically something like this
foreach ($activities.$i as &$activity) { //$i = 1,2,3,4..
//code
}
//activities
$activities1 = $_POST["activities1"];
$activities2 = $_POST["activities2"];
$activities3 = $_POST["activities3"];
$activities4 = $_POST["activities4"];
Easier method is to simply use the array naming hack:
<input name="activities[1]" ..>
<input name="activities[2]" ..>
<input name="activities[3]" ..>
which makes $_POST['activities'] you array:
foreach($_POST['activities'] as $i => $value) {
// $i -> 1,2,3,4
}
But if you insist on embedding the index inside the key's name, then:
foreach(range(1,4) as $i) {
foreach($_POST["activities{$i}"] as $value) {
...
}
}
I have created a shortlist feature which acts a bit like a shopping cart. I output the items in the shortlist by:
$i = 0;
while ($i < $countArray){
echo $_SESSION['shortlistArray'][$i]." <a href='shortlistRemoveItem.php?arrayID=$i'>[x]</a><br />";
$i++;
}
and delete an item by
$arrayID = $_GET["arrayID"];
unset($_SESSION['shortlistArray'][$arrayID]);
The problem is that when I delete an item from an array such as $_SESSION['shortlistArray'][2] the output is all messed up as the array is no londer sequential. Should I recode the way my array is outputted or the way I am deleting an item from an array?
The most efficient solution would be simply changing the way your array is outputted:
foreach($countArray as $key => $item){
echo $_SESSION['shortlistArray'][$key]." <a href='shortlistRemoveItem.php?arrayID=$key'>[x]</a><br />";
}
If you insist on changing the way you are deleting an item from the array, consider this alternative:
$arrayID = $_GET["arrayID"];
$tempArray = array();
foreach($countArray as $key => $item){
if($arrayID == $key) continue;
$tempArray[] = $item;
}
$_SESSION['shortlistArray'] = $tempArray;
I' recommend the first option though.