im currently trying to generate a dynamic subnavigation. Therefor i pull data off of the database and store some of it an an array. Now i want to do an foreach in this array. Well as far as I know, this isn't possible.
But maybe I'm wrong. I would like to know if it would be possible, and if so how do i do it?
This is my code, which wont work since it hands out an syntax error.
$this->subnav = array(
'' => array(
'Test Link' => 'login.php',
'Badged Link' => array('warning',10023,'check.php')
),
'benutzer' => array(
'Benutzer suchen' => '/mother/index.php?page=benutzer&subpage=serach_user',
'Benutzer hinzufügen' => '/mother/index.php?page=benutzer&subpage=add_user',
'Rechtevergabe' => '/mother/index.php?page=benutzer&subpage=user_rights'
),
'logout' => array(
'Login' => '/mother/login.php',
'Logout' => '/mother/index.php?page=logout'
),
'datenbank' => array(
(foreach($this->system->get_databases() as $db){array($db->name => $db->url)}),
'Deutschland' => '/mother/login.php',
'Polen' => '/mother/index.php',
'Spanien' => '/mother/index.php',
'Datenbank hinzufügen' => '/mother/index.php?page=datenbank&subpage=add_database'
)
);
}
You can't place foreach loop inside an array like this. You can do something like this though.
foreach($this->system->get_databases() as $db)
{
$this->subnav['datenbank'][$db->name] = $db->url;
}
This is not possible. but you can do it in other way like you can place the foreach outsite and top of this array and assign to an array and then you can use that array variable.
e.g.
$arrDB = array();
foreach($this->system->get_databases() as $db) {
$arrDB[$db->name] = $db->url;
}
Now assign it to:
'datenbank' => $arrDB
Related
I am new in PHP. I am working for send data using PHP API. Its method is like below
$createContact->setCustomFieldValues(
[
array(
'customFieldId' => 'pcVFD6',
'value' => array('35')
),
array(
'customFieldId' => 'pcVnzW',
'value' => array('37')
)
]
);
I have data in array like this
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
I want pass this values to above function but I am not getting idea about proper way to do it.
I have tried something like this
foreach ($aa as $key => $value){
$createContact->setCustomFieldValues([
array(
'customFieldId' => $key,
'value' => array($value)
)
]
);
}
its working but passing only last one array to API. Anyone here can please help me for achieve my goal?
Thanks!
You need to transform the array before you pass it to setCustomFieldValues. You don't want to be calling setCustomFieldValues multiple times as your current attempt does, as that's not equivalent to the original. You just need to change the structure of the array ahead of time.
For example:
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
$transformedArr = array();
foreach ($aa as $key => $value){
$transformedArr[] = array(
'customFieldId' => $key,
'value' => array($value)
);
}
$createContact->setCustomFieldValues($transformedArr);
I have the following array built:
$arr['ticket']['custom_fields'][] = array('id'=>$key,'value'=>$value);
I want to dump the contents of that array inside the following section, rather than stating each index such as $arr['ticket']['custom_fields'][0]. When I try a for each loop, it fails using var_dump or even var_export? Any ideas on how I might do this?
$create = json_encode(array('ticket' => array('subject' => $arr['z_subject'],
'comment' => array( "body"=> $arr['z_description']), 'requester' =>
array('name' => $arr['z_name'], 'email' => $arr['z_requester']),
'custom_fields' => array(**$arr['ticket']['custom_fields'][]**))));
I am using this code below but although I have the same domain twice only one output it echoing on the page.
$urls = array(
'phpclasses.org' => 'phpclass',
'phpclasses.org' => 'php',
'php.com' => 'php'
);
$RankChecker=new RankChecker(1,5);
foreach($urls as $url => $keyword) {
$result=$RankChecker->find($url,$keyword);
if ($result!==false) {
echo $url . " is found at page number ".$result["page"].".";
echo '<br>';
}
}
I need to be able to use same url multiple times.
Why is this not working on the code above?
You can't have an array with the same key twice. How would you access the value you wanted by key if it wasn't unique?
You need to try some other structure for your array.
E.g
$urls = array(
0 => array('url' => 'phpclasses.org', 'keyword' => 'phpclass'),
1 => array('url' => 'phpclasses.org', 'keyword' => 'php'),
2 => array('url' => 'php.com', 'keyword' => 'php')
);
Then you would need to call your function like $result=$RankChecker->find($keyword['url'],$keyword['keyword']); in the loop.
You are overwriting the value of the key 'phpclasses.org'. Doing
$urls = array(
'phpclasses.org' => 'phpclass',
'phpclasses.org' => 'php'
);
is essentially the same as
$urls = array(
'phpclasses.org' => 'phpclass'
);
$urls['phpclasses.org'] = 'php';
In the end, there will only be one key for 'phpclasses.org' in the array
Think about changing it to a multidimensional array, removing the dependance on the key
I am not very familiar with this type of array. How do i push another set of data in this type of array?
$array = array(
array(
'service_name' => $row['application_name'],
'html_id' => $row['html_id'],
'url' => $row['url']
)
);
as far as I understood-
$array[]= array(
'service_name' => $row['application_name'],
'html_id' => $row['html_id'],
'url' => $row['url']
);
will add another section, same as first
This adds another array to the outhermost array:
$array[] = array('key' => 'value', 'key2' => 'value2');
Always take the path of the PHP man my friend :) http://php.net/manual/en/function.array-push.php
it depends on which array you want to put data into :) - the first one is a classical array and the included one is an associative array
The answer really depends on what the application or API is expecting but to take a wild stab at it, something like -
<?php
$array = array (
array (
'service_name' => $row['application_name'],
'html_id' => $row['html_id'],
'url' => $row['url']
),
array (
'service_name' => 'app2',
'html_id' => 'htmlid2',
'url' => 'url2'
)
);
I have array like this:
array(
'person0' => array( 'name'=>'name0','address'=>'address0' ),
'person1' => array( 'name'=>'name1','address'=>'address1' ),
'person2' => array( 'name'=>'name2','address'=>'address2' )
);
I want to change it like this. (just append a new value in each sub-array)
array(
'person0' => array( 'name'=>'name0','address'=>'address0','type'=>'type0' ),
'person1' => array( 'name'=>'name1','address'=>'address1','type'=>'type1' ),
'person2' => array( 'name'=>'name2','address'=>'address2','type'=>'type2' )
);
Is there any related function in php to perform this action? What is the shortest way to do this. Is it possible without loop?
Thanks
Browse the PHP manual when you wonder if a function exists to do something... it probably does.
http://www.php.net/manual/en/function.array-walk.php
http://php.net/manual/en/function.array-map.php
I'd just write the loop, but you can use those functions if you don't want to.