i have a function with a dynamic array.
function doIt($accountid,$targeting){
$post_url= "https://url".$accountid."/";
$fields = array(
'name' => "test",
'status'=> "PAUSED",
'targeting' => array(
$targeting
),
);
$curlreturn=curl($post_url,$fields);
};
And i want to build the array "$fields" dynamically within a foreach loop. Like that:
$accountid="57865";
$targeting=array(
"'device_platforms' => array('desktop'),'interests' => array(array('id' => '435345','name' => 'test')),",
"'device_platforms' => array('mobile'), 'interests' => array(array('id' => '345345','name' => 'test2')),",
);
foreach ($targeting as $i => $value) {
doit($accountid,$value);
}
The Problem is, that the array within the function will not be correctly filled. If i output the array in the function i get something like:
....[0] => array('device_platforms' => array('desktop'),'custom_audiences'=> ['id' => '356346']), )
The beginning [0] should be the problem. Any ideas what im doing wrong?
Hope this will help you out. The problem was the way you are defining $targeting array. You can't have multiple keys with same name
Change 1:
$targeting = array(
array(
'device_platforms' => array('desktop'),
'interests' => array(
array('id' => '435345',
'name' => 'test')),
),
array(
'device_platforms' => array('mobile'),
'interests' => array(
array('id' => '345345',
'name' => 'test2'))
)
);
Change 2:
$fields = array(
'name' => "test",
'status' => "PAUSED",
'targeting' => $targeting //removed array
);
Try this code snippet here this will just print postfields
<?php
ini_set('display_errors', 1);
function doIt($accountid, $targeting)
{
$post_url = "https://url" . $accountid . "/";
$fields = array(
'name' => "test",
'status' => "PAUSED",
'targeting' => $targeting
);
print_r($fields);
}
$accountid = "57865";
$targeting = array(
array(
'device_platforms' => array('desktop'),
'interests' => array(
array('id' => '435345',
'name' => 'test')),
),
array(
'device_platforms' => array('mobile'),
'interests' => array(
array('id' => '345345',
'name' => 'test2'))
)
);
foreach ($targeting as $i => $value)
{
doit($accountid, $value);
}
Related
actually i am tring to get data from url which i already got but i can't able to insert data into the array please check values are dummy1 dummy2
Array ( [id] => 491 [headings] => ‘సౌందర్య లహరి’ ఎలా ఉంది అంటే..? [content] => శ్రీ వాసు దర్శకత్వం లో బెల... [userPic] => 682473.jpg ) JSON sent: {"app_id":"35fi6c27-5f39-4s9f-94db-79bf123g0f9","included_segments":["All"],"data":{"foo":"bar"},"contents":{"en":"dummy1"},"headings":{"en":"dummy2"},"url":"http:\/\/www.gggg.in\/view.php?id=[id]","chrome_web_image":"http:\/\/www.ggg.in\/admin\/user_images\/[userPic]"} JSON received: {"allresponses":"{\"id\":\"f66a03a4-1b17-8tf3-93ed-f3ad6rt7cdb9\",\"recipients\":6}"}
the above data got from url
http://www.ggg.in/gistfile1.php?id=491&headings=%E2%80%98%E0%B0%B8%E0%B1%8C%E0%B0%82%E0%B0%A6%E0%B0%B0%E0%B1%8D%E0%B0%AF%20%E0%B0%B2%E0%B0%B9%E0%B0%B0%E0%B0%BF%E2%80%99%20%E0%B0%8E%E0%B0%B2%E0%B0%BE%20%E0%B0%89%E0%B0%82%E0%B0%A6%E0%B0%BF%20%E0%B0%85%E0%B0%82%E0%B0%9F%E0%B1%87..?&content=%20%E0%B0%B6%E0%B1%8D%E0%B0%B0%E0%B1%80%20%E0%B0%B5%E0%B0%BE%E0%B0%B8%E0%B1%81%20%E0%B0%A6%E0%B0%B0%E0%B1%8D%E0%B0%B6%E0%B0%95%E0%B0%A4%E0%B1%8D%E0%B0%B5%E0%B0%82%20%E0%B0%B2%E0%B1%8B%20%E0%B0%AC%E0%B1%86%E0%B0%B2...&userPic=682473.jpg
and i need to insert the values in this function
function sendMessage() {
$content = array(
"en" => 'dummy1'
);
$headings = array(
"en" => 'dummy2'
);
$hashes_array = array();
$fields = array(
'app_id' => "35fi6c27-5f39-4s9f-94db-79bf123g0f9",
'included_segments' => array(
'All'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'headings' => $headings,
'url' => 'http://www.gggg.in/view.php?id=[id]',
'chrome_web_image' => 'http://www.ggg.in/admin/user_images/[userPic]',
);
OneSignal does not support substituting variable data directly into API calls. However, you can achieve this with Tag and Variable Substitution: https://documentation.onesignal.com/docs/personalization
this is the solutions for this Question i asked about
actually i have a varable passing throug url and i need them to insert into function
$array = [
'id' => 498,
'value2' => 'యూట్యూబ్లో “భరత్ అనే నేను” అన్ కట్ సీన్లు..!',
'value1' => 'కొరటాల శివ దర్శకత్వం లో సూ...',
'value3' => '525722.jpg'
];
sendMessage($array);
function sendMessage($array) {
$content = array(
"en" => $array['value1']
);
$headings = array(
"en" => $array['value2']
);
$hashes_array = array();
$fields = array(
'app_id' => "31pe2347-5i39-4y9f-2222-79b5875f00f9",
'included_segments' => array(
'All'
),
'data' => array(
"foo" => "bar"
),
'contents' => $content,
'headings' => $headings,
'url' => 'http://w...content-available-to-author-only...9.in/view.php?id=' . $array['id'],
'chrome_web_image' => 'http://w...content-available-to-author-only...9.in/admin/user_images/' . $array['value3'],
);
print_r($fields);
}
$array_subjected_to_search =array(
array(
'name' => 'flash',
'type' => 'hero'
),
array(
'name' => 'zoom',
'type' => 'villian'
),
array(
'name' => 'snart',
'type' => 'antihero'
),
array(
'name' => 'flash',
'type' => 'camera'
)
);
$key = array_search('flash', array_column($array_subjected_to_search, 'name'));
var_dump($array_subjected_to_search[$key]);
This works fine, but is there a way to search using multiple values: eg. get key where name='flash' && type='camera' ?
is there a way to search using multiple values: eg. get key where
name='flash' && type='camera' ?
Simply with array_keys function:
$result_key = array_keys($array_subjected_to_search, [ 'type' => 'camera','name' => 'flash']);
print_r($result_key);
The output:
Array
(
[0] => 3
)
The array_search function accepts an array as parameters the following will work for the use case you provided.
$array_subjected_to_search =array(
array(
'name' => 'flash',
'type' => 'hero'
),
array(
'name' => 'zoom',
'type' => 'villian'
),
array(
'name' => 'snart',
'type' => 'antihero'
),
array(
'name' => 'flash',
'type' => 'camera'
)
);
$compare = array(
'name'=>'flash',
'type'=>'camera'
);
$key = array_search($compare, $haystack);
var_dump($haystack[$key]);
Note: your current search will not function correctly it will always return the zero index because the array_search returns 0 or false.
$key = array_search('flash', array_column($array_subjected_to_search, 'name'));
var_dump($array_subjected_to_search[$key]);
I think I would just make my own function using a loop that will just retrieve the array I want based on either one or two parameters.
function getValueMatch($array, $val1, $val2 = false, $type = 'name')
{
foreach($array as $key => $row) {
# See note below, but it might be handy to have a reversible key name
if($row[$type] == $val1) {
if($val2) {
# You can put a changeable key name to reverse-find
# It might be helpful to search for the other key first
# at some point, best to keep your options open!
$altVar = ($type == 'name')? 'type' : 'name';
if($row[$altVar] == $val2)
return $row;
}
else
return $row;
}
}
}
$array =array(
array(
'name' => 'flash',
'type' => 'hero'
),
array(
'name' => 'zoom',
'type' => 'villian'
),
array(
'name' => 'snart',
'type' => 'antihero'
),
array(
'name' => 'flash',
'type' => 'camera'
)
);
print_r(getValueMatch($array,'flash','camera'));
Gives you:
Array
(
[name] => flash
[type] => camera
)
Example of reverse match (type instead of name):
print_r(getValueMatch($array,'antihero',false,'type'));
Gives you:
Array
(
[name] => snart
[type] => antihero
)
$test = array('hola'=>'mundo','salami'=>'frito');
$data = array(
'gift_promoted' => '',
'questions' => array(
0 => array(
'extra' => array()
)
),
'quiz' => array(
'extra' => array(),
$test
)
);
print json_encode($data);
Actual result:
{"gift_promoted":"","questions":[{"extra":[]}],"quiz":{"extra":[],"0":{"hola":"mundo","salami":"frito"}}}
annotated screenshot
I need this is:
{"gift_promoted":"","questions":[{"extra":[]}],"quiz":{"extra":[],"hola":"mundo","salami":"frito"}}
a little dirty:
$test = array('hola'=>'mundo','salami'=>'frito'); $data = array(
'gift_promoted' => '',
'questions' => array(
0 => array(
'extra' => array()
)
),
'quiz' => array(
'extra' => array(),
//$test --> remove this.
) );
foreach($test as $key => $value) { $data['quiz'][$key]=$value; }
print json_encode($data);
I try to code a API Connection to Shopware (Shop CMS). Now I want to get the Data from a Database and fill the array with it.
So the Original Example Code from the
$updateArticle = array(
'configuratorSet' => array(
'groups' => array(
array(
'name' => 'Size',
'options' => array(
array('name' => 'S'),
array('name' => 'M'),
array('name' => 'L'),
array('name' => 'XL'),
array('name' => 'XXL'),
)
),
Now i want to replace this
array('name' => 'S'),
array('name' => 'M'),
array('name' => 'L'),
array('name' => 'XL'),
array('name' => 'XXL'),
with a while so like this (but this doesn't work)
$updateArticle = array(
'configuratorSet' => array(
'groups' => array(
array(
'name' => 'Farbe',
'options' => array(
if ($resultatfarben = $db->query('SELECT * FROM cache_article WHERE artikelnummer = '.$herstellnummer.' GROUP BY farben')) {
while($datenfarben = $resultatfarben->fetch_object() ){
array('name' => ''.$datenfarben->farbe.''),
}
}
)
),
I can't insert a while but I like the same result like the original example in the front of the post but with a mysql while and not write the code by my self.
First we need to create the options array with the results of the database. Then, assign this array to the 'options' property of your $updateArticle array
$options = array(); // Initialize a new array
$resultatfarben = $db->query('SELECT * FROM cache_article WHERE artikelnummer = '.$herstellnummer.' GROUP BY farben');
while($datenfarben = $resultatfarben->fetch_object() ){
$ret = array('name' => $datenfarben->farbe);
$options[] = $ret; // Push each db result ($ret array) in the options array
}
$updateArticle = array(
'configuratorSet' => array(
'groups' => array(
array(
'name' => 'Farbe',
'options' => $options // Assign the $options array created before
)
)
)
)
$custom = Array(
Array(
'name' => $name1,
'url' => $url1
),
Array(
'name' => $name_a,
'url' => $url_a
)
);
I am attempting to splice the array with the following:
$bread_elem = array('name' => 'Golf', 'url' => $slug . $parent_slug);
array_splice($custom, 1, 0, $bread_elem);
I want my array to become the following, with the value of $sale_bread_elem inserted into position one within the array. I can't see what I am doing wrong.
$custom = Array(
Array(
'name' => $name1,
'url' => $url1
),
Array(
'name' => 'Golf',
'url' => $slug . $parent_slug
),
Array(
'name' => $name_a,
'url' => $url_a
)
);
array_spliceDocs takes an array of elements to insert. So the call should actually be
array_splice($custom, 1, 0, array($bread_elem));