Reputation: 6849
I have a PHP array like below:
$list = array(
array(
'name'=>'switch',
'value'=>'cisco'
),
array(
'name'=>'switchport',
'value'=>'2'
)
);
How can I get name='switch'
or name='switchport'
's item in one step?
because this is in smarty
template, there is the img
<img src="http://localhost:8080/api.php?switch={$my_need_switch_value}&switchport={$my_need_switchport_value}">
So, how can I get the my_need_switch_value
from $list
in one step?
Upvotes: 0
Views: 69
Reputation: 26896
You can use {assign}
to assign new variables.
{foreach from=$list item=customfield}
{if $customfield.name=='switch'}
{assign var=switch value=$customfield.value}
{elseif $customfield.name=='switchport'}
{assign var=portname value=$customfield.value}
{/if}
{/foreach}
then you can use the $switch
and $switchport
directily.
Upvotes: 1
Reputation: 909
If I understood correctly what you want, you can create a function with the array and the name you want to find as parameters, then loop through it and get its value. Like so:
$list = [
[
'name'=>'switch',
'value'=>'cisco'
],
[
'name'=>'switchport',
'value'=>'2'
]
];
function getValue($array, $name) {
foreach($array as $key => $value) {
if($value['name'] == $name) {
echo $value['value'];
}
}
}
If you call it using 'switch' as name parameter it will echo 'cisco', and if you change it to 'switchport', it will echo '2'. Then you can use it on your structure like so:
<img src="http://localhost:8080/api.php?switch={getValue($list,'switch')}&switchport={getValue($list,'switchport')}">
Is that what you want to achieve?
Upvotes: 0
Reputation: 4150
Have you tried something like:
$list[0].name
$list[1].name
I do not believe there is a one shoot. You should at least iterate the array and compare the value with what you need.
Upvotes: 0
Reputation: 71
Did you try {$list.0}
or {$list.0.name}
? If I understood what you want.
Upvotes: 0