-1

I've got a project where I'm pulling data from SOQL using salesforce's RestAPI. I've cleaned up the response and this is what I'm currently working with:

Created:2021-03-02,Tracks:1,Created:2021-02-23,Tracks:3,Created:2021-02-20,Tracks:4,Created:2021-02-16,Tracks:41,Created:2021-02-06,Tracks:1,Created:2021-02-02,Tracks:3,Created:2021-02-01,Tracks:2,Created:2021-01-29,Tracks:1,Created:2021-01-22,Tracks:1,Created:2021-01-19,Tracks:1,Created:2021-01-15,Tracks:1,Created:2021-01-14,Tracks:1,Created:2021-01-13,Tracks:1

I'm working in PHP and I'd like to build an array based off this data. For example:

graph_data = array(
          array("Created" => array("2021-03-02","2021-02-23","2021-02-20","2021-02-16")),
          array("Tracks" => array(1,3,4,41,1)));

Anyone have suggestions for this? I've tried doing some regex but I don't think it's the right solution. Originally, I got this back as an object but there's no clean way to get them into an associative array as json_decode and json_encode did not work.

For reference, I'm building a chart based on this dynamic data and need the date created as the x axis and tracks as y axis.

pseudoseed
  • 21
  • 3

2 Answers2

2

A solution with explode() and foreach()

$arr = explode(',', $str);
$finalArray = [];


foreach($arr as $ar){
    $againExplode = explode(':',$ar);
    $finalArray[$againExplode[0]][] = $againExplode[1];
}

print_r($finalArray);

Output: https://3v4l.org/JoeJK

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • 1
    This is the most direct and clean answer on the page. This is what I would recommend and how I would do it in my own project. (although not technically what the OP's desired output structure is -- but this structure is better anyhow) – mickmackusa Mar 23 '21 at 23:39
  • 1
    This was extremely helpful, thank you so much! – pseudoseed Apr 19 '21 at 22:26
0

This can be achieved using in built PHP functions:

explode() and array_chunk()

<?php
$str = 'Created:2021-03-02,Tracks:1,Created:2021-02-23,Tracks:3,Created:2021-02-20,Tracks:4,Created:2021-02-16,Tracks:41,Created:2021-02-06,Tracks:1,Created:2021-02-02,Tracks:3,Created:2021-02-01,Tracks:2,Created:2021-01-29,Tracks:1,Created:2021-01-22,Tracks:1,Created:2021-01-19,Tracks:1,Created:2021-01-15,Tracks:1,Created:2021-01-14,Tracks:1,Created:2021-01-13,Tracks:1
';

$arr = explode(',', $str);
$temp = array_chunk($arr, 2);
$finalArray = [];
$i=0;
if (! empty($temp)) {
    foreach ($temp as $elem) {
        if (! empty($elem)) {
            foreach ($elem as $secondary) {
                $tempSecondary = explode(':', $secondary);
                $finalArray[$i][$tempSecondary[0]] = $tempSecondary[1];
            }
        }
                ++$i;
    }
}
echo '<pre>';print_r($finalArray);echo '</pre>';

Output:

Array
(
    [0] => Array
        (
            [Created] => 2021-03-02
            [Tracks] => 1
        )

    [1] => Array
        (
            [Created] => 2021-02-23
            [Tracks] => 3
        )

    [2] => Array
        (
            [Created] => 2021-02-20
            [Tracks] => 4
        )

    [3] => Array
        (
            [Created] => 2021-02-16
            [Tracks] => 41
        )

    [4] => Array
        (
            [Created] => 2021-02-06
            [Tracks] => 1
        )

    [5] => Array
        (
            [Created] => 2021-02-02
            [Tracks] => 3
        )

    [6] => Array
        (
            [Created] => 2021-02-01
            [Tracks] => 2
        )

    [7] => Array
        (
            [Created] => 2021-01-29
            [Tracks] => 1
        )

    [8] => Array
        (
            [Created] => 2021-01-22
            [Tracks] => 1
        )

    [9] => Array
        (
            [Created] => 2021-01-19
            [Tracks] => 1
        )

    [10] => Array
        (
            [Created] => 2021-01-15
            [Tracks] => 1
        )

    [11] => Array
        (
            [Created] => 2021-01-14
            [Tracks] => 1
        )

    [12] => Array
        (
            [Created] => 2021-01-13
            [Tracks] => 1

        )

)

Explanation:

  1. Split the string by comma.

  2. Now, use array_chunk() function to split array into elements of two length.

  3. Append the splited array into resutlant array.

Pupil
  • 23,834
  • 6
  • 44
  • 66