dongsu4345 2018-08-29 17:44
浏览 203

为PHP数组中的各个变量指定颜色

I'm not sure if this is possible, but the concept is similar to using strtoupper. I am trying to set a color # value for each of the items in the array. Is there a valid PHP color function which can be applied here?

e.g. [0] = #ed7b7 [1] = #c13f0

These values are being called later on in the code using jQuery to display in a Javascript SweetAlert and I am trying to get each of the strings to appear as a different colour within the alert.

Segment of code where I am converting string to uppercase:

    // Return the product category count that belongs to the added item
if( has_term( $categories[0], 'product_cat', $product_id ) )
    echo json_encode(array(strtoupper($categories[0]) => $counts[0])); // Returned value to jQuery
if( has_term( $categories[1], 'product_cat', $product_id ) )
    echo json_encode(array(strtoupper($categories[1]) => $counts[1])); // Returned value to jQuery
}

And here is the Javascript alert where I am then trying to call the modified response with the colored variables:

    success: function (response) {
                    $.each( JSON.parse(response), function( category, count ) {
                        if( count == 4 ){                         
swal({
  type: 'success',
  title: "You've Added The 4 Minimum "+category+" Items!",
  allowOutsideClick: false,
  showCancelButton: false,
  showConfirmButton: false,
timer: 3000,
})  
  • 写回答

1条回答 默认 最新

  • duandang9434 2018-08-29 17:58
    关注

    You can generate any random color using the following function and assign to your array as color attribute.

    function random_color_part() {
            return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
        }
    
        function random_color() {
            return random_color_part() . random_color_part() . random_color_part();
        }
    
    if( has_term( $categories[0], 'product_cat', $product_id ) )
        echo json_encode(array(strtoupper($categories[0]) => $counts[0],'color' => random_color())); // Returned value to jQuery
    if( has_term( $categories[1], 'product_cat', $product_id ) )
        echo json_encode(array(strtoupper($categories[1]) => $counts[1],'color' => random_color())); // Returned value to jQuery
    }
    

    Generating a random hex color code with PHP

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line