-4

Im using a charts plugin an i need to force some array elements to be strings.

$values_x = array();
            for ($i = 0; $i < count( $series[0]['data'] ); $i++ ) {
                $values_x[] = $series[0]['data'][$i][0];
            }

            $values_y = array();
            for ($i = 0; $i < count( $series[0]['data'] ); $i++ ) {
                $values_y[] = $series[0]['data'][$i][1];
            }

            $categories = array(
                "label"  => $label_x,
                "values" => $values_x,
                "colors" => $eco_chart_colors,
            );

            $series = array("label" => null, "values" => array( array("data" => $values_y)));

        } else {

            $categories = array(
                "label"  => $label_x,
                "values" => $values_x,
                "colors" => "",
            );

            $series = array("label" => $label_y, "values" => $series);

        }

i need $values_x to become strings. it outputs the years in the graph but in the json it comes as integers and i need it to be strings. eg: not 2010 but '2010'. ive tried with strval and some other options but cant seem to get it right, it outputs "values":null when i try it. can someone help? thanks in advance.

1
  • 1
    This isn't really a WordPress question, you'd probably have better luck with it over at StackOverflow. – MahdiY Mar 30 '17 at 15:31
2

There are a few ways you can make an integer a string. Here are 2 main ways to make that happen

  1. Type Cast - $values_x[] = (string) $series[0]['data'][$i][0]

  2. Double Quote - $values_x[] "{$series[0]['data'][$i][0]}"

Also, you might want to check for null first and assign a default

$values_x = !empty($values_x) ? $values_x : array('2010');

If you want to make the whole array a string, use implode

"values" => implode(', ', $values_x)

If you have multiple integers, this will output '2010, 2011'. Replace the comma and space with what you want placed between the pieces.

http://php.net/manual/en/function.implode.php

0

I am not sure how your code works. But if $series[0]['data'][$i][0] generates 2010 for example you could do this:

$values_x = array();
        for ($i = 0; $i < count( $series[0]['data'] ); $i++ ) {
            $values_x[] = (string)$series[0]['data'][$i][0];
        }

        $values_y = array();
        for ($i = 0; $i < count( $series[0]['data'] ); $i++ ) {
            $values_y[] = $series[0]['data'][$i][1];
        }

        $categories = array(
            "label"  => $label_x,
            "values" => $values_x,
            "colors" => $eco_chart_colors,
        );

        $series = array("label" => null, "values" => array( array("data" => $values_y)));

    } else {

        $categories = array(
            "label"  => $label_x,
            "values" => $values_x,
            "colors" => "",
        );

        $series = array("label" => $label_y, "values" => $series);

    }

You can force a specifice type with (int), (bool), (string) etc. An example for string would be:

$AlwaysAnInt = (int)$CouldBeAnInt

Not the answer you're looking for? Browse other questions tagged or ask your own question.