0

I have $view_data['auth_info'] which is populated with a query row, that works fine. But i need to add an additional row to this array. How is this possible? All the variables are unique, so theres no change i will have to identical definitions.

I tried following:

$view_data['auth_info'] = $myFirstSQLRow
array_push($view_data['auth_info'],$mySecondSQLRow);

But it failed:

Message: array_push() [function.array-push]: First argument should be an array

But isnt the first var an array or have i misunderstood something?

4
  • $view_data['auth_info'] = array($myFirstSQLRow, $mySecondSQLRow)? Commented Sep 25, 2012 at 19:58
  • I want to merge the two variables into one spot. If you understand. Commented Sep 25, 2012 at 20:00
  • 1
    Very hard to understand what you mean by 'merge'. Can you give an example? Commented Sep 25, 2012 at 20:03
  • @Aust, tried to give an example on this merging, which is exactly what i need. Commented Sep 25, 2012 at 20:52

1 Answer 1

2

You are looking for array_merge. Use it like this:

$view_data['auth_info'] = $myFirstSQLRow;
$view_data['auth_info'] = array_merge($view_data['auth_info'], $mySecondSQLRow);

As it says in the documentation, you may need to type-cast (by putting (array) in front of your variables.)

7
  • I tried this, also by typecasting both vars to (array), but when i try to print the variables $view_data['auth_info']->name i get Message: Undefined offset: 1 and Trying to get property of non-object Commented Sep 25, 2012 at 20:50
  • 1
    @JavaCake - Try this instead: $view_data['auth_info']['name'] Commented Sep 25, 2012 at 20:53
  • Works like a charm. But isnt there a workaround so i still can use the objects rather than typecasting to array? Commented Sep 25, 2012 at 20:56
  • @JavaCake - I dunno. Sorry, I've never had to do that. But out of curiosity, why do you need to do that? Commented Sep 25, 2012 at 21:01
  • Im querying data from two tables and returning them as rows. I wanted to merge the two rows somehow. Commented Sep 25, 2012 at 21:02

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.