PHP array only prints first letter of each item - arrays

The code below is only printing the first letter of each array item. It's got me quite baffled.
require_once 'includes/global.inc.php';
print_r($site->bookmarkTags(1));
$index = 0;
foreach ($site->bookmarkTags(1) as $tag) {
echo $tag['$index'];
$index = $index + 1;
}
print_r return:
Array ( [0] => Wallpapers [1] => Free )
the loop:
WF

Try echo $tag, not $tag['$index']
Since you are using foreach, the value is already taken from the array, and when you post $tag['$index'] it will print the character from the '$index' position :)

It seems you've attempted to do what foreach is already doing...
The problem is that you're actually echoing the $index letter of a non-array because foreach is already doing what you seem to be expecting your $index = $index+1 to do:
require_once 'includes/global.inc.php';
print_r($site->bookmarkTags(1));
$index = 0;
foreach ($site->bookmarkTags(1) as $tag) {
echo $tag; // REMOVE [$index] from $tag, because $tag isn't an array
$index = $index + 1; // You can remove this line, because it serves no purpose
}

require_once 'includes/global.inc.php';
// Store the value temporarily instead of
// making a function call each time
$tags = $site->bookmarkTags(1);
foreach ($tags as $tag) {
echo $tag;
}
This should work. The issue might be because you're making a function call every iteration, versus storing the value temporarily and looping over it.

Related

Can't print contents of hash

Here's how I've set up my hash:
my #keys_i_need = qw(A B C D E F G);
my %keys_i_need = map {$_ => []} #keys_i_need;
foreach my $line (#{$file_arr_ref}) {
my $sub = substr( $line, 0, 1);
if(($sub ne "#") and ($sub ne "")){
my #key_vals = split(/\s+/, $line);
my $key = shift #key_vals;
if(exists $keys_i_need{$key}) {
INFO("key is $key value is " . join(", ", #key_vals));
push (#{$keys_i_need{$key}}, \#key_vals);
DEBUG(Dumper \%keys_i_need);
}
}
}
If I understand this correctly, it's a hash, where each value is an array reference with array references inside the array reference. I don't want to use Dumper because I want to pick out each piece.
I'm trying to read out what's been pulled into the hash but I'm getting an error message that says:
"my" variable $values masks earlier declaration in same statement at /home/rabdelaz/workspace/akatest_5/scripts/Viper/Stragglers.pl line 67.
foreach my $key (keys %$config_options) {
foreach my $arr_ref_of_arr_values (%$config_options{$key}) {
foreach my $values (#$arr_ref_of_arr_values) { #<----------line 67
foreach my $value (#$values) {
INFO("key $key has values $value");
}
}
}
}
This looks right to me. I can't quite figure out what perl is complaining about. Any thoughts?
I see what I did now.
I was confused (once again) by the fact that an array reference is a scalar and not an array. Although, as you can see from my original code I went the wrong direction any way (outside to the hash, rather than inside to the array ref. So my final code should look more like this:
foreach my $key (keys %$config_options) {
foreach my $arr_ref_of_arr_values ($$config_options{$key}) {
foreach my $values (#$arr_ref_of_arr_values) { #<----------line 67
foreach my $value (#$values) {
INFO("key $key has values $value");
}
}
}
}
In the course of debugging, I ended up with this to get the first value of the first array in the first key:
INFO("first value of first array of first key: " . ${${$$config_options{'A'}}[0]}[0]);
I then used this as my guidepost.

php: rename duplicate keys by giving number in foreach

i have a form data and the data's array like this:
$datas=array("x-1","y-2","y-2","y-3","t-1");
my foreach loop:
foreach($datas as $x => $data){
$data=explode("-",$data);
if($data[0]==$data[0]+1){$n=1;}else{$n=0;}
$keys[$x]=$data[0].$n++;
$vals[$x]=$data[1];
}
i couldn't write the true code, my 3rd line is wrong i think (if($data[0]=$data[0]+1){$n="1";}else{$n="";})
so, i wanna rename the duplicate keys by giving number. my output should be like this:
x=1 y1=1 y2=2 y3=2 t1=1
Try
$datas=array("x-1","y-2","y-2","y-3","t-1");
$i=0;
$n=1;
foreach($datas as $x => $data){
$data=explode("-",$data);
$data2=explode("-",$datas[$i+1]);
if($data[0]==$data2[0])
{
$keys[$x]=$data[0].$n;
$n=$n+1;
}
else
{
$keys[$x]=$data[0].$n;
$n=0;
}
$vals[$x]=$data[1];
$i++;
}
This code has error you use = Which will assign value , not compare it.
also n should be integer not string
To fix that
foreach($datas as $x => $data){
$data=explode("-",$data);
if($keys[$x]==$keys[$x+1]){$n=1;}else{$n=0;}
$keys[$x]=$data[0].$n++;
$vals[$x]=$data[1];
}
if($data[0]=$data[0]+1){$n="1";}else{$n="";}
Use == instead of = for the if statements

Write session data with dot notation

I add my session variables like this:
foreach ( $data as $key => $value ) {
$this->Session->write("MyVariable.$key", $value );
}
Is it possible to add element to session variable array without passing the key ?
I mean like this:
$MyArray[] = "apple";
$MyArray[] = "banana";
So is it possible to add like this? Pseudo code:
$this->Session->write('MyVariable'.[], "apple");
$this->Session->write('MyVariable'.[], "banana");
Edit: $data array was for giving an example. The data that will be saved is not array. It is a string. Everytime I add to session variable I don't want to give key by code. I wonder whether is it possible out of the box. In my current codes I make it like this:
$newKey = count( $this->Session->read("MyVariable") );
$this->Session->write("MyVariable.$newKey", "apple");
Hi i guess it should be like this:
foreach ( $data as $key => $value ) {
$this->Session->write('MyVariable.'.$key, $value );
}
You have to place a dot inside the quotation mark.
If you don't want to give the key value each time, then store it as an array like #mark said
$this->Session->write("MyVariable", $data);
If you want to add a new value to the $data array in some other part of your code, you'll have to do something like
$data = $this->Session->read("MyVariable");
$data[] = array('other'=>'value');
$this->Session->write("MyVariable", $data);
Or add the exact key like #mmahgoub said
$this->Session->write("MyVariable".$key, $value);

convert a php array with key in a string?

I am novice in php development and I need your help.
I have read the column of a file and I want to keep in an array
only the distinct values
I do it with this code in a while (fgetcsv) loop
$sex_array = array();
isset($sex_array[$sex])?$sex_array[$sex]++:$sex_array[$sex]=1;
my array is in format
Array ( [man] => 33 [woman] => 141 )
How can I make a variable and when I print it to take
man:33,woman:141
I tried implode but I take 33,141
look at foreach taken from the page:
foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}

Populate Array in Controller Codeigniter

I want to populate array record in controller, what should I do?
$data['list'] = $this->tshirt_model->getAllModelType(0,0,1); //this working
foreach($data['list']->result_array() as $row) :
$info =array('name'=>$row['name']);
endforeach;
print_r($info); die(); //when i tracing, only returned last record. it should be 20 records.
Yes it will be the last record because $info is just array as I can see.
Try using:
$info[] =array('name'=>$row['name']);
in some versions of php you shoud use:
foreach($data['list']->result_array() as $key => $row) :
$info[$key] =array('name'=>$row['name']);
endforeach;

Resources