3

I have a PHP Code

$brandNames = array();
foreach ($maufacturers as $manu) {
    array_push($brandNames, array($manu->getTitle() => $manu->getUrl()));
}
print_r($brandNames);

I am getting output as

Array
(
    [0] => Array
        (
            [key0] => val0
        )

    [1] => Array
        (
            [key1] => val1
        )

    [2] => Array
        (
            [key2] => val2
        )
)

I need output as

Array
(
     [key0] => val0
     [key1] => val1
     [key2] => val2
     [key3] => val3
)

Not the nested array Just all keys and values inside one wrapper array.

1
  • foreach ($maufacturers as $key => $manu) Commented Jun 2, 2015 at 10:21

2 Answers 2

7
$brandNames = array();
foreach ($maufacturers as $manu) {
    $brandNames[$manu->getTitle()] = $manu->getUrl();
}
print_r($brandNames);
Sign up to request clarification or add additional context in comments.

Comments

1

Change your code to:

$brandNames = array();
foreach ($maufacturers as $key => $manu) {
    $brandNames[$manu->getTitle()] = $manu->getUrl();
}
print_r($brandNames);

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.