PHP Array

stargateatlantis
stargateatlantis used Ask the Experts™
on
I am trying to loop thru the following array and put it into a new array called results here is the array   But it doesn't seem to be working

$myArray=Array ( [0] => Array ( [title] => the title [descr] => my description [photo] => mypic.jpg ) ) ;

foreach ($myArray as $key => $value)
        {
            $fn($key, $value);
            $newArray[$key] = $value;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The syntax for the first line is wrong.  It should be:

$myArray = array( 0 => array( 'title'=>'the title', 'descr'=>'my description', 'photo'=>'mypic.jpg'));

Author

Commented:
I am using this PDO wrapper and the results come in as a array take a look at the link below

http://www.imavex.com/php-pdo-wrapper-class/#select
OK, my first post was about the syntax - but I guess you pasted the output of a print_r or some error message as code.  Assuming that the output is from t hat wrapper, I guess the other issue is that you need two loops here.

foreach($myArray as $element) {
    foreach($element as $key=>$value) {
        ...
    }
}

Open in new window

please provide sample of expected array.

foreach ($myArray as $key => $value)
{
    foreach ($value as $subkey => $subvalue)
    {
        echo $subkey.' Its value is'.$subvalue;
    }

}
Most Valuable Expert 2011
Top Expert 2016
Commented:
In the original code snippet, you might want to use something like this:

foreach ($myArray[0] as $key => $value)

You can use var_dump() to print out the contents of variables.  If you develop the habit of doing that frequently it will make your programming life much easier!
http://php.net/manual/en/function.var-dump.php

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial