I'm trying to put data into from:
[comments] => Array
(
[count] => 2
[data] => Array
(
[0] => Array
(
[idcomments] => 1
[from] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
[text] => testing this comment out
)
[1] => Array
(
[idcomments] => 2
[from] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
[text] => more comments yeah
)
)
)
I have something like this:
while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
$json['data'][] = array(
"comments" =>array(
"count"=>$SQL_ccount[0],
"data" => array($SQL_comments->fetchAll(PDO::FETCH_ASSOC),
"from" => array($SQL_comments_from->fetchAll(PDO::FETCH_ASSOC)))
),
);
}
But it doesn't fall into the from. Not sure what the right syntax is to get $SQL_comments_from into data such like the example above.
This is what I'm getting
[comments] => Array
(
[count] => 2
[data] => Array
(
[0] => Array
(
[0] => Array
(
[idcomments] => 1
[from] => 1
[text] => testing this comment out
)
[1] => Array
(
[idcomments] => 2
[from] => 2
[text] => more comments yeah
)
)
[from] => Array
(
[0] => Array
(
[idusers] => 1
[username] =>
[full_name] => AndrewLiu
)
)
)
)
I'm trying to get "from" into the "data". The example I provided does not make the "from" go into the other "from" (thats right under idcomments)
Thanks in advance!
Your data field uses data from SQL_comments_from and $SQL_comments_from mixed together, this should generate what you want.
while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
$from = $SQL_comments_from->fetchAll(PDO::FETCH_ASSOC);
$data= array();
foreach ($SQL_comments->fetchAll(PDO::FETCH_ASSOC as $items){
$data[] = array('idcomments' => $items['idcomments'], 'from' => $from[$items['from']], 'text' => $items['text']);
}
$json['comments'][] = array(
"count"=>$SQL_ccount[0],
"data" => $data;
)
),
);
}
Related
My arrays result is like this one
Array
(
[0] => Array
(
[id] => Bank Transfer
[ec] => 1000
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
)
)
[1] => Array
(
[id] => Wired
[ec] => 1001
[accounts] => Array
(
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
[2] => Array
(
[name] => Account ID
[value] =>
)
)
)
)
It's weird because 2nd array of accounts contains same value as first array.
[0] => Array
(
[name] => Account WD
[value] =>
)
[1] => Array
(
[name] => Keterangan
[value] =>
)
How to prevent this duplicated so the 2nd array of accounts will only show
[0] => Array
(
[name] => Account ID
[value] =>
)
Here's my code
$arr = $arr_pay = array();
foreach($site_payment as $key => $value){
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
Array of $site_payment contains
[Bank Transfer] => Array
(
[id] => Bank Transfer
[ec] => 1000
[status] => 1
[auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
)
[Dana] => Array
(
[id] => Wired
[ec] => 1001
[status] => 1
[auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
)
I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array
Please help me to solve the problem
Duplication is due to the $arr is not being reset
$arr_pay = array();
foreach($site_payment as $key => $value){
$arr = array(); // Resetting
if($value['status'] && $value['ec']>=1000){
$payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
foreach ($payment_data_cust as $ke => $va) {
$arr[] = array("name"=>$va,"value"=>'');
}
$spc[] = array(
"id"=>$value['id'],
"ec"=>$value['ec'],
"accounts"=>$arr
);
}
}
I have an Array of Strings, each String that exist in Advanced Custom Fields groups should be returned in foreach loop. Final results should be a single array of all values.
$lubuvna_groups = acf_get_field_groups();
$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
foreach($ArrayDiffs as $ArrayDiff) {
$resultsFileToImports[] = $ArrayDiff;
}
//$keysToImports = implode(", ",$resultsFileToImports);
$keysToImports = 'group_lubuvna_contact, group_lubuvna_subscriber';
foreach($resultsFileToImports as $resultsFileToImportsKey) {
$keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
return ( strpos($el['key'], $keysToImports) !== false );
});
}
The above code returns only if one string exists in $keysToImports. Once there are more than one value it doesn't work. I am sure i am missing something, but can't find any solution in here!
It shows me empty Array:
Array ( )
Maybe there is a different way how to get the arrays without strpos?
Final Array should looks like following:
Array ( [0] => Array ( [ID] => 0 [key] => group_lubuvna_contact [title] => ACF Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781382 [_valid] => 1 ) [1] => Array ( [ID] => 0 [key] => group_lubuvna_subscriber [title] => Lubuvna - Subscriber Fields [fields] => Array ( ) [location] => Array ( [0] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => post ) ) [1] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => page ) ) [2] => Array ( [0] => Array ( [param] => post_type [operator] => == [value] => lubuvna_subscriber ) ) ) [menu_order] => 0 [position] => normal [style] => default [label_placement] => top [instruction_placement] => label [hide_on_screen] => [active] => 1 [description] => [local] => json [modified] => 1592781369 [_valid] => 1 ) )
Try changing:
$keysToImports = 'group_lubuvna_contact', 'group_lubuvna_subscriber';
// then return
return ( strpos($el['key'], $keysToImports) !== false );
to
$keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
// then return
return in_array($el['key'], $keysToImports);
Full code update:
$lubuvna_groups = acf_get_field_groups();
$ArrayDiffs = array_diff($resultsFilesKey, $resultsKey);
foreach($ArrayDiffs as $ArrayDiff) {
$resultsFileToImports[] = $ArrayDiff;
}
$keysToImports = ['group_lubuvna_contact', 'group_lubuvna_subscriber'];
foreach($resultsFileToImports as $resultsFileToImportsKey) {
$keysToImports_filtered = array_filter($lubuvna_groups, function($el) use ($keysToImports) {
return in_array($el['key'], $keysToImports);
});
}
How to add extra values to a existing array
$item = get_post_meta($post->ID, 'extra_fileds', true);
when I print $item I get the following
Array
(
[0] => Array ( [name] => test1 [type] => this1 [location] => 1 )
[1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
)
I would like to add a extra field and make it like`
Array
(
[0] => Array ( [name] => test1 [type] => this1 [location] => 1 )
[1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
[2] => Array ( [name] => test3 [type] => this3 [location] => 3 )
)
Thank you in advance
Write
$item[] = ['name'=>'test3','type'=>'this3','location'=>3];
Here either you can use array_push or $rows[] will solve your problem.
Try this code snippet here
ini_set('display_errors', 1);
$rows=Array (
0 => Array ( "name" => "test1","type" => "this1", "location" => 1 ),
1 => Array ( "name" => "test2" ,"type" => "this2", "location" => 2 ) );
$arrayToAdd=Array ( "name" => "test3","type" => "this3", "location" => 3 );
Solution 1:
array_push($rows, $arrayToAdd);
Solution 2:
$rows[]=$arrayToAdd;
Use array push.
$new_array_item=array("name" => "test3","type" => "this3", "location" => 3);
array_push($item, $new_array_item);
print_r($item);
You're array is at the moment stored in $item.
To add a new item use these brackets: [ ].
Here is your code:
$item[] = [
'name' => 'test3'
'type' => 'this3'
'location' => 3
]
You can use this as much as you like to add more items.
I think that this is the best solution but you can also take a look at php array_push() function.
Array
(
[daterange] => Array
(
[0] => 14/02/2017 - 14/02/2017
[1] => 14/02/2017 - 14/02/2017
)
[location] => Array
(
[0] => dggfd
[1] => hyd
)
[latitude] => Array
(
[0] => 3545
[1] => 111111
)
[longitude] => Array
(
[0] => 43545
[1] => 222222
)
)
zeroth index as one row and 1 index as another row .
Support your array name is $arr then do this:
$size = sizeof($arr['daterange']);
for($i=0;$i<$size;$i++) {
$data = array(
'daterange' => $arr['daterange'][$i],
'location' => $arr['location'][$i],
'latitude' => $arr['latitude'][$i],
'longitude' => $arr['longitude'][$i],
);
$this->db->insert('table_name',$data);
}
Hope this will help you. Leave the comment if you have any doubts.
I declare the following array
$job_scope = array( "proposal_id",
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope",
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
I expect it to be created like
array([0] => 'proposal_id',
[1] => 'will_provide' => array([0] => "0",
[1] => "Supervision",
[2] => "Labor",
[3] => "Material",
[4] => "Equpment"),
[2] => 'general_scope',
[3] => 'per_bid' => array([0] => "Yes",
[1] => "No",
[2] => "Omit"),
[4] => 'job_type' => array([0] => "Painting",
[1] => "Sandblasting",
[2] => "Scappling")
But when I print the array it looks like
Array ( [0] => proposal_id [will_provide] => Array (
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment )
[1] => general_scope [per_bid] => Array (
[0] => Yes
[1] => No
[2] => Omit )
[job_type] => Array (
[0] => Painting
[1] => Sandblasting
[2] => Scappling )
I would like the array to be created in the same format as the second section of code.
All you need to do is assign an empty array to the proposal_id and general_scope. So the code will look like this
$job_scope = array( "proposal_id" => array(),
"will_provide" => array("0","Supervision","Labor","Material","Equpment"),
"general_scope" => array(),
"per_bid" => array("Yes","No","Omit"),
"job_type" => array("Painting","Sandblasting","Scappling")
);
It will produce this array
Array (
[proposal_id] => Array ( )
[will_provide] => Array ( [0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
[general_scope] => Array ( )
[per_bid] => Array ( [0] => Yes
[1] => No
[2] => Omit
)
[job_type] => Array ( [0] => Painting
[1] => Sandblasting
[2] => Scappling
))
If you want to callback the value, (ex : call supervision value).
All you need to do is
print_r($job_scope['will_provide'][1])
and that will print the supervision value
use $new_job_scope = array_values($job_scope);
$job_scope = array(
"proposal_id",
"will_provide" => array(
"0",
"Supervision",
"Labor",
"Material",
"Equpment"
),
"general_scope",
"per_bid" => array(
"Yes",
"No",
"Omit"
),
"job_type" => array(
"Painting",
"Sandblasting",
"Scappling"
)
);
$new_job_scope = array_values($job_scope);
print_r($new_job_scope);
PhpFiddle
Create array first !!! Reassign at specified index with 2D array will be more clear to me
<?php
$arr = array('proposal_id','','general_scope','',''); //create array first
$arr[1] = array("will_provide" => array("0","Supervision","Labor","Material","Equpment"));
$arr[3] = array("per_bid" => array("Yes","No", "Omit"));
$arr[4] = array("job_type" => array("Painting","Sandblasting","Scappling"));
var_dump($arr);
?>
I think this process can serve you. I have just used a foreach loop to convert non-int key to int key:
$new_array = '';
foreach($job_scope as $k => $v){
if(is_int($k)){
$new_array[] = $v;
}else{
$new_array[] = [$k => $v];
}
}
print_r($new_array);
Output would be:
Array
(
[0] => proposal_id
[1] => Array
(
[will_provide] => Array
(
[0] => 0
[1] => Supervision
[2] => Labor
[3] => Material
[4] => Equpment
)
)
[2] => general_scope
[3] => Array
(
[per_bid] => Array
(
[0] => Yes
[1] => No
[2] => Omit
)
)
[4] => Array
(
[job_type] => Array
(
[0] => Painting
[1] => Sandblasting
[2] => Scappling
)
)
)