Please help in code that i am unable to print values of associative array after extracting itself
class display{
protected $variables = array();
function set($name,$value) {
$this->variables[$name] = $value;
}
function render(){
extract($this->variables);
// ?? to print values of $variable array
}
foreach($this->variables as $key => $value) {
echo "{$key}: {$value}\n";
}
And how do you try to print the values? The array itself (it's $varables, not $variable, btw) should not be affected.
Update: For what I can tell by your reply to the other answer, you do not really need to extract array. extract jusst puts the variables into local namespace where they will be harder to enumerate. What you need is to use array as is.
foreach($this->variables as $k => $v) echo "$k: $v\n";
or whatever you want to do with them.
if you are using classes, u will need to have something like
var $variables = array(); or
public $variables = array();
and if you are using structured , you will need to do
global $variables;
inside the function .. but as u are using $this-> it indicates ur using a class. You will have to put in some more code in here to make the situation clear.
Related
I have this case when I have array_push inside function and then I need to run it inside foreach filling the new array. Unfortunately I can't see why this does not work. Here is the code:
<?php
$mylist = array('house', 'apple', 'key', 'car');
$mailarray = array();
foreach ($mylist as $key) {
online($key, $mailarray);
}
function online($thekey, $mailarray) {
array_push($mailarray,$thekey);
}
print_r($mailarray);
?>
This is a sample function, it has more functionality and that´s why I need to maintain the idea.
Thank you.
PHP treats arrays as a sort of “value type” by default (copy on write). You can pass it by reference:
function online($thekey, &$mailarray) {
$mailarray[] = $thekey;
}
See also the signature of array_push.
You need to pass the array by reference.
function online($thekey, &$mailarray) {
I am calling a function like:
get(array('id_person' => $person, 'ot' => $ot ));
In function How can I access the key and value as they are variable?
function get($where=array()) {
echo where[0];
echo where[1];
}
How to extract 'id_person' => $person, 'ot' => $ot without using foreach as I know how many key-values pairs I have inside function?
You can access them via $where['id_person'] / $where['ot'] if you know that they will always have these keys.
If you want to access the first and second element, you can do it like this
reset($where)
$first = current($where);
$second = next($where);
Couple ways. If you know what keys to expect, you can directly address $where['id_person']; Or you can extract them as local variables:
function get($where=array()) {
extract($where);
echo $id_person;
}
If you don't know what to expect, just loop through them:
foreach($where AS $key => $value) {
echo "I found $key which is $value!";
}
Just do $where['id_person'] and $where['ot'] like you do in JavaScript.
If you do not care about keys and want to use array as ordered array you can shift it.
function get($where=array()) {
$value1 = array_shift($where);
$value2 = array_shift($where);
}
I have a google language translate PHP class you can see here
My array file like this:
$lang['FORUM_LOCK'] = 'Lock';
$lang['FORUM_EDIT'] = 'Edit';
$lang['FORUM_POST'] ='Post';
...
I want to loop through and translate all array values and write to file.
I tried all kinds of methods but cant figure it out :(
Im sure someone has done this before?
You may want to use a foreach construct to iterate through all values of $lang.
Something like this :
$lang_fr = array();
foreach($lang as $key => $val) {
$lang_fr = $gt->translate($val , "en", "fr");
}
You can write then it to a PHP file using fwrite() using the same construct :
fwrite($fp, "\$lang_fr['$key'] ='$val';\n");
Be wary of specials characters though. You may want to use addslashes().
Try to extend Google Translate class:
class ExtendedTranslate extends GoogleTranslateWrapper {
public function translateArray($array, $fromLanguage, $toLanguage) {
foreach ($array as &$item) {
$item = $this->translate($item, $fromLanguage, $toLanguage);
}
return array();
}
}
Is it possible to have an AND in a foreach loop?
For Example,
foreach ($bookmarks_latest as $bookmark AND $tags_latest as $tags)
You can always use a loop counter to access the same index in the second array as you are accessing in the foreach loop (i hope that makes sense).
For example:-
$i = 0;
foreach($bookmarks_latest as $bookmark){
$result['bookmark'] = $bookmark;
$result['tag'] = $tags_latest[$i];
$i++;
}
That should achieve what you are trying to do, otherwise use the approach sugested by dark_charlie.
In PHP 5 >= 5.3 you can use MultipleIterator.
Short answer: no. You can always put the bookmarks and tags into one array and iterate over it.
Or you could also do this:
reset($bookmarks_latest);
reset($tags_latest);
while ((list(, $bookmark) = each($bookmarks_latest)) && (list(,$tag) = each($tags_latest)) {
// Your code here that uses $bookmark and $tag
}
EDIT:
The requested example for the one-array solution:
class BookmarkWithTag {
public var $bookmark;
public var $tag;
}
// Use the class, fill instances to the array $tagsAndBookmarks
foreach ($tagsAndBookmarks as $bookmarkWithTag) {
$tag = $bookmarkWithTag->tag;
$bookmark = $bookmarkWithTag->bookmark;
}
you can't do that.
but you can
<?php
foreach($keyval as $key => $val) {
// something with $key and $val
}
the above example works really well if you have a hash type array but if you have nested values in the array I recommend you:
or option 2
<?php
foreach ($keyval as $kv) {
list($val1, $val2, $valn) = $kv;
}
No, but there are many ways to do this, e.g:
reset($tags_latest);
foreach ($bookmarks_latest as $bookmark){
$tags = current($tags_latest); next($tags_latest);
// here you can use $bookmark and $tags
}
No. No, it is not.
You'll have to manually write out a loop that uses indexes or internal pointers to traverse both arrays at the same time.
Yes, for completeness:
foreach (array_combine($bookmarks_latest, $tags_latest) as $bookm=>$tag)
That would be the native way to get what you want. But it only works if both input arrays have the exact same length, obviously.
(Using a separate iteration key is the more common approach however.)
Every day , I have this pattern
- an array of objects
- i make a loop to traverse the array
foreach($arr as $obj){
$arrIds[] = $obj->Id;
$arrNames[] = $obj->Name;
}
I could build a function like arrayFromProperties($Array,$ProperyName) but I was wondering if you know a native php function to do this, or something similar, without having to write a new class/function for this.
As far as I know, you'll have to do this by your own.
Have you tried get_object_vars?
There isn't. It's not very hard to write, either. Feel free to reuse or adapt the following to your liking:
function soa_of_aos($aos)
{
$soa = array();
foreach ($aos as $i => $struct)
foreach ($struct as $field => $value)
if (!isset($soa[$field])) $soa[$field] = array($i => $value);
else $soa[$field][$i] = $value;
return $soa;
}