0

I have an array like this :

  $total= [20140124] => Array
    (
        [abc] => 0.19287878787879
        [total] => 38
        [revenue] => 1232
        [clicks] => 1110
        [score]=>100
    )

[20140123] => Array
    (
        [abc] => 0.32898148148148
        [total] => 28
        [revenue] => 1142
        [clicks] => 1022
        [score]=>200
    )

Now I am preparing another array and checking some conditions like the following:

foreach($total as $t){
    $new_array[] = array(
       "total"=>$t->abc;
       "another_value"=>$t->revenue/$t->clicks;
    );
    if(some_condition){
        $new_array[] = array("total_score"=>$t->clicks+$t->score); 
    }
}

What I need is an array like

$new_array = 

[0] => Array
        (
            [total] => total_value
            [another_value] => anopther_value
            [total_score] => total_score_value

        )

    [1] => Array
        (
            [total] => total_value
            [another_value] => anopther_value
            [total_score] => total_score_value
        )
)

But I am not getting the total_score inserted to the 0th index. Instead the whole array is replaced with the values in the if condition. How can I get the total_score also with the other indexes ?

2
  • 1
    Why you use object notation ($t->abc) on arrays? Commented Jan 27, 2014 at 8:51
  • ha...that's a mistake anyway Commented Jan 27, 2014 at 8:53

3 Answers 3

1

You can try with:

foreach($total as $t){
    $data = array(
       'total'         => $t['abc'],
       'another_value' => $t['revenue'] / $t['clicks']
    );

    if(some_condition){
        $data['total_score'] = $t['clicks'] + $t['score']; 
    }
    $new_array[] = $data;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Actually both the answers are same. So I am accepting the first one. :) Thanks hsz
Not at all. @k102 uses -> which are incorrect here. You are looping arrays, not objects, so you have to call arrays elements with $array['key']` !
@hsz ok... shame on me
@k102 It was my mistake actually that I used '->' for array. Somehow it came in my mind and so used it. Anyway when I used k102 solution, I corrected it and used array.
1

Its not getting inserted to 0th index because you are using $new_array[] which creates a new index each time it is called. You can use fixed index by incrementing a counter in your loop or by calling this once each iteration. Your counter solution will look as follows:

$count = 0;    
foreach($total as $t){
    $new_array[$count] = array(
       "total"=>$t->abc;
       "another_value"=>$t->revenue/$t->clicks;
    );
    if(some_condition){
        $new_array[$count] = array("total_score"=>$t->clicks+$t->score); 
    }
    $count++;
}

Comments

0

try this one:

foreach($total as $t){
    $array_element = array();
    $array_element["total"] = $t["abc"];
    $array_element["another_value"] = $t["revenue"] / $t["clicks"];
    );
    if(some_condition){
        $array_element["total_score"] = $t["clicks"] + $t["score"]; 
    }
    $new_array[] = $array_element;
}

In your code you are adding an element to $new_array and then checking for that condition. So you are adding the next element, but not editing the first

Comments

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.