数组到字符串转换错误,需要爆炸然后内爆

时间:2021-04-03 16:27:48

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);

5 个解决方案

#1


2  

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

$shop_list未定义,在此行array_push中使用之前($shops_list,$name_column);这条线,

    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'];

#2


1  

Several issues:

几个问题:

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

name_column is an array.

name_column是一个数组。

shops_list is never initialized.

shops_list没有初始化。

You should use [] instead of array_push.

您应该使用[]而不是array_push。

#3


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:

其他的元素会碰到它,但是当你对$name_column进行数组推送时,因为$name_column是一个数组,你会得到:

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.

话虽如此,您在这里真正需要做的是,不要将类别映射保存为数据库中的逗号分隔字符串。标准的DB架构要求您使用映射表。

  • Table shops
  • 表店
  • Table categories
  • 表类别
  • Table shop_category_map that has shop_id and category_id
  • 具有shop_id和category_id的表shop_category_map

#4


1  

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

使用group_concat检索值。得到结果后,直接使用它们进行搜索。就像

$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

试试别的办法,找到更好的解决办法

#5


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);

#1


2  

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

$shop_list未定义,在此行array_push中使用之前($shops_list,$name_column);这条线,

    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'];

#2


1  

Several issues:

几个问题:

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

name_column is an array.

name_column是一个数组。

shops_list is never initialized.

shops_list没有初始化。

You should use [] instead of array_push.

您应该使用[]而不是array_push。

#3


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:

其他的元素会碰到它,但是当你对$name_column进行数组推送时,因为$name_column是一个数组,你会得到:

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.

话虽如此,您在这里真正需要做的是,不要将类别映射保存为数据库中的逗号分隔字符串。标准的DB架构要求您使用映射表。

  • Table shops
  • 表店
  • Table categories
  • 表类别
  • Table shop_category_map that has shop_id and category_id
  • 具有shop_id和category_id的表shop_category_map

#4


1  

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

使用group_concat检索值。得到结果后,直接使用它们进行搜索。就像

$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

试试别的办法,找到更好的解决办法

#5


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);