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
  • 58,877
  • 16
  • 101
  • 156
Shahid Rafiq
  • 669
  • 1
  • 12
  • 27

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
  • 58,877
  • 16
  • 101
  • 156
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,436
  • 14
  • 30
  • 50
  • [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