0

When I run the below PHP in WordPress without the foreach I successfully get printed a multidimensional array. When I use the foreach it returns an error 500 I am just trying to loop through the results so I am able to select each name and then push it to another array.

If someone could assist me with looping through this array, that would be great!

Array
(
    [0] => stdClass Object
        (
            [term_taxonomy_id] => 26
            [taxonomy] => product_brand
            [name] => Authentic Cheese 
            [slug] => authentic-cheese
        )

    [1] => stdClass Object
        (
            [term_taxonomy_id] => 27
            [taxonomy] => product_brand
            [name] => Robot
            [slug] => robot
        )

)

PHP

$q2 = "SELECT t.term_taxonomy_id, t.taxonomy, e.name, e.slug
FROM wp_term_taxonomy t
INNER JOIN wp_terms e ON t.term_taxonomy_id = e.term_id
WHERE taxonomy = 'product_brand'";

$r2 = $wpdb->get_results($q2);

print_r($r2);

foreach ($r2 as $row) {
    echo $row['name'];
}
3
  • That's not a 2D array, it's an array of objects Commented Mar 6, 2014 at 9:14
  • It is Object Not an Array... As @EliasVanOotegem says... Commented Mar 6, 2014 at 9:15
  • Note that you don't have to use the result-set as objects, and you can get the result as an array of associative arrays, too... added details + link to my answer Commented Mar 6, 2014 at 9:33

4 Answers 4

2

You have an array of objects in the result. So use:

foreach ($r2 as $row) {
    echo $row->name;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have a 1 dimensional array of objects, instances of stdClass, to be exact:

Array
(
    [0] => stdClass Object //<== stdClass OBJECT!!

That means that you can either use the objects, and access the names like you would with any object:

foreach ($array as $row)
{
    echo $row->name, PHP_EOL;
}

Or, if you really want to, you can simply cast the objects to arrays:

foreach ($array as $k => $o)
{
    $array[$k] = (array) $o;//cast and re-assign
}

Next time you loop over the $array, the objects will be gone, and you'll get associative arrays instead... really, though, this cast business is just overhead, I'd really just use the objects if I were you.
Of course, if you can change the fetch mode, you could change it so that all results are fetched as associative arrays.

According to the wordpress documentation, you can pass a second argument to the get_results method, that specifies the fetch method:

$r2 = $wpd->get_results($q2, ARRAY_A);

Would be the best way to ensure that $r2 is actually an array of associative arrays

To quote the documentation on which I base this:

Generic, mulitple row results can be pulled from the database with get_results. The function returns the entire query result as an array, or NULL on no result. Each element of this array corresponds to one row of the query result and, like get_row, can be an object, an associative array, or a numbered array.

<?php $wpdb->get_results( 'query', output_type ); ?> 

query
(string) The query you wish to run. Setting this parameter to null will return the data from the cached results of the previous query.

output_type
One of four pre-defined constants. Defaults to OBJECT. See SELECT a Row and its examples for more information.
OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.

Comments

1

Note from your print_r that it's an array of objects not an array of arrays. So you need to do

foreach ($r2 as $object) {
    echo $object->name;
}

Comments

1

You can use wp_list_pluck in WordPress:

<?php names = wp_list_pluck( $your_array, 'name' ); ?>

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.