0

How can i add element to my array under a specific key?

This is my array output before i use foreach. As you can see, the error field is empty. I want to fill it out.

Array (
  [0] => Array (
    [transactionid] => 2223
    [created] => 26-02-13 14:07:00
    [cardid] => 10102609
    [pricebefordiscount] => 68900
    [error] =>
  )

This is my foreach. As you can see i already tried to make this work by implementing $arrayname['index'] = $value;. But this does not work, nothing comes out when i spit out in a print_r. Why is this happening?

foreach ($samlet as $key)
{
    if ($key['pricebefordiscount'] > '200000') 
    {
        $samlet['error'] = "O/2000";
    }
    if ($key['cardid'] === '88888888') 
    {
        $samlet['error'] = "Testscan";
    }
}

This is the desired output:

Array (
  [0] => Array (
    [transactionid] => 2223
    [created] => 26-02-13 14:07:00
    [cardid] => 10102609
    [pricebefordiscount] => 68900
    [error] => "Testscan"
  )
3
  • I think it would be done with array_merge Commented Feb 27, 2013 at 9:54
  • is your if statement true? Commented Feb 27, 2013 at 9:55
  • I don't think any of your conditions are being satisfied. pricebefordiscount is 68900 whereas you have checked it for > 200000. And cardid is 10102609 whereas you are checking it for 88888888. Plz correct me if i am wrong. Commented Feb 27, 2013 at 9:59

4 Answers 4

3

Change your foreach, so you have the indexes used in the "main" $samlet array:

foreach($samlet as $key => $array)
{
    if ($array['cardid'] === '88888888')
    {
        $samlet[$key]['error'] = '0/2000';
    }
}

And so on...

Sign up to request clarification or add additional context in comments.

5 Comments

With this solution the problem is that within this loop $array retains the old values. Not a serious issue, but something you have to remember.
@leafnode: Yes, $array retains the "original" values, but by accessing $samlet whenever we're assigning something, that's not really a problem, is it?
It's not, but with that approach in a single block we have to deal with two different data accessing methods, having almost-the-same-but-not-really-identical data, losing the advantages of foreach. With this approach, I'd rather refrain from using $array at all not to confuse anyone, or even go with foreach(array_keys($samlet) as $key) - but this is rather eccentric when having possibility to use reference.
@leafnode: I wouldn't use array_keys at all here, but why not combine both our suggestions (that way, everybody is happy): foreach ($samlet as $key => &$value)... :-)
And I also wouldn't use array_keys, that was just a mental exercise ;)
1

Try this :

foreach ($samlet as &$key){

    if ($key['pricebefordiscount'] > '200000'){
         $key['error'] = "O/2000";
    }
    if ($key['cardid'] === '88888888'){
        $key['error'] = "Testscan";
    }

}

Comments

1

According to PHP manual:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

So your code should looke like this:

<?php
foreach ($samlet as &$key)
{

    if ($key['pricebefordiscount'] > '200000') 
    {
         $key['error'] = "O/2000";
    }
    if ($key['cardid'] === '88888888') 
    {
        $key['error'] = "Testscan";
    }
}

2 Comments

I think you mean $key['error'], otherwise there's no point in passing the $key arrays by reference... also note that using references can lead to bugs more easily (as an ampersand is easy to overlook)
Yes, my mistake - I copied the OPs code assuming that apart from the reference the rest was OK ;) I fixed the code just a minute ago.
0

TRY THIS

foreach ($samlet as $key=>$value)
{

    if ($value['pricebefordiscount'] > '200000') 
    {
         $samlet[$key]['error'] = "O/2000";
    }
        if ($value['cardid'] === '88888888') 
    {
        $samlet[$key]['error'] = "Testscan";
    }
}

2 Comments

That won't work, you're using an array as key... it's not because the variable is called key, that that is what it'll hold
@EliasVanOotegem, but this will :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.