简体   繁体   English

json数组元素到数组

[英]json array elements to array

 $cuisines = RestaurantProfile::select('cuisines')->get();
    $cuisines_array = array();
    foreach ($cuisines as $cuisine) {
        $string = implode(",",json_decode($cuisine, true));
        $array = explode(",", $string);
        foreach ($array as $single) {
             if (!in_array($single, $cuisines_array)) {
                 $cuisines_array[] = $single;
             }
         } 
    }
    dd($cuisines_array);

I want $cuisines_array to have something like 我想要$ cuisines_array有类似

array:33 [▼
0 => "Afghani"
  1 => "Mughlai"
  2 => "Chinese"
  3 => "Indian"
  4 => "continental"
  5 => "south indian"
  6 => "mughlai"

But I am getting as in the screenshot: output screenshot 但是我在屏幕截图中得到了: 输出屏幕截图

My Cuisines attribute in table is database table . 我在表中的Cuisine属性是数据库表 Any leads? 有线索吗?

You can use array_slice() : 您可以使用array_slice()

...
foreach (array_slice($array, 0, 6) as $single) {
...

array_slice()  

returns the sequence of elements from the array array as specified by the offset and length parameters 返回由offset和length参数指定的数组数组中的元素序列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM