1

I am running a query that outputs a multidimensional array like this: -

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 772
                [display_name] => Test Company Ltd
                [profile_page] => partners/test-company
            )

    )

[1] => Array
    (
    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 772
                [display_name] => Test Company Ltd
                [profile_page] => partners/test-company

            )

)

I am trying to make the Array look like this so that we can loop through it on our site: -

Array
(
[0] => Array
    (
        [id] => 772
        [display_name] => Test Company Ltd
        [profile_page] => partners/test-company
    )
)

This is all being generated using PHP using Symfony2 and Doctrine2. I have tried a couple of methods to get this working but I have reverted back to my first method which is this: -

$companies = array();

foreach($postcodes as $key=>$value) {
    $conn = $em->getConnection();

    $query = $conn->prepare("SELECT a.id, a.display_name, a.profile_page, b.meta_data FROM companies a, `cms`.pages b WHERE b.slug = a.profile_page AND a.active = 1 AND a.postcode = ".$value."  ORDER BY a.display_name ASC");
    $execute = $query->execute();
    $companies[] = array_replace_recursive($query->fetchAll());
}

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

die();

If someone can show me how to get around this, that would be great.

0

3 Answers 3

2

$query->fetchAll() is itself is an multidimensional array you're in need.

Just replace following line

$companies[] = array_replace_recursive($query->fetchAll());

with

$innCompnies=$query->fetchAll();
foreach($innCompnies as $inComp) {
     $companies[]=$inComp;
}

It wasn't working because you were pushing multidimensional array into another array. What we actually need to do is to push element one by one into the main array.

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

4 Comments

I tried this and when I remove the square brackets from $companies as well as the array_replace_recursive it showed an empty array. When I put the square brackets back in and removed the array_replace_recursive, I have exactly the same answer as before.
Hi Alok, thats great and that worked a treat. Thank you. However, how would I merge it as the two values are similar.
You will need to keep track of the IDs you visit in foreach only insert array if the id is not visited before.
Of course. Monday mornings eh? Thank you Alok. I will mark this as answered. Thanks again mate, I really appreciate your help. Sometimes too much coding can cloud your judgement, haha!
0

I suggest you to avoid SQL-queries in loop. Also you have potential SQL-injection vulnerability concatenating postcodes to your query. So the possible solution may be replacing your foreach loop with something like:

$companies = $conn->fetchAll(
    "SELECT a.id, a.display_name, a.profile_page, b.meta_data FROM companies a, `cms`.pages b WHERE b.slug = a.profile_page AND a.active = 1 AND a.postcode IN :postcodes  ORDER BY a.display_name ASC",
    compact('postcodes')
);

Comments

0

you can transform the multidimentional array like this.

$new_companies = array();
array_walk($companies, function($companie) use($companies, &$new_companies){
  if(is_array($companie)) {
    foreach ($companie as $c) {
        $new_companies[] = $c;
    }
  }
});

$new_companies is what you want.

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.