1

How to fetch array in correct order ?

I am printing a array but it is not fetching in correct order.

I want that each track value contain array of date and array of number respectively. please give a solution .Any help would be highly appreciated.

My code for fetch array are:

  <?php
        foreach($posts as $post)            
        {
            $array['track_value'][] = $post->track_value;
            $array['track_value']['date'][] = $post->date;
            $array['track_value']['num'][] = $post->num; 
         }  
  ?>    

From this i am getting wrong value like these:

  <?php
       Array (
     [track_value] => Array
         (
              [0] => mobile
              [date] => Array
                  (
                      [0] => 2015-08-23
                      [1] => 2015-08-24
                      [2] => 2015-08-23
                     [3] => 2015-08-24
                      [4] => 2015-08-22
                      [5] => 2015-08-23
                      [6] => 2015-08-24
             )

              [num] => Array
                  (
                      [0] => 1
                      [1] => 1
                      [2] => 1
                      [3] => 2
                      [4] => 1
                      [5] => 1
                      [6] => 1
                  )

              [1] => mobile
              [2] => laptop
              [3] => laptop
              [4] => pc
              [5] => pc
              [6] => pc
          )

  )
  ?>

The output should be like:

  <?php
  Array (
      [track_value] => Array
          (
              [0] => mobile
              Array
              (
                  [date] => Array
                      (
                          [0] => 2015-08-23
                         [1] => 2015-08-24
                      )

                  [num] => Array
                      (
                   [0] => 1
                    [1] => 1
                 )
              )

              [1] => laptop
              Array
              (
                  [date] => Array
                      (
                          [0] => 2015-08-23
                     [1] => 2015-08-24
                      )

                  [num] => Array
                      (
                          [0] => 1
                          [1] => 2
                      )
              )    
              [2] => pc
              Array
              (
                  [date] => Array
                      (
                          [0] => 2015-08-23
                          [1] => 2015-08-24
                          [2] => 2015-08-23
                      )

                  [num] => Array
                      (
                          [0] => 1
                          [1] => 1
                         [2] => 1
                      )
              )
          )

  )
 ?>
1
  • You can not create an array like that, as everything in an array is a key-value-pair. If you don't specify a key, it will use its own index. Commented Aug 25, 2015 at 7:48

3 Answers 3

1

I'm not sure what you are trying to do, but maybe this will help you on the way:

foreach($posts as $post)            
{
    $array['track_value'][$post->track_value]['date'][] = $post->date;
    $array['track_value'][$post->track_value]['num'][] = $post->num;
}

This will give you an output like:

Array(

    'track_value' => Array(

        'mobile' => Array(

            'date' => Array(

                0 => '2015-08-25',
                1 => '2015'08'26',

            ),

            'num' => Array(

                0 => 1337,
                1 => 13337,

            ),
        ),
        'pc' => ...
    ),
)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

  $arr = [];

  foreach($posts as $key => $val)
   $arr['track_value'][$val->track_value][] = ['date' => $val->date, 'num' => $val->num];
  }

  print_r($arr);

Comments

0

Try this:

  $tracks = [];

  foreach($posts as $k1 as $v1) {

         if (is_array($value)){

           foreach ($value as $k2 => $v2) {

              $tracks[$k1][$k2] = $v2;
           }
         }

         $tracks[$k1] = $v1;
  }  

1 Comment

Has has multiple dates within a [$key] so it will override the previous stored one in this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.