Merge duplicate values from array in Json Formate - php

I am fetching data from location MySql table as structure is below
in my PHP code
I need send Json output in response as below (Table data may not be same as below json data but the format is same)
http://jsoneditoronline.org/?id=7c5600712df6f9ec1f8fbb8a13aba3de
Tried to do the following in the code to convert the array that i fetch from the table but however am unable to get it in the right format
$sql = "SELECT * FROM mobile_user_regions";
$stmt = $this->db->prepare($sql);
$stmt->execute();
$resCArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
$ress = array();
foreach ($resCArray as $key => $value) {
$ress['regions'][] = array(array(
'name' => $value['region'],
'location' => array(
array(
'name' => $value['location'],
'store' => array(
'store_details' => $value['store_details'],
'store_phone' => $value['store_phone'],
'store_email' => $value['store_email'],
'store_latitude' => $value['store_latitude'],
'store_longitude' => $value['store_longitude']
)
)
)
)
);
Output: that i am getting is
**http://jsoneditoronline.org/?id=4d4a75177350e254ceee7238af13f2f7**

$regionArray = $xyzObject->getRegion();
if (!empty($regionArray) && isset($regionArray)) {
$i = 0;
$locationData = array();
foreach ($regionArray as $key => $value) {
$locationData['regions'][$i]['name'] = $value['region'];
$locationArray = $xyzObject->getLocation($value['region']);
$locationData['regions'][$i]['locations'] = $locationArray;
$i++;
}
$j = 0;
foreach ($locationData['regions'] as $key => $regions) {
$k = 0;
foreach ($regions['locations'] as $key1 => $locations) {
$storeArray = $xyzObject->getStore($locations['name']);
$locationData['regions'][$j]['locations'][$k]['stores'] = $storeArray;
$k++;
}
$j++;
}
echo json_encode($locationData);

Related

Convert MySQL result to XML format using PHP with multidimensional array

I am trying to get my XML format be like this:
but my code now does not loop for the 'order_line' array and will return like this:
below are sample of my code I did:
$result = $this->db->query("SELECT * FROM `grn_order` a");
foreach($result->result() as $row )
{
$result_line = $this->db->query("SELECT * FROM `grn_order_line` a where a.order_no = '$row->order_no'");
foreach($result_line->result() as $row_line);
{
$line = array(
'guid' => $row_line->guid,
'itemcode' => $row_line->itemcode,
);
}
$my_array[] = array(
'order_no' => $row->order_no,
'loc_code' => $row->loc_code,
'trans_code' => $row->trans_code,
'po_no' => $row->po_no,
'order_line' => $line
);
} $xml = new SimpleXMLElement('<orders/>');
// function callback
$data = $this->array2XML($xml, $my_array);
print $xml->asXML();
function array2XML($obj, $array)
{
foreach ($array as $key => $value)
{
if(is_numeric($key))
$key = 'order';
if (is_array($value))
{
$node = $obj->addChild($key);
$this->array2XML($node, $value);
}
else
{
$obj->addChild($key, htmlspecialchars($value));
}
}
}
You keep on overwriting the last line in your load loop. Change it to...
$line = [];
foreach($result_line->result() as $row_line);
{
$line[] = array(
'guid' => $row_line->guid,
'itemcode' => $row_line->itemcode,
);
}
So each line is added using $line[].

How can i access these arrays in foreach?

how can i access this in foreach in my view?
foreach($data as $key => $row){
$col[$row['user_id']][$row['loan_id']] = $this->db->where('loan_user_id', $row['user_id'])->where('loan_id', $row['coll_loan_id'])->get('loans')->result_array();
$col[$row['user_id']][$row['loan_id']][$row['coll_id']] = $this->db->where('coll_loan_id', $row['loan_id'])->where('coll_user_id', $row['user_id'])->get('collectables')->result_array();
}
In your Controller
$col = array();
foreach($data as $key => $row){
// Simplified the where condition by using array in where object
$user_id = $row['user_id'];
$load_id = $row['loan_id'];
$col[$user_id][$load_id] = $this->db->where(array('loan_user_id' => $user_id,'loan_id' => $row['coll_loan_id']))->get('loans')->result_array();
$col[$user_id][$load_id][$row['coll_id']] = $this->db->where(array('coll_loan_id' => $row['loan_id'], 'coll_user_id' => $row['user_id']))->get('collectables')->result_array();
}
$this->load->view('view_name', array('view_data' => $col));
in view file
// Loop here
echo '<pre>';print_r($view_data);

Unite into one array based array from explode string in php

I have a lot of array based explode a string like this :
$size = explode(",", $row->SIZE);
$coil_no = explode(",", $row->COIL_NO);
$net = explode(",", $row->NET);
$gross = explode(",", $row->GROSS);
$contract_no = explode(",", $row->CONTRACT_NO);
$location = explode(",", $row->LOCATION);
How can I unite those array into one multidimensional array ?
I have try like this :
foreach ($size as $value) {
foreach ($coil_no as $coil) {
$detail[] = array(
"coil_no" => $coil,
"size" => $value
);
}
}
you know the result the looping is loop weird,
I need more elegant array, like
foreach ($unite_array as $row) :
echo "<tr> $row->size </tr>" ;
endforeach;
What the best way to unite those array ?
You can write your own function which does the grouping on keys as required, see example below:
function array_group(){
if(func_num_args() > 0) {
$params = func_get_args();
$result = array();
foreach($params as $key => $param) {
foreach($param['values'] as $k => $value) {
$result[$k][$param['alias']] = $value;
}
}
return $result;
}
return false;
}
$rows = array_group(
array('alias' => 'size', 'values' => array(1,2,3,4,5)),
array('alias' => 'coil_no', 'values' => array(6,7,8,9,10)),
array('alias' => 'net', 'values' => array(11,12,13,14,15)),
array('alias' => 'gross', 'values' => array(16,17,18,19,20)),
array('alias' => 'contract_no', 'values' => array(21,22,23,24,25)),
array('alias' => 'location', 'values' => array(26,27,28,29,30))
);
print_r($rows);
And you can access it like:
foreach ($rows as $row) :
echo "<tr> {$row['size']} </tr>" ;
endforeach;

Store array into array of array

Im generating Chart4PHP.
In sample it takes data like this
$p->data = array(array(array("2010/10",-48),array("2011/01",238),array("2011/02",395)));
I have array "rows" constructed of row[date][units].
Im storing it in this way:
$rows = array();
for(...)
{
$row[date] = $mydate;
$row[units]= $myunits;
$rows[]=$row;
}
What I should make additionally to be able to use it as $p->data = $rows;
To add the extra array container, call array() with the rows array as the argument.
$data = array(array('date' => "2010/10", 'units' => -48),
array('date' => "2011/01", 'units' => 238),
array('date' => "2011/02", 'units' => 395));
foreach ($data as $d) {
$mydate = $d['date'];
$myunits = $d['units'];
$rows[] = array($mydate, $myunits);
}
$p->data = array($rows);

PHP While loop array

Any help is appreciated.
I have an array that is forfetch like this. The reason is to brake down a product in individual arrays. However I can not figure out what statement to put in the while loop so i can loop through each array in $row. initially the statement should be
while ($row = mysql_fetch_assoc($result))
however this was already done to be able to sort the array.
$sorted = array_orderby($newarray, 'volume', SORT_DESC, 'edition', SORT_ASC);
foreach($sorted as $row)
{
while( ??????? )
{
$row = build_items($row);
$template->assign_block_vars('featured_items', array(
'ID' => $row['id'],
'IMAGE' => $row['pict_url'],
'TITLE' => $row['title'],
'SUBTITLE' => $row['subtitle'],
'BUY_NOW' => ($difference < 0) ? '' : $row['buy_now'],
'B_BOLD' => ($row['bold'] == 'y')
));
$k++;
$feat_items = true;
}
}
Just found the answer. Sorry guys im new at PHP.
foreach($sorted AS $row) {
$row = build_items($row);
// time left till the end of this auction
$s_difference = time() - $row['starts'];
$difference = $row['ends'] - time();
$bgcolour = ($k % 2) ? 'bgcolor="#FFFEEE"' : '';
$template->assign_block_vars('featured_items', array(
'ID' => $row['id'],
'IMAGE' => $row['pict_url'],
'TITLE' => $row['title'],
'SUBTITLE' => $row['subtitle'],
'BUY_NOW' => ($difference < 0) ? '' : $row['buy_now'],
'BID' => $row['current_bid'],
'BIDFORM' => $system->print_money($row['current_bid']),
'TIMELEFT' => FormatTimeLeft($difference),
'NUMBIDS' => $row['num_bids'],
'B_BOLD' => ($row['bold'] == 'y')
));
$k++;
$feat_items = true;
}
foreach($sorted AS $rows) {
foreach($rows AS $row) {
...
}
}
or with keys/indices
foreach($sorted AS $key => $rows) {
foreach($rows AS $index => $row) {
Yes.., we can use foreach to print all the values in an array.
Example: I have an array called "data" (SQLITE dynamic data). I want to print all the values which are there on "data" array.
By using following sample code we can print the values in a table format.
foreach ($data as $item) {
$date = $item['date'];
$url = $item['url'];
$name = $item['name'];
echo"
<tr>
<td>$date</td>
<td>$name</td>
<td>$url</td>
</tr>
";
}
Please let me know if i did any mistakes here, sorry for my bad English.

Categories