Avatar of stargateatlantis
stargateatlantis
 asked on

PHP Array

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

PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
John Kawakami

The syntax for the first line is wrong.  It should be:

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

ASKER
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
John Kawakami

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Insoftservice

please provide sample of expected array.

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

}
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question