When i am using shortcode it prints and error as
Notice: Array to string conversion in /home/nadiaz5/public_html/wp-includes/shortcodes.php on line 345
function ff_register_shortcode(){
$danger="";
$BoxLength=esc_attr( get_option('ff_list_boxes') );
$fedex_feeding= array();
$keys = array('name', 'id', 'max_weight', 'box_weight','length','width','height','inner_length','inner_width','inner_height','type');
for($f=0 ; $f<$BoxLength;$f++){
array_push($fedex_feeding, array(
'name' => get_option('ff_name_'.$f),
'id' => get_option('ff_id_'.$f),
'max_weight' => get_option('ff_max_weight_'.$f),
'box_weight' => get_option('ff_box_weight_'.$f),
'length' => get_option('ff_length_'.$f),
'width' => get_option('ff_width_'.$f),
'height' => get_option('ff_height_'.$f),
'inner_length' => get_option('ff_inner_length_'.$f),
'inner_width' => get_option('ff_inner_width_'.$f),
'inner_height' => get_option('ff_inner_height_'.$f),
'type' => get_option('ff_type_'.$f),
)
);
}
$danger = array();
$BoxLength=esc_attr( get_option('ff_list_boxes') );
for ($i=0; $i < $BoxLength; $i++) {
array_push($danger , $fedex_feeding[$i]);
}
return $danger;
}
add_shortcode('ff_arrays' , 'ff_register_shortcode');
Related
I'm trying to catch specific data from a weather forecast API request (JSON output). The request gives me data for the next 3 days but I only need the next day. Here a snippet of the output:
array (
'cod' => '200',
'message' => 0.00259999999999999988065102485279567190445959568023681640625,
'cnt' => 40,
'list' =>
array (
0 =>
array (
'dt' => 1526461200,
'main' =>
array (
'temp' => 292.93000000000000682121026329696178436279296875,
'temp_min' => 292.05000000000001136868377216160297393798828125,
'temp_max' => 292.93000000000000682121026329696178436279296875,
'pressure' => 1019.259999999999990905052982270717620849609375,
'sea_level' => 1029.170000000000072759576141834259033203125,
'grnd_level' => 1019.259999999999990905052982270717620849609375,
'humidity' => 71,
'temp_kf' => 0.88000000000000000444089209850062616169452667236328125,
),
'weather' =>
array (
0 =>
array (
'id' => 802,
'main' => 'Clouds',
'description' => 'scattered clouds',
'icon' => '03d',
),
),
'clouds' =>
array (
'all' => 36,
),
'wind' =>
array (
'speed' => 4.20000000000000017763568394002504646778106689453125,
'deg' => 37.00240000000000151203494169749319553375244140625,
),
'sys' =>
array (
'pod' => 'd',
),
'dt_txt' => '2018-05-16 09:00:00',
),
1 =>
array (
'dt' => 1526472000,
'main' =>
array (
'temp' => 293.6100000000000136424205265939235687255859375,
'temp_min' => 292.95800000000002683009370230138301849365234375,
'temp_max' => 293.6100000000000136424205265939235687255859375,
'pressure' => 1019.799999999999954525264911353588104248046875,
'sea_level' => 1029.65000000000009094947017729282379150390625,
'grnd_level' => 1019.799999999999954525264911353588104248046875,
'humidity' => 66,
'temp_kf' => 0.66000000000000003108624468950438313186168670654296875,
),
'weather' =>
array (
0 =>
array (
'id' => 803,
'main' => 'Clouds',
'description' => 'broken clouds',
'icon' => '04d',
),
),
'clouds' =>
array (
'all' => 56,
),
'wind' =>
array (
'speed' => 5.79999999999999982236431605997495353221893310546875,
'deg' => 38.0009000000000014551915228366851806640625,
),
'sys' =>
array (
'pod' => 'd',
),
'dt_txt' => '2018-05-16 12:00:00',
)
I'm trying to get the 'all' values where 'dt_txt' starts from 0am the next day to 0pam the day after.
For now I'm using the following php code without checking for dt_txt:
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?q=D%C3%BClmen,de&mode=json";
$response = file_get_contents($url);
$obj = json_decode($response);
$t = 0;
$regen = 0;
$regendiv = 0;
for($i=0; $i < 10; $i++) {
$fg = $obj->list[$i]->clouds->all;
$regen += $fg;
}
echo $regen;
?>
My code requires me to run the request at a specific time (like close to midnight) in order to catch the 'all' values for the next day. Is there any way to check for 'dt_text' = the next day?
My Idea would be something like this:
for($i=0; $i < 10; $i++) {
$fg = $obj->list[$i]->clouds->all;
if (strpos($obj->list[$i]->dt_txt), $date){
$regen += $fg;
}
**//But how do I get $date to be the following day**
}
echo $regen;
?>
I was able to solve it this way:
<?php
$nextWeek = time() + (24 * 60 * 60);
$morgen = date('Y-m-d', $nextWeek);
$url = "http://api.openweathermap.org/data/2.5/forecast?q=D%C3%BClmen,de&mode=json";
$response = file_get_contents($url);
$obj = json_decode($response);
$quote = 0;
for($i=0; $i < 14; $i++) {
if (strpos($obj->list[$i]->dt_txt, $morgen) !== false) {
$fg = $obj->list[$i]->clouds->all;
if($fg > 80) { $t = 1; }
$regen += $fg;
$quote++;
}
}
$regendiv = $regen / $quote;
?>
Probably not the prettiest way but it gets the job done.
My first array like this :
$photoList = array(
array(
'id' => 1,
'name' => 'chelsea.jpg'
),
array(
'id' => 2,
'name' => 'mu.jpg'
),
array(
'id' => 3,
'name' => 'city.jpg'
)
);
My second array like this :
photo = array('cover1'=>'liverpool.jpg', 'cover2'=>'city.jpg');
My code like this :
$photo = array_filter($photo, function($item) use ($photoList) {
return in_array($item, array_column($photoList, 'name')) ;
});
if (empty($photo))
$photo=null;
If I run : echo '<pre>';print_r($photo);echo '</pre>';, the result like this :
Array
(
[cover2] => city.jpg
)
I want the result like this :
Array
(
[cover1] => city.jpg
)
So if there is no cover1, the cover2 changed to be cover1
How can I do it?
You could re-index the keys like this :
$photoList = array(
array(
'id' => 1,
'name' => 'chelsea.jpg'
),
array(
'id' => 2,
'name' => 'mu.jpg'
),
array(
'id' => 3,
'name' => 'city.jpg'
)
);
$photo = array('cover1'=>'liverpool.jpg', 'cover2'=>'city.jpg');
$photo = array_filter($photo, function($item) use ($photoList) {
return in_array($item, array_column($photoList, 'name')) ;
});
if (empty($photo)){
$photo = null;
} else {
$idx = 1 ;
foreach ($photo as $key => $value) {
unset($photo[$key]);
$photo['cover'.$idx++] = $value;
}
}
print_r($photo);
Outputs :
Array
(
[cover1] => city.jpg
)
I'm not sure about what you really need. Assuming you want to do that recursively, I hope this helps:
$label = 'cover';
$index = 1;
$a_keys = array_keys($photo);
$new_photo = [];
foreach($a_keys AS $k=>$v){
$new_photo[$label.$index] = $photo[$k];
$index++;
}
$photo = $new_photo;
It will rewrite your array with wanted naming style.
when searching an element in a nested array, could i get back it's 1st level nesting index.
<?php
static $cnt = 0;
$name = 'victor';
$coll = array(
'dep1' => array(
'fy' => array('john', 'johnny', 'victor'),
'sy' => array('david', 'arthur'),
'ty' => array('sam', 'joe', 'victor')
),
'dep2' => array(
'fy' => array('natalie', 'linda', 'molly'),
'sy' => array('katie', 'helen', 'sam', 'ravi', 'vipul'),
'ty' => array('sharon', 'julia', 'maddy')
)
);
function recursive_search(&$v, $k, $search_query){
global $cnt;
if($v == $search_query){
/* i want the sub array index to be returned */
}
}
?>
i.e to say, if i'am searching 'victor', i would like to have 'dep1' as the return value.
Could anyone help ??
Try:
$name = 'victor';
$coll = array(
'dep1' => array(
'fy' => array('john', 'johnny', 'victor'),
'sy' => array('david', 'arthur'),
'ty' => array('sam', 'joe', 'victor')
),
'dep2' => array(
'fy' => array('natalie', 'linda', 'molly'),
'sy' => array('katie', 'helen', 'sam', 'ravi', 'vipul'),
'ty' => array('sharon', 'julia', 'maddy')
)
);
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($coll), RecursiveIteratorIterator::SELF_FIRST);
/* These will be used to keep a record of the
current parent element it's accessing the childs of */
$parent_index = 0;
$parent = '';
$parent_keys = array_keys($coll); //getting the first level keys like dep1,dep2
$size = sizeof($parent_keys);
$flag=0; //to check if value has been found
foreach ($iter as $k=>$val) {
//if dep1 matches, record it until it shifts to dep2
if($k === $parent_keys[$parent_index]){
$parent = $k;
//making sure the counter is not incremented
//more than the number of elements present
($parent_index<$size-1)?$parent_index++:'';
}
if ($val == $name) {
//if the value is found, set flag and break the loop
$flag = 1;
break;
}
}
($flag==0)?$parent='':''; //this means the search string could not be found
echo 'Key = '.$parent;
Demo
This works , but I don't know if you are ok with this...
<?php
$name = 'linda';
$col1=array ( 'dep1' => array ( 'fy' => array ( 0 => 'john', 1 => 'johnny', 2 => 'victor', ), 'sy' => array ( 0 => 'david', 1 => 'arthur', ), 'ty' => array ( 0 => 'sam', 1 => 'joe', 2 => 'victor', ), ), 'dep2' => array ( 'fy' => array ( 0 => 'natalie', 1 => 'linda', 2 => 'molly', ), 'sy' => array ( 0 => 'katie', 1 => 'helen', 2 => 'sam', 3 => 'ravi', 4 => 'vipul', ), 'ty' => array ( 0 => 'sharon', 1 => 'julia', 2 => 'maddy', ), ), );
foreach($col2 as $k=>$arr)
{
foreach($arr as $k1=>$arr2)
{
if(in_array($name,$arr2))
{
echo $k;
break;
}
}
}
OUTPUT :
dept2
Demo
I have an array:
$settings = array(
'name' => array(
0 => 'Large Pouch',
1 => 'XL Pouch'
),
'size' => array(
0 => '9x14',
1 => '12x18'
),
'weight' => array(
0 => '10',
1 => '20'
),
'metro_manila_price' => array(
0 => '59',
1 => '79'
),
'luzvimin_price' => array(
0 => '89',
1 => '139'
)
);
I wanted to put the values from that array to one array. $shipping_options with format of
for example:
$shipping_options = array(
'0' => 'Large Pouch 9x14 - $59',
'1' => 'XL Pouch 12x18 - $79'
);
How to program this?
You could write a loop:
$shipping_options = array();
foreach ($settings['name'] as $key => $value) {
$value = sprintf('%s(%s) - $%s',
$value,
$settings['size'][$key],
$settings['metro_manila_price'][$key]);
$shipping_options[$key] = $value;
}
try this one
echo "<pre>";
$size = count($settings['name']);
$shipping_options = array();
for($i=0; $i<$size; $i++)
{
$shipping_options[$i] = $settings['name'][$i]."(".$settings['size'][$i].") - $".$settings['metro_manila_price'][$i];
}
print_r($shipping_options);
You can try this
foreach ($settings['name'] as $key => $value) {
$shipping_options[$key] = $settings['name'][$key] . " " . $settings['size'][$key] . " - $" . $settings['metro_manila_price'][$key];
}
public function getCheckoutForm(){
$arr = array(
'cmd' => '_cart',
'business' => 'some#mail',
'no_shipping' => '1',
'upload' => '1',
'return' => 'url',
'cancel_return' => 'url1',
'no_note' => '1',
'currency_code' => 'url2',
'bn' => 'PP-BuyNowBF');
$cpt=1;
foreach($this->items as $item){
$arr1[] = array(
'item_number_'.$cpt.'' => $item['item_id'],
'item_name_'.$cpt.'' => $item['item_name'],
'quantity_'.$cpt.'' => $item['item_q'],
'amount_'.$cpt.'' => $item['item_price']
);
$cpt++;
}
return array_merge($arr,$arr1[0],$arr1[1]);
}
This returns array like that:
Array
(
[cmd] => _cart
[business] => some#mail
[no_shipping] => 1
[upload] => 1
[return] => url1
[cancel_return] =>url2
[no_note] => 1
[currency_code] => EUR
[bn] => PP-BuyNowBF
[item_number_1] => 28
[item_name_1] => item_name_1
[quantity_1] => 1
[amount_1] => 5
[item_number_2] => 27
[item_name_2] => item_name_2
[quantity_2] => 1
[amount_2] => 30
)
The problem is that in return $arr1[0] and $arr1[1] are hardcoded. And if in loop i have more than 2 arrays, lets say 0,1,2,3 ans so on, this code won't work. Any idea? Maybe my logic is compleatly wrong...
There's no need to create arrays in your loop - just add new keys directly to the first array:
public function getCheckoutForm(){
$arr = array(
'cmd' => '_cart',
'business' => 'some#mail',
'no_shipping' => '1',
'upload' => '1',
'return' => 'url',
'cancel_return' => 'url1',
'no_note' => '1',
'currency_code' => 'url2',
'bn' => 'PP-BuyNowBF'
);
$cpt=1;
foreach($this->items as $item){
$arr['item_number_'.$cpt] = $item['item_id'];
$arr['item_name_'.$cpt] = $item['item_name'];
$arr['quantity_'.$cpt] = $item['item_q'];
$arr['amount_'.$cpt] = $item['item_price'];
$cpt++;
}
return $arr;
}
I would probably do something like
$count = count($arr1);
for($i=0;$i<$count;$i++){
$arr = array_merge($arr,$arr1[$i]);
}
return $arr;
I hope, I understood, what you mean ^^
foreach ($i = 0, $n = count($arr1); $i < $n; $i++) {
$arr = array_merge($arr, $arr1[$i]);
}
return $arr;
You could do the merge in every iteration:
foreach($this->items as $item){
$temp_arr = array(
'item_number_'.$cpt.'' => $item['item_id'],
'item_name_'.$cpt.'' => $item['item_name'],
'quantity_'.$cpt.'' => $item['item_q'],
'amount_'.$cpt.'' => $item['item_price']
);
$arr = array_merge($arr,$temp_arr)
$cpt++;
}
which has the advantage that you could possibly get $temp_arr from a function,
or just add all the elements to one array:
foreach($this->items as $item){
$arr['item_number_'.$cpt.''] => $item['item_id'];
$arr['item_name_'.$cpt.''] => $item['item_name'];
$arr['quantity_'.$cpt.''] => $item['item_q'];
$arr['amount_'.$cpt.''] => $item['item_price'];
$cpt++;
}
do this
$count = count($data);
$sum = 1;
$arr = [];
for($i=0;$i<$count;$i++){
$temp = $arr;
if($i == $count - 1){
$sum = 0;
}
$arr = array_merge($temp,$data[$i + $sum]);
}
return $arr;