Welcome Guest, Not a member yet? Register   Sign In
Replace values inside array question
#1

[eluser]BobbyB[/eluser]
Hi Codeigniters,
I have got following problem I dont know how to solve.
I get the user data in my controller like this:
Code:
$data['userdata'] = $this->get_userdata_filecenter->get_data($user);

The array looks like this:
Code:
Array ( [userdata] => Array (
[0] => Array (
[uid] => 177
[username] => test
[first_name] => n/a
[last_name] => n/a
[company_name] => n/a
[address] => n/a
[city] => n/a
[zip] => n/a
[country] => n/a
[phone] => n/a
[language] => de
[root] => test
) ) )
What I want to do is replace all "n/a" with an empty value "" and pass it to my view.
How can I replace those values?
Thanks in advance for your help.
#2

[eluser]Rob Gordijn[/eluser]
Code:
foreach($data['userdata'] as $k => $v} {}
in the loop, check the $v for 'n/a' and set $data['userdata'][$k] to ''

but, you can also take a closer look into your model en cut out the 'insert-useles-n/a-function-thingy' Smile
#3

[eluser]BobbyB[/eluser]
Hi,
thanks for your reply.
Please be patient, as I am a noob Smile
I just tried:
Code:
foreach($data['userdata'] as $k => $v)
{
print_r($v);
}
and it gives me the array as a whole(just like the one we started with).
So how can I replace the values inside the array and pass it on to my view?
Thank you.
#4

[eluser]Rob Gordijn[/eluser]
ok, haha, I've seen this before 2 days ago.
the $data['userdata'] holds an array called 'userdata' (as shown in your first post)
so you need to:

Code:
foreach($data['userdata']['user_data'] as $k => $v)
{
echo $v.'<br>';
}
#5

[eluser]BobbyB[/eluser]
Thnx, I just found that out myself.
Now I got:
Code:
foreach($data['userdata']['userdata']['0'] as $k => $v)
{
echo $v;
}
This prints out all the values.
Then I tried:
Code:
foreach($data['userdata']['userdata']['0'] as $k => $v)
{
$v = str_replace("n/a","",$v);
}

Which replaces all the "n/a" with "".
Now how can I apply those changes to the array?
Thank you.
#6

[eluser]Rob Gordijn[/eluser]
ok, when setting/replacing values in an array, you need to address the specific key to set it Smile

fast-reply-code:
Code:
foreach($data['userdata']['userdata'] as $data)
{
    foreach($data as $k => $v)
    {
        $data['userdata']['userdata'][$k] = str_replace("n/a","",$v);
    }
}
#7

[eluser]BobbyB[/eluser]
Ah,
thanks for your patience and time.
My final code:
Code:
foreach($data['userdata']['userdata']['0'] as $k => $v)
{
$data['userdata']['userdata']['0'][$k] = str_replace("n/a","",$v);
}
Works Smile
#8

[eluser]Rob Gordijn[/eluser]
ah ok, NP bobby

but If I'm allow to do a suggestion; please look into the get_userdata_filecenter->get_data() function.
alter it, let it return just an array (instead of a multi-dim. array) and fix the "n/a" (probably when the value from i.e. the DB is empty)
#9

[eluser]BobbyB[/eluser]
Thnx for the tips.
A lot to do - I know Smile




Theme © iAndrew 2016 - Forum software by © MyBB