Announcement

Collapse
No announcement yet.

Values from text file prepend with data

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Values from text file prepend with data

    Hi Guys, i have an array that i need to use as a certain format, but the values are 4 digit numbers, currently in a text file...

    Is it possible to use this file/list and insert each value into the format required?

    eg.

    Current format :

    1111
    2222
    3333
    4567

    Required format :

    array("y" => 1111),
    array("y" => 2222),
    array("y" => 3333),
    array("y" => 4567),

    So rather than having to manually add it, is it possible to have this automated from a plaintext list?

    Many thanks,

  • #2
    Hi there eddy-r3,

    I am not a "PHP" expert but check out the attachment, it might help.
    Attached Files


    ~ the original bald headed old fart ~

    Comment


    • #3
      Excellent thankyou!

      So the values are now being passed to the correct datapoint, but the format is wrong...

      The format needs to be :

      $dataPoints: array (
      array("y"=>2267),
      array("y"=>2288),
      array("y"=>2258),
      )


      But its coming through as :


      dataPoints: Array
      (
      [0] => 2267
      [1] => 2288
      [2] => 2258
      )
      null

      Comment


      • #4
        Hi there eddy-r3,

        As I said I am not a PHP expert.

        So I do not understand why you require this...

        Code:
        
        $dataPoints: array (
           array("y"=>2267),
           array("y"=>2288),
           array("y"=>2258),
        )
        
        ...which appears to be a multi-dimensional array.


        ~ the original bald headed old fart ~

        Comment


        • #5
          Im trying to integrate this :

          http://canvasjs.com/php-charts/

          Comment


          • #6
            Hi there eddy-r3,

            check out version 2.
            Attached Files


            ~ the original bald headed old fart ~

            Comment


            • #7
              Excellent thankyou!

              There are potentially 1000's of numbers in the list though! LOL

              So although the array uysing $parts would work, how would i go around automating it?

              Thanks so much for your help!

              Comment


              • #8
                Hi there eddy-r3,

                and what about the fruits?


                ~ the original bald headed old fart ~

                Comment


                • #9
                  Well i just literally need the numbers, so the array could potentially look like this format :

                  $dataPoints: array (
                  array("y"=>2267),
                  array("y"=>2288),
                  array("y"=>2258),
                  ......
                  )

                  Comment


                  • #10
                    Hi there eddy-r3,

                    I believe that I have cracked it.

                    Check out version three.
                    Attached Files


                    ~ the original bald headed old fart ~

                    Comment


                    • #11
                      Hey thanks so much for the help!

                      That one worked a treat! So im gone ahead and ruined it all and changed the graph im using, the new input data is as such :

                      line1: [20, 45, 75, 33, 51, 85],

                      But the data from this :

                      $str=file_get_contents("sr.txt");
                      $str=preg_replace('/\s+/', '', $str);
                      $parts=str_split($str, 4);
                      print_r($parts);

                      is coming through as this :

                      [0] => 2267
                      [1] => 2288
                      [2] => 2258
                      [3] => 2277
                      [4] => 2248
                      [5] => 2219

                      I just need the numbers as such : 2267, 2288, 2277 etc etc....

                      Any help?

                      Please & Thankyou!

                      Comment


                      • #12
                        Hi there eddy-r3,


                        Why are you using this...

                        Code:
                        
                            $str=file_get_contents("sr.txt");
                            $str=preg_replace('/\s+/', '', $str);
                            $parts=str_split($str, 4);
                            print_r($parts);
                        
                        ... when I gave you this code...

                        Code:
                        
                        <?php
                           $str=file_get_contents("nine-numbers.txt");
                           $str=preg_replace('/\s+/', '', $str);
                           $parts=str_split($str, 4);
                        
                           $dataPoints=[];
                        for($c=0; $c<count($parts); $c++) {
                           array_push($dataPoints, ["y"=>$parts[$c]]);
                         }
                        ?>
                        
                        ...in the PHP-eddy-r3-version-3 folder files.



                        ~ the original bald headed old fart ~

                        Comment


                        • #13
                          I was getting a code highlighting error on in DW, also it didnt work with my Laravel controller :

                          Where as this :

                          Code:
                          $str=file_get_contents("sr.txt");
                                     $str=preg_replace('/\s+/', '', $str);
                                     $parts=str_split($str, 4);
                                     print_r($parts);
                          Works fine just the printed format i need to alter!

                          Comment


                          • #14
                            Hi there eddy-r3,


                            I'm trying to integrate this ....

                            http://canvasjs.com/php-charts/
                            So you need to use this..

                            Code:
                            
                            <?php
                               $str=file_get_contents("nine-numbers.txt");
                               $str=preg_replace('/\s+/', '', $str);
                               $parts=str_split($str, 4);
                            
                               $dataPoints=[];
                            for($c=0; $c<count($parts); $c++) {
                               array_push($dataPoints, ["y"=>$parts[$c]]);
                             }
                            ?>
                            
                            ... not this...

                            Code:
                             
                                 print_r($parts); 


                            ~ the original bald headed old fart ~

                            Comment


                            • #15

                              ...by the way, have you actually tried the second-test.php file.


                              ~ the original bald headed old fart ~

                              Comment

                              Working...
                              X