0

So currently I am having some issues with trying to access an element in this multidimensional array. So what I am trying to do is to create 3 different arrays of different classes, and then return them in array at the end.

The problem comes when I am trying to accsess one of the array's in the $result array. I have checked and there are elements in the array's but I am unable to access them.

public function search($searchString) : array
{
        $bloggArr[] = array();
        $bloggerArr[] = array();
        $innleggArr[] = array();

        $stmt = $this->db->prepare("SELECT * FROM blogg WHERE 
        bnavn=:bnavn");
        $stmt->bindParam('bnavn', $searchString, PDO::PARAM_STR);
        $stmt->execute();

        while ($blogg=$stmt->fetchObject("Blogg"))
        {
            $bloggArr[]=array('blogg'=>$blogg);
            echo $bloggArr['blogg']->hentBnavn();
            // sort($bloggArr);
        }

        $stmt = $this->db->prepare("SELECT * FROM blogger WHERE 
        fornavn=:fornavn OR etternavn=:etternavn");
        $stmt->bindParam('fornavn', $searchString, PDO::PARAM_STR);
        $stmt->bindParam('etternavn', $searchString, PDO::PARAM_STR);
        $stmt->execute();

        while ($blogger = $stmt->fetchObject("Blogger"))
        {
            $bloggerArr[]= array('blogger' => $blogger);
            // sort($bloggArr);
        }

        $stmt = $this->db->prepare("SELECT * FROM innlegg WHERE tittel=:tittel");
        $stmt->bindParam('tittel', $searchString, PDO::PARAM_STR);
        $stmt->execute();

        while ($innlegg = $stmt->fetchObject("Innlegg"))
        {
            $innleggArr[] = array('innlegg' => $innlegg);
            // sort($innleggArr);
        }
        $result = array('bloggArr' => $bloggArr, 'bloggerArr' => 
        $bloggerArr, 'innleggArr' => $innleggArr);
        return $result;
}

I thought I would be able to access the element in the second array by:

echo $resultat['bloggArr']['blogg']->SomeFunction();

the problem is that I get the error message that ['blogg'] is Undefined index. I am been unable to find a way to access the second array elements for a while now, and are wondering if anyone could point me in the right direction. Thanks for all help.

1
  • Try to var_dump($resultat); and see what you get. Commented Apr 24, 2018 at 1:22

1 Answer 1

1

You're not using your arrays properly. Just considering $bloggArr:

$bloggArr[] = array();

creates this:

Array
(
    [0] => Array
        (
        )

)

that line should be changed to:

$bloggArr = array();

To create an empty array. Then, each time through the loop,

$bloggArr[]=array('blogg'=>$blogg);

adds an element like this:

[1] => Array
    (
        [blogg] => <your object>
    )

So, to access those values in the $result array you would need to use a loop:

foreach ($result['bloggArr'] as $blogg) {
    echo $blogg['blogg']->SomeFunction();
}

If your queries will only return one result, then you could simply change this line:

$bloggArr[]=array('blogg'=>$blogg);

to

$bloggArr=array('blogg'=>$blogg);

and then you could access the function via

echo $result['bloggArr']['blogg']->SomeFunction();
Sign up to request clarification or add additional context in comments.

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.