0

What I'm trying to do:
Get a list of names (if more than one) on a single string separated by a comma this way:

  • Britney Spears, Lady Gaga, Miley Cyrus

What's going on:

  • We are inside a loop.
  • I'm trying to associate these celebrities to a post ID

So we have this unique $id for each loop.
More than one celebrity name can be attached to an ID.

Query:

$array = $wpdb->get_results("SELECT name
FROM celebrities
WHERE id = " . $id . "");

PHP CODE:

if (is_array($array) && $array != null) {
    foreach($array as $single) {
        $array = $single->name . ',';
    }
}
else {
    $array = $array->name;
}

Problem:

With my code, I'm being limited to one celebrity per loop (even though the database shows that another celebrity should be present, as well).

I really just want to check if more than one celebrity name is present, then put them together inside a single string separated by a comma.

Desired Output:

Here's a simple outline of what I'm trying to accomplish:

  • array(0) (no celebrity) = // SHOW NOTHING
  • array(1) (one celebrity name found) = echo $name
  • array(2) (more than one celebrity) = foreach($names as $name)

Var_dump:

I'd really just rather show you the complexity of the array structure:

array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1620 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1683 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
[1] = >
    object(stdClass) #1611 (1) {
    ["name"]= >
    string(10)
"Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1614 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1690 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1598 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(4) {
    [0] = >
        object(stdClass) #1722 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
[1] = >
    object(stdClass) #1706 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
[2] = >
    object(stdClass) #1668 (1) {
    ["name"]= >
    string(10)
"Lady Gaga"
}
[3] = >
    object(stdClass) #1663 (1) {
    ["name"]= >
    string(10)
"Lady Gaga"
}
}
array(4) {
    [0] = >
        object(stdClass) #1615 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
[1] = >
    object(stdClass) #1708 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
[2] = >
    object(stdClass) #1723 (1) {
    ["name"]= >
    string(10)
"Lady Gaga"
}
[3] = >
    object(stdClass) #1726 (1) {
    ["name"]= >
    string(10)
"Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1598 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1663 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1722 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1734 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1598 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1750 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1751 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1716 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1776 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1795 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1647 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1784 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1776 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1611 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1676 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1781 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(0) {}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1715 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1813 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1826 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1828 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1717 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1765 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1769 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1848 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1690 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1660 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1821 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1717 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1766 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1836 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1765 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1826 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1843 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(2) {
    [0] = >
        object(stdClass) #1854 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
[1] = >
    object(stdClass) #1820 (1) {
    ["name"]= >
    string(5)
"Beyonce"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1814 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1847 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1866 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1820 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1814 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1847 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(2) {
    [0] = >
        object(stdClass) #1875 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1821 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1786 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1880 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1746 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1815 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1817 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1811 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1874 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1819 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1798 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1820 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1802 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1853 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1741 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1867 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1820 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1688 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1919 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1889 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1923 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1927 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1811 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1904 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1910 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1889 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1927 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1904 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(1) {
    [0] = >
        object(stdClass) #1843 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1860 (1) {
    ["name"]= >
        string(6)
    "Miley Cyrus"
}
}
array(2) {
    [0] = >
        object(stdClass) #1889 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
[1] = >
    object(stdClass) #1684 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1843 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1811 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
[1] = >
    object(stdClass) #1900 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1904 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1926 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1915 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1829 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
[1] = >
    object(stdClass) #1932 (1) {
    ["name"]= >
    string(10)
"Lady Gaga"
}
}
array(0) {}
array(2) {
    [0] = >
        object(stdClass) #1889 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
[1] = >
    object(stdClass) #1952 (1) {
    ["name"]= >
    string(5)
"Beyonce"
}
}
array(0) {}
array(0) {}
array(2) {
    [0] = >
        object(stdClass) #1915 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1829 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1932 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1892 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(3) {
    [0] = >
        object(stdClass) #1899 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
[1] = >
    object(stdClass) #1929 (1) {
    ["name"]= >
    string(5)
"Beyonce"
}
[2] = >
    object(stdClass) #1805 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1886 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1829 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1802 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(2) {
    [0] = >
        object(stdClass) #1932 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1941 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(0) {}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1836 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
}
array(1) {
    [0] = >
        object(stdClass) #1947 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1917 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1954 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1902 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
}
array(1) {
    [0] = >
        object(stdClass) #1836 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(2) {
    [0] = >
        object(stdClass) #1970 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1842 (1) {
    ["name"]= >
    string(5)
"Beyonce"
}
}
array(0) {}
array(1) {
    [0] = >
        object(stdClass) #1878 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
}
array(2) {
    [0] = >
        object(stdClass) #1802 (1) {
    ["name"]= >
        string(5)
    "Beyonce"
}
[1] = >
    object(stdClass) #1969 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1961 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(2) {
    [0] = >
        object(stdClass) #1836 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
[1] = >
    object(stdClass) #1970 (1) {
    ["name"]= >
    string(5)
"Beyonce"
}
}
array(1) {
    [0] = >
        object(stdClass) #1902 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
}
array(1) {
    [0] = >
        object(stdClass) #1979 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1860 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(1) {
    [0] = >
        object(stdClass) #1946 (1) {
    ["name"]= >
        string(6)
    "Madonna"
}
}
array(3) {
    [0] = >
        object(stdClass) #1926 (1) {
    ["name"]= >
        string(10)
    "Lady Gaga"
}
[1] = >
    object(stdClass) #1848 (1) {
    ["name"]= >
    string(5)
"Beyonce"
}
[2] = >
    object(stdClass) #1912 (1) {
    ["name"]= >
    string(6)
"Madonna"
}
}
array(0) {}

I believe I am getting confused for no reason, but I am stuck.

3
  • Watch out for SQL Injection. Is the query going to return an array if there are multiple results? Commented Jun 11, 2013 at 12:05
  • 1
    i didnt understand exactly but php (implode) or mysql (group concat) will help you. Commented Jun 11, 2013 at 12:08
  • @MattWhipple : Yes, it would return arrays (some null). It's not the query that is the problem though, I am trying to get the results in a string by going through a foreach, but I am only able to return one result each time, when there should be more. Thanks. Commented Jun 11, 2013 at 12:11

2 Answers 2

0

I think the problem is here:

foreach($array as $single) {
    $array = $single->name . ',';
}

$array is only an array the first time the loop is executed as inside the loop your assigning it a new value.

Try this instead:

$names = array();
foreach ($array as $single) {
    $names[] = $single->name;
}
echo implode(', ', $names);
1
  • Thank you @Dale, smart guy you are. I'm surprised you understood my jibberish at all. This worked. Commented Jun 11, 2013 at 12:17
0

i agree with Dale, because in your example, you will always get one name(the last one from, for example, database), so you should put it in a different array when looping

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.