找到数组键值并将其与关键字相同

时间:2017-06-14 09:03:54

标签: php mysql

Array (
    [Name] => Amjad Khan 
    [percentage] => 70.0 
    [Hobbies] => Array ( 
        [0] => reading 
        [1] => cricket 
        [2] => football 
        [3] => hockey
    ) 
) 

我有上面的数组。

我需要过滤[爱好],如

[Hobbies]=>reading,cricket,football,hockey
爱好关键是动态而不是静态

4 个答案:

答案 0 :(得分:0)

不完全确定你想要什么,但试试这个:

$key = 'Hobbies';
$array[$key] = implode(',', $array[$key]);

答案 1 :(得分:0)

使用implode功能

$arr = Array ( 'Name' => "Amjad Khan", 'percentage' => 70.0 ,'Hobbies' => Array ('reading','cricket','football','hockey' ) );
print_r($arr['Hobbies']);
$new = implode(',',$arr['Hobbies']);
echo $new;

演示:http://sandbox.onlinephpfunctions.com/code/dacec2fbf86c62c95a2632aecf3583d608eeb097

答案 2 :(得分:0)

试试这个!!!!!!!!!!!!!!!!!

$arr = array(


   "Name" => Amjad,

   "percentage" => 70.0,

    "Hobbies" => array (
    "0" => reading,

     "1" => cricket, 

   "2" => football

    )         

  );



 foreach ($arr as $key => $value) {

  if($key == Hobbies){


 $arr[Hobbies] = implode(',',$value);

  }

       }

  echo '<pre>';

  print_r($arr);



 }

答案 3 :(得分:0)

您可以检查是否存在爱好键,然后将其值内爆:

if (isset($array['Hobbies'])) {
    $array['Hobbies'] = implode(',', $array[$key]);
}