0

I have this string:

[25-03-15, 1236], [26-03-15, 3000], [27-03-15, 3054], [30-03-15, 4000]

I want to get two parts from it as below:

['25-03-15','26-03-15','27-03-15','30-03-2015']

and

[1236,3000,3054,4000]

Please guide me how I can perform this task.

PS: I am working of view page of codeigniter.

I'm getting the first thing as:

<?php 
$usd=$this->db->query('select transaction_date, SUM(amount) as total 
from transactions GROUP BY transaction_date')->result_array();
$str = '';

for($i=0; $i<count($usd); $i++){
    if($i!=0){
        $str = $str.', ['.date('d-m-y', strtotime($usd[$i]["transaction_date"])).', '.$usd[$i]["total"].']';
    }else{
        $str = $str.'['.date('d-m-y', strtotime($usd[$i]["transaction_date"])).', '.$usd[$i]["total"].']';
    }
}

echo $str;

?>
Rizier123
  • 57,440
  • 16
  • 89
  • 140
Shahid Rafiq
  • 649
  • 1
  • 10
  • 25
  • the very first one in your question one is array or string? – Anant Kumar Singh Apr 01 '15 at 10:20
  • Is it a string, 4 different arrays or 1 multidimensional array? – Daan Apr 01 '15 at 10:20
  • I hope it is a string – Shahid Rafiq Apr 01 '15 at 10:21
  • @ShahidRafiq So this: `[25-03-15, 1236], [26-03-15, 3000], [27-03-15, 3054], [30-03-15, 4000]` is a string not an array? If yes why did you tagged it with array and wrote array in the title? – Rizier123 Apr 01 '15 at 10:22
  • I said i hope, Please check the question again, @Rizier123 I mentioned string or array, any how I have edited the question to explain what kind of thing it is – Shahid Rafiq Apr 01 '15 at 10:24
  • @ShahidRafiq Why do you save the data in a string?? That makes is just very complicated to extract the data which you want! – Rizier123 Apr 01 '15 at 10:25
  • I will pass this extracted data to a graph that accepts this kind of data – Shahid Rafiq Apr 01 '15 at 10:26
  • FYI: You can accept the answer how helped you the most and solved your problem! (http://meta.stackexchange.com/q/5234) You can also do this for all of your other questions if there is such an answer – Rizier123 Apr 01 '15 at 10:48

2 Answers2

1

I don't see any reason why you need to save this data from your db in a string. Just save it in an array and it is that easy:

So here I save your data in an array, so that you get this structure:

array(
    array(25-03-15, 1236),
    array(26-03-15, 3000),
    array(27-03-15, 3054),
    array(30-03-15, 4000)
)

The you can simply use array_column() to extract the single columns, like this:

<?php

    $usd = $this->db->query('select transaction_date, SUM(amount) as total 
    from transactions GROUP BY transaction_date')->result_array();

    foreach($usd as $k => $v)
        $result[] = [date('d-m-y', strtotime($usd[$k]["transaction_date"])), $usd[$k]["total"]];


    $partOne = array_column($result, 0);
    $partTwo = array_column($result, 1);

?>  

Also if you then need this data in a string as you said in the comments you can simply transform it:

I will pass this extracted data to a graph that accepts this kind of data – Shahid Rafiq 6 mins ago

Just use this:

echo $str = "[" . implode("],[", array_map(function($v){
    return implode(",", $v);
}, $usd)) . "]";

output:

[25-03-15, 1236], [26-03-15, 3000], [27-03-15, 3054], [30-03-15, 4000]

EDIT:

If you also want the parts as string just simply do this:

$partOne = "[" . implode("],[", array_column($arr, 0)) . "]";
$partTwo = "[" . implode("],[", array_column($arr, 1)) . "]";

output:

[25-03-15],[26-03-15],[27-03-15],[30-03-15]
[1236],[3000],[3054],[4000]
Community
  • 1
  • 1
Rizier123
  • 57,440
  • 16
  • 89
  • 140
1

Try this..

  $date = '';
  $total = '';
for($i=0; $i<count($usd); $i++){
    if($i!=0){
        $date .= date('d-m-y', strtotime($usd[$i]["transaction_date"])).', ';
         $total .= $usd[$i]["total"].', ';

    }else{
         $date .= date('d-m-y', strtotime($usd[$i]["transaction_date"])).', ';
         $total .= $usd[$i]["total"].', ';
    }
}

 echo $finaldate='['. $date.']';
  echo $finaltotal='['. $total.']';
Deenadhayalan Manoharan
  • 5,357
  • 14
  • 29
  • 48
  • [25-03-15, 26-03-15, 27-03-15, 30-03-15, ] in this ['25-03-15','26-03-15','27-03-15','30-03-2015'] and same about the total Thank you – Shahid Rafiq Apr 01 '15 at 10:36
  • @Jocker *Please accept my answer if it is correct* are you begging for reputation?! (BTW: It is not a good idea to store the data in a string, since it's very hard to maintain!) – Rizier123 Apr 01 '15 at 10:58