0

I have 2 arrays :

$a and $b

i want to merge this arrays to $res

here is my code:

$a = array("
    [0] => Array
        (
            [0] => 45677
            [ID] => 45677
            [1] => 10
            [post_author] => 10
            [2] => 2014-02-26 19:05:59
            [post_date] => 2014-02-26 19:05:59
            [3] => 2014-02-26 14:35:59
            [post_date_gmt] => 2014-02-26 14:35:59
        )");
$b= array("
    [0] => Array
        (
            [first_image] => http://gogole.com/a.jpg
        )");


$res = array_merge($a,$b);

OUTPUT :

Array
(
    [0] => 
    [0] => Array
        (
            [0] => 45677
            [ID] => 45677
            [1] => 10
            [post_author] => 10
            [2] => 2014-02-26 19:05:59
            [post_date] => 2014-02-26 19:05:59
            [3] => 2014-02-26 14:35:59
            [post_date_gmt] => 2014-02-26 14:35:59
        )
    [1] => 
    [0] => Array
        (
            [first_image] => http://gogole.com/a.jpg
        )
)

it should be something like this :

Array
(
    [0] => Array
        (
            [0] => 45677
            [ID] => 45677
            [1] => 10
            [post_author] => 10
            [2] => 2014-02-26 19:05:59
            [post_date] => 2014-02-26 19:05:59
            [3] => 2014-02-26 14:35:59
            [post_date_gmt] => 2014-02-26 14:35:59
            [first_image] => http://gogole.com/a.jpg
        )
    )

)

what is problem ?

and how can i fix it ?

5
  • 3
    The problem is that your "arrays" are not multi-dimensional arrays. They're an array with one long string in it...! Commented Mar 19, 2014 at 19:10
  • sounds like you don't want array_merge() but you want array_push() Commented Mar 19, 2014 at 19:11
  • why would $res = array_merge($a[0],$b[0]); not be a correct option, just wondering...? Commented Mar 19, 2014 at 19:13
  • @webeno because he's not defining the multi-dimensional arrays properly, so there is only one element. You would essentially be making an array with two strings, which is not what he wants. Commented Mar 19, 2014 at 19:17
  • @echolocation I wasn't really considering his way of coding, based my answer merely on the assumption that he actually has multidimensional arrays... thanks for answering my question though! Commented Mar 19, 2014 at 19:20

3 Answers 3

4

You're not defining the arrays correctly. I have a feeling you used an outputted var_dump of someone's to define your array, but that is not for the purposes of using in code. It is for a easily understandable array. You could change this:

$b= array("
    [0] => Array
        (
            [first_image] => http://gogole.com/a.jpg
        )");

To this:

$b= array(array('first_image' => 'http://gogole.com/a.jpg'));

Also, you would need to do the same with the $a array.


Also, for it to function in the way that you want, you would need to use array_merge($a[0],$b[0]);.

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

4 Comments

it's not working , i got this error : Notice: Use of undefined constant first_image - assumed 'first_image'
You are using the code before my edit. Try the edited version.
it's still giving my output and not creating new array
I would need to see how you define $a.
2

I am not sure how you are getting that output, it looks like the result from a mysql_fetch_array() as opposed to a mysql_fetch_assoc() which it seems like you are looking for. I just did the following without issue:

$a = array(
  'id' => '123123',
  'author' => 'Random Author',
  'date' => '2013-01-05'
);

$b = array(
  'first_image' => 'http://gogole.com/a.jpg'
);

$res = array_merge($a, $b);

echo '<pre>';
print_r($res);
echo '</pre>';

Result:

Array
(
    [id] => 123123
    [author] => Random Author
    [date] => 2013-01-05
    [first_image] => http://gogole.com/a.jpg
)

Comments

0

I fixed your syntax, you storage value as string Here you have Assoc arrays. Copy this code

$a = array("ID"=> 45677,
            "post_author" => 10,
            "post_date" => "2014-02-26 19:05:59",
            "post_date_gmt" => "2014-02-26 14:35:59"
        );
        $b= array("first_image" => "http://gogole.com/a.jpg");


$res = array_merge($a,$b);
var_dump($res);

Good Luck!

1 Comment

i write just simple example to explain a problem , i have many array so i must post IDs as arrays index !

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.