zoukankan      html  css  js  c++  java
  • PHP中一些常用知识点

    1、json字符串转json对象

    $data='[{"user_id":"93","price":"52.50"},{"user_id":"93","price":"52.50"},{"user_id":"93","price":"52.50"}]';
    $jsonObj = json_decode($data,true);
    var_dump($jsonObj);

    输出如下:

    array (size=3)
      0 => 
        array (size=2)
          'user_id' => string '93' (length=2)
          'price' => string '52.50' (length=5)
      1 => 
        array (size=2)
          'user_id' => string '93' (length=2)
          'price' => string '52.50' (length=5)
      2 => 
        array (size=2)
          'user_id' => string '93' (length=2)
          'price' => string '52.50' (length=5)

    这种输出方法与下面的一样:

     $sql = "SELECT * " ."FROM goods WHERE is_hot = 1";
     $row = $GLOBALS['db']->getAll($sql);
     var_dump($row);

    输出如下:

    array (size=10)
      0 => 
        array (size=52)
          'goods_id' => string '1089' (length=4)
          'cat_id' => string '310' (length=3)
          'goods_sn' => string 'ECS001201' (length=9)
          'goods_name' => string '喇叭袖中长上衣夏季短袖T恤打底裙' (length=86)
         
      1 => 
        array (size=52)
          'goods_id' => string '1099' (length=4)
          'cat_id' => string '336' (length=3)
          'goods_sn' => string 'ESCOO1105' (length=9)
          'goods_name' => string '榄油菜籽油玉米植物调和油商超同款' (length=86)
        
      2 => 
        array (size=52)
          'goods_id' => string '1110' (length=4)
          'cat_id' => string '336' (length=3)
          'goods_sn' => string 'ECS001110' (length=9)
          'goods_name' => string '自榨菜油5l食用油非转基因粮油' (length=88)
          

     2、怎样保存JSON字符串?怎样添加或删除一个JSON字符串

    //定义一个数组
    $allowArr=array();
    //把从数据库库中获取的[{"user_id":"93","user_name":"james","price":"88"},{"user_id":"293","user_name":"139335","price":"52.50"}]字符串转换为数组对象
    $allowMemberArr=json_decode($allowMember); //这里注意如果使用的是$allowMemberArr=json_decode($allowMember,true);遍历取值的时候要用$row['user_id'],而不是$row->user_id

    //遍历并添加到数组行
    foreach ($allowMemberArr as $key=>$row) { $allowArr[] = array('user_id' => $row->user_id, 'user_name' => $row->user_name, 'price' => $row->price); } //添加一个数组行 $allowArr[]=array('user_id'=>$user_id,'user_name'=>$user_name,'price'=>$price); //把所有已经添加的数组行转换为JSON字符串,然后就可以保存了(JSON_UNESCAPED_UNICODE 防止中文乱码) $jsonString =json_encode($allowArr,JSON_UNESCAPED_UNICODE);

     删除其中一个数组行也可以用遍历判断进行去除

    $allowArr=array();
        $allowMemberArr=json_decode($allowMember);
        foreach ($allowMemberArr as $key=>$row){
            if($row->user_id!=$userId){
                $allowArr[]=array('user_id'=>$row->user_id,'user_name'=>$row->user_name,'price'=>$row->price);
            }
        }

     3、在数组中判断某个值是否存在

    in_array(value,array,type)

    该函数的作用是在数组array中搜索指定的value值,type是可选参数,如果设置该参数为 true ,则检查搜索的数据与数组的值的类型是否相同,即恒等于(区分大小写)。

    
    
    //如果按JSON来说,解决的形如:["Peter", "Joe", "Glenn", "Cleveland"]方式
    $people = array("Peter", "Joe", "Glenn", "Cleveland");
    if(in_array("Glenn",$people)){
      echo "Match found";
    }else{
      echo "Match not found";
    }
    
    输出:
    Match found
    //与下面这种方式是一样的,如:$arrStr='["20","60","55"]';这种类型的字符串,如果用它进行比较需要转换为数组:$arr=$json->decode($_GET['ids']);这种方法可以解决如:go.php?act=drop_group_users&ids=["287","294","302"]
    if(in_array("60",$arr)){
      echo "Match found";
    }else{
      echo "Match not found";
    }

    array_key_exists(key,array)

    该函数是判断某个数组array中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false。

    提示:请记住,如果您指定数组的时候省略了键名,将会生成从 0 开始并且每个键值对应以 1 递增的整数键名

    //如果按JSON来说,解决的形如:{"a":"Dog","b":“Cat"}方式
    $a=array("a"=>"Dog","b"=>"Cat");
    if(array_key_exists("a",$a)){
    echo "Key exists!";
    }else{
    echo "Key does not exist!";
    }
    
    输出:
    
    Key exists!

    array_search(value,array,strict)

    array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,则返回匹配该元素所对应的键名。如果没找到,则返回 false。注意在 PHP 4.2.0 之前,函数在失败时返回 null 而不是 false。同样如果第三个参数 strict 被指定为 true,则只有在数据类型和值都一致时才返回相应元素的键名。

    $a=array("a"=>"Dog","b"=>"Cat","c"=>5,"d"=>"5");
    echo array_search("Dog",$a);
    echo array_search("5",$a);

    输出:
    ad

  • 相关阅读:
    查看Linux内核版本的命令
    Viterbi算法
    在matlab 画箭头
    BST和DST简单的matlab程序(图的广度和深度遍历)
    灯光设置(light)
    VC++安装window8.1系统
    prim algorithm
    模拟退火
    Kruskal最小生成树
    matlab练习程序(演化策略ES)
  • 原文地址:https://www.cnblogs.com/firstcsharp/p/10698472.html
Copyright © 2011-2022 走看看