I try to merge the data's in the arrays 'c' and 'a' inside MyData with the following code, but the outcome was still corrupted.
Are there something wrong with my code ? Or am I simple making a mistake with how I merge the arrays ? I'm doing all sorts of stuff to solve the problem but cannot find any solution that works. Some examples or tips will be great!
Want to merge [my_test] and [my_date] inside [MyData].
Array
(
[0] => Array
(
[MyData] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
)
[c] => Array
(
[my_test] => math
)
[a] => Array
(
[my_date] => 2017-08-13
)
).....Loops
[1] => Array
(
I would want the result to be like
Array
(
[0] => Array
(
[MyData] => Array
(
[id] => 79
[my_birth_day] => 1990-06-20
[my_address] => 400
[my_age] => 26
[my_name] => Joy
[my_id] => 1
[created] => 2017-06-19 15:39:44
[my_test] => math
[my_date] => 2017-08-13
I made a logic to merge the arrays and display it as the above code , but wasn't able to merge
$res = $this->find( 'all', $cond); // All the data are fetchd from this $res
$count = count($res);
for($i=0;$i<$count;$i++){
$result[] = $res[$i] ;
$fixed_arrays[] = $result[$i]['MyData'];
if (!empty($result[$i]['c'])) {
$corrupt_c_array = $result[$i]['c'];
$fixed_arrays = array_merge($fixed_arrays,$corrupt_c_array);
}
if(!empty($result[$i]['a'])) {
$corrupt_a_array = $result[$i]['a'];
$fixed_arrays = array_merge($fixed_arrays, $corrupt_a_array);
}
}
$result['data'] = $fixed_arrays; // This $result['data'] should show the expected result.
[Update]
Heard about a function called set::combine for cakephp2, Is there a way to use set::combine since it's cakephp2?
You can create the temporary $data array and merge everything and then assign to the $fixed_arrays list
<?php
$res = $this->find( 'all', $cond); // All the data are fetched from this $res
$count = count($res);
for($i=0;$i<$count;$i++){
$result[] = $res[$i] ;
$data =array(); //temporary array
$data['MyData'] = $result[$i]['MyData'];
if (!empty($result[$i]['c']) && isset($result[$i]['c']['my_test'])) {
$corrupt_c_array = $result[$i]['c'];
$data['MyData']['my_test'] = $result[$i]['c']['my_test'];
}
if(!empty($result[$i]['a']) && isset($result[$i]['a']['my_date'])) {
$corrupt_a_array = $result[$i]['a'];
$data['MyData']['my_date'] = $result[$i]['a']['my_date'];
}
$fixed_arrays[] = $data;
}
$result['data'] = $fixed_arrays;
If you use the condition then you can not get the all values if it will blank so use like this :
<?php
$res = $this->find('all', $cond); // All the data are fetched from this $res
$count = count($res);
for ($i = 0; $i < $count; $i++) {
$result[] = $res[$i];
$data = array(); //temporary array
$data[$i]['MyData'] = $result[$i]['MyData'];
$data[$i]['MyData']['my_test'] = $result[$i]['c']['my_test'];
$data[$i]['MyData']['my_date'] = $result[$i]['a']['my_date'];
}
$result['data'] = $data;
Just Print the $data or $result to get array like you want.
$res = $this->find ( 'all', $cond );
foreach ( $res as &$result ) {
$result ['MyData'] ['my_test'] = $result ['c'] ['my_test'];
$result ['MyData'] ['my_date'] = $result ['a'] ['my_date'];
unset ( $result ['a'] );
unset ( $result ['c'] );
}
echo "<pre>";
print_r ( $res );
exit ();
Related
I am confused with array.
What I want to do is two merge two array, but this kind of the two array are different:
Array
(
[0] => Array
(
[sud] => 60
[sad] => Array
(
[incharge] => Perusahaan
[perusahaan_id] => 1
[barang_id] => 3
[gudang_id] => 2
[stock] => 1
)
)
[1] => Array
(
[sud] => 23
[sad] => Array
(
[incharge] => Perusahaan
[perusahaan_id] => 1
[barang_id] => 4
[gudang_id] => 1
[stock] => 2
)
)
)
I want to move the array of [sud] into [sad] array, and named it as quantity.
This is my codes which generate the array above:
if($q->num_rows() > 0)
{
foreach ($q->result() as $row => $rows)
{
$data[] = $rows;
$stock[] = $rows->stock;
}
}
$i = -1;
foreach ($update as $updates)
{
$i++;
$test3['sud'] = $stock[$i];
$test3['sad'] = $updates;
$happy[] = $test3;
}
print_r ($happy);
What I want to do here actually is to check if the number of array [stock] => value is not bigger than the number in array [sud].
Please help, thanks in advance.
If I understood well, you want to change it like this:
if($q->num_rows() > 0)
{
foreach ($q->result() as $row => $rows)
{
$data[] = $rows;
$stock[] = $rows->stock;
}
}
$i = -1;
foreach ($update as $updates)
{
$i++;
$test3['sad'] = $updates;
$test3['sad']['quantity'] = $stock[$i];
$happy[] = $test3;
}
print_r ($happy);
Fetch all rows based on the query into an array and return single value
Query database for data
//----------------------------------------------------------------------
$result = mysql_query("SELECT * FROM $tableName"); query
$array = mysql_fetch_row($result);
fetch result In Array
$arr = array();
while ($row = mysql_fetch_assoc($result)) {
$arr2 = array();
foreach ($row as $val) $arr2[] = $val;
$arr[] = $arr2;
}
Result Will Be
Array
(
[0] => Array
(
[0] => status_site
[1] => 0
)
[1] => Array
(
[0] => title_site
[1] => Script
)
[2] => Array
(
[0] => keys_site
[1] =>
)
)
I need to make function that return element 0 to 1
ex: function getsetting (title_site){
return value script}
Replace the $arr[] = $arr2; with $arr[$arr2[0]] = $arr2[1];. That might solve your problem.
I am looking for some advices how could I sort this kind of Array by 'variant_name' key.
Because Array is really huge I minified it to the looking result state.
...
$filter[2413][1][81][sub_id] = 1;
$filter[2413][1][81][variant_id] = 81;
$filter[2413][1][81][variant_name] = 'Banana';
$filter[2413][2][87][sub_id] = 2;
$filter[2413][2][87][variant_id] = 87;
$filter[2413][2][87][variant_name] = 'Apple';
$filter[2413][3][32][sub_id] = 3;
$filter[2413][3][32][variant_id] = 32;
$filter[2413][3][32][variant_name] = 'Carrot';
...
Keys $filter[x][x][x] are not sequential.
I have tried the sort function I used before but it doesn't work with this kind of Array:
function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
array_sort_by_column($filter[][][], 'variant_name');
My target is modify array by sorting 'variant_name' to 'Apple', 'Banana', 'Carrot' accordingly keeping the array structure.
It's a working code tested from given examples.
Note that my codes still can be optimized, etc. Just take it as my advice.
TL;DR = Use usort().
function mySort($a,$b)
{
$av = "";
$bv = "";
foreach($a as $ak)
$av = $ak['variant_name'];
foreach($b as $bk)
$bv = $bk['variant_name'];
if($av[0] < $bv[0])
return false;
else return true;
}
How to use it? You have to specify which first level array to sort.
usort($filter['2413'],"mySort");
Then the result I got is:
Array
(
[2413] => Array
(
[0] => Array
(
[87] => Array
(
[sub_id] => 2
[variant_id] => 87
[variant_name] => Apple
)
)
[1] => Array
(
[81] => Array
(
[sub_id] => 1
[variant_id] => 81
[variant_name] => Banana
)
)
[2] => Array
(
[32] => Array
(
[sub_id] => 3
[variant_id] => 32
[variant_name] => Carrot
)
)
)
)
How can I create an array like the following in PHP from a database result set using a loop:
Array
(
[T] => Array
(
[0] => Array
(
[id] => 1
[name] => Timer
)
[1] => Array
(
[id] => 2
[name] => Tub
)
)
[P] => Array
(
[0] => Array
(
[id] => 3
[name] => Paper
)
[1] => Array
(
[id] => 4
[name] => Puppy
)
)
)
You will notice that the array keys are a letter, which is taken from the 'name' value in the result set. The loop will be something like this:
while($result = $db->fetch($query) {
$key = $result['name']{0};
// your answer :-)
}
I think something like this should do it:
$sql = 'SELECT id, name FROM table';
$result = mysql_query( $sql);
$answer = array();
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = $row;
}
mysql_free_result( $result);
var_dump( $answer);
OR, to be more specific (if your query is returning more columns than just id and name):
while( $row = mysql_fetch_assoc( $result))
{
$answer[ strtoupper($row['name'][0]) ][] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
$indexArray = array(); // Array from Example
while($result = $db->fetch($query) {
$key = $result['name']{0};
if(!isset($indexArray[$key])) {
$indexArray[$key] = array();
}
array_push($indexArray[$key], $result);
}
$results = array();
while($result = $db->fetch($query)) {
$key = strtoupper($result['name'][0]);
if(!isset($results[$key]))
$results[$key] = array();
$results[$key][] = $result;
}
I was wondering if anyone could help me restructure a predefined php array. The output of my current array is:
Array
(
[71-ctns] => 1
[71-units] => 1
[308-units] => 1
[305-ctns] => 1
[306-units] => 2
)
And I would like it to look like:
Array
(
[71] => Array
(
[ctns] => 1
[units] => 1
)
[308] => Array
(
[units] => 1
)
[305] => Array
(
[ctns] => 1
)
[306] => Array
(
[units] => 2
)
)
Is this possible?
This should do it
$merged = array();
foreach($a as $k=>$v){
$t = explode('-',$k);
$id = intval($t[0]);
if(!array_key_exists($id, $merged))
$merged[$id] = array();
$merged[$id][$t[1]] = $v;
}
EDIT:
Sorry you should use explode instead of split.
Yes, but you need to loop (note: array_map can also work, but this example is more explicit):
$fin = array();
foreach( $complex as $item => $val )
{
$pieces = explode('-', $item);
$fin[$pieces[0]] = isset($fin[$pieces[0]])?:array();
$fin[$pieces[0]][$pieces[1]] = $val;
}
Find below code to restructure a predefined php array
<?php
$newArray=array();
$result = array("71-ctns"=>1,"71-units"=>1,"308-ctns"=>1,"308-units"=>1,"305-units"=>1,"306-units"=>2);
if(is_array($result) && count($result)>0) {
foreach($result as $key=>$val) {
$getKeyArray = explode("-",$key);
$newArray[$getKeyArray[0]][$getKeyArray[1]] =$val;
}
}
print"<pre>";
print_r($newArray);
?>