2

I am trying to create an associate array like this

while($row = $result1->fetch_assoc()) {

        $user = $row['first_name'] ."_" . $row['last_name'];
        $userholder[$user] = $row['choice'];
        $event = $row['event_name'] . "_" . $row['event_location'] . "_" . $row['even_date'];
        $consolidateEvents[$event] = $userholder;
    }

but my $consolidateEvents array is numeric. I cannot see what I am doing wrong. Why am I not getting $event as the key for my array?

7
  • what is result of echo $event ? this can not be complete code, something is fishy other than your code. Commented Feb 12, 2017 at 12:41
  • The result of echo $event is Practice_Cross Flats Over 50_2017-02-07 19:22:00 Commented Feb 12, 2017 at 12:43
  • and what is print_r($consolidateEvents) Commented Feb 12, 2017 at 12:43
  • No it is not the complete code but I was trying show just the relevant code I can post everything if it helps? Commented Feb 12, 2017 at 12:44
  • Because, from this snippet of your code, no problem, I see, its working properly, that why I am asking, for other code Commented Feb 12, 2017 at 12:45

1 Answer 1

1

Try this code to correct your output,

function custom_function($input_array){
    $output_array = array();
    foreach ($input_array as $key => $value) {
        foreach ($v as $k => $v) {
            $output_array[$key][$k] = $v;
        }
    }
    return $output_array;
}

Give it a try, this will work

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

1 Comment

Works brilliantly. Thanks a bunch

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.