2

I want to explode an array, read each value and print them back in an array...

I dont understand where i am getting wrong. Please help me..this is my code..

I am getting an array to string conversion error

$query="SELECT categories FROM shops";
$result = mysql_query($query);  
while($column = mysql_fetch_assoc($result)){
    $categories=explode(",",$column['categories']);
    foreach($categories as $value){
        $new_query="SELECT name from categories where id='$value'";
        $name = mysql_query($new_query);    
        $name_column= mysql_fetch_assoc($name);
        array_push($shops_list,$name_column);

    }
}
echo implode(",",$shops_list);
2
  • quick hint on performance, don't do queries inside loop if not neccessary. Commented Oct 18, 2012 at 13:49
  • 2
    Learn how to use SQL Joins, and you won't need to do this inefficient query looping: learn how to use MySQLi or PDO as well... then you won't need to use deprecated libraries Commented Oct 18, 2012 at 13:50

5 Answers 5

2

$shop_list is not defined, before using it in this line array_push($shops_list,$name_column);. And, this line

    array_push($shops_list,$name_column);

needs to be, as you need to mention the key name,

    array_push($shops_list,$name_column['name']); //or better
    $shop_list[] = $name_column['name'];
Sign up to request clarification or add additional context in comments.

2 Comments

why the loop is not breaking after each id?
foreach($categories as $value){ will loop through all the values in array categories. isn't that what you want?
1

Several issues:

$name_column = mysql_fetch_assoc($name);
$name_column = $name_column['name'];

name_column is an array.

shops_list is never initialized.

You should use [] instead of array_push.

Comments

1

The other guys hit it on the nose, but when you did your array push on $name_column, since $name_column is an array, you end up with:

Array
(
    [0] => Array
        (
            [name] => boo
        )
)

Obviously doing an implode on that is going to not work.

That being said, what you really need to do here is not keep your category mappings as a comma delimited string in the database. Standard DB architecture dictates you use a mapping table.

  • Table shops
  • Table categories
  • Table shop_category_map that has shop_id and category_id

Comments

1

use group_concat to retrieve values. and after getting the result, use them directly for searching. like

$result_array = explode(",",$row['category']);

    foreach($result_array as $ra)
    {
    //sql command. fetch here.
    $new_query="SELECT name from categories where id='$value'";
    $name = mysql_query($new_query);    
    $name_column= mysql_fetch_assoc($name);
    $shops_list[] = $name_column;
    }

try else go for better solution

Comments

1

// explode an array and then implode until a particular index of an array

$a = '192.168.3.250';
$b = explode('.',$a);
$ar = array();

for($i=0;$i<=2;$i++)
{
   array_push($ar,$b[$i]);
}

$C = implode($ar,'.');
print_r($C);

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.