$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);
Related
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);
}
Let's say i have an array like this:
$array = array(
0 =>
array (
'value' => '1' ,
'name' => 'dasdfa sadfa' ),
1=> Array (
'value' => 'adresa#gmail.com' ,
'name' => 'd2' ),
21 =>
array(
'value' => 'adresa#gmail.com' ,
'name' => 'name1`' ),
23 =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
24 =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
26 =>
array(
'value' => 'ricardo.ramos#amadeus.com',
'name' => '43414 Test01'),
27 =>
array(
'value' => 'sta3no213123ct3av#yahoo.com',
'name' => 'oct oct' )
);
I want to know if exists duplicated value in array with key 'value' I know how to do this if i want a specified value but general no. The result must be an array with no duplicated values(eg:
$array = array(
0 =>
array (
'value' => '1' ,
'name' => 'dasdfa sadfa' ),
1=> Array (
'value' => 'adresa#gmail.com' ,
'name' => 'd2' ),
23 =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
26 =>
array(
'value' => 'ricardo.ramos#amadeus.com',
'name' => '43414 Test01'),
27 =>
array(
'value' => 'sta3no213123ct3av#yahoo.com',
'name' => 'oct oct' )
);`
Please help me.
This is my try
function has_dupes($array){
$dupe_array = array();
foreach($array as $val){
if(++$dupe_array[$val] > 1){
return true;
}
}
return false;
}
Try this way:
$array = array(
'0' =>
array (
'value' => '1' ,
'name' => 'dasdfa sadfa' ),
'1'=> Array (
'value' => 'adresa#gmail.com' ,
'name' => 'd2' ),
'21' =>
array(
'value' => 'adresa#gmail.com' ,
'name' => 'name1`' ),
'23' =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
'24' =>
array(
'value' => 'popescu.catalina#gmail.com' ,
'name' => 'POPESCU CATALINA' ),
'26' =>
array(
'value' => 'ricardo.ramos#amadeus.com',
'name' => '43414 Test01'),
'27' =>
array(
'value' => 'sta3no213123ct3av#yahoo.com',
'name' => 'oct oct' )
);
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
$result = array_unique($array);
print_r($result);
And if you want to store all unique data in one array do it like this:
//declare $array
$unique_array = array();
foreach ($array as $key => $type) {
foreach($type as $vale => $name) {
if ($vale == 'value') {
//echo $name . '<br>';
array_push($unique_array, $name);
}
}
}
$result = array_unique($unique_array);
foreach ($result as $res) {
echo $res . '<br>';
}
Try this
$values = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($values as $key => $value)
{
if ( is_array($value) )
{
$values[$key] = $value;
}
}
print_r($values);
$unique_data = array(); // the result array
$duplicate_data = array();
$seen = array();
foreach ($array as $key => $arr) {
$value = $arr['value'];
if (!isset($seen[$value])) {
$seen[$value] = '';
$unique_data[$key] = $arr;
} else {
$duplicate_data[$key] = $arr; // optional
}
}
unset($seen); // optional in function scope
I have this php associative array which I created from a MySQL query. It looks like this;
array(
(int) 0 => array(
'items' => array(
'index_no' => '1'
),
(int) 0 => array(
'NumItems' => '2'
)
),
(int) 1 => array(
'items' => array(
'index_no' => '2'
),
(int) 0 => array(
'NumItems' => '3'
)
)
It looks unnecessarily complicated. I would like to simplify it to look something like this;
array(
(int) 0 => array(
'index_no' => '1',
'NumItems' => '2'
)
),
(int) 1 => array(
'index_no' => '2',
'NumItems' => '3'
)
)
How can this be done in php? I have been stuck on this problem for some time. I will post my answer if I have it. I would appreciate it if someone could give me some starting point. Thank you very much.
You can try this out:
$tempArray = array(
(int) 0 => array(
'items' => array(
'index_no' => '1'
),
(int) 0 => array(
'NumItems' => '2'
)
),
(int) 1 => array(
'items' => array(
'index_no' => '2'
),
(int) 0 => array(
'NumItems' => '3'
)
));
$newArray = array();
$i=0;
foreach($tempArray as $temp) {
$newArray[$i]['index_no'] = $temp['items']['index_no'];
$newArray[$i]['NumItems'] = $temp[0]['NumItems'];
$i++;
}
print "<pre>";
print_r($newArray);
try this
<?php $res=array(array('item'=>1,'number'=>5),array('item'=>2,'number'=>56));
$final_array =array();
$i=0;
foreach ($res as $val)
{
foreach($val as $key=>$val2)
{
$final_array[$i][$key] = $val2;
}$i++;
}
print_r($final_array);
?>
Here is solution for you.
$diffArray = array(
(int) 0 => array(
'items' => array(
'index_no' => '1'
),
(int) 0 => array(
'NumItems' => '2'
)
),
(int) 1 => array(
'items' => array(
'index_no' => '2'
),
(int) 0 => array(
'NumItems' => '3'
)
));
print_r($diffArray);
$getArray = array();
foreach ($diffArray as $simArray) {
$getArray['index_no'][] = $simArray['items']['index_no'];
$getArray['NumItems'][]= $simArray[0]['NumItems'];
}
print_r($getArray);
$newArray = array();
foreach ($array as $items) {
$temp = array('index_no' => $items['index_no']);
$temp = array_merge($temp, $items[0]);
$newArray[] = $temp;
}
it will add all the keys to the array under index - 0
I have this array in php:
$field_data = array(
'add_image' => array(
array(
'image_caption' => $caption,
'upload' => $attachname,
),
),
);
I need to append something to the array to get it to look like this:
$field_data = array(
'add_image' => array(
array(
'image_caption' => $caption,
'upload' => $attachname,
),
array(
'image_caption' => $caption,
'upload' => $attachname,
),
),
);
I tried array push but I was unable to get it to work properly. Any help would be appreciated.
You don't really need a function, just do it like this:
$field_data['add_image'][] = $to_append_array;
Try something like this:
$newEntry = array(
'image_caption' => $caption,
'upload' => $attachname,
);
$field_data['add_image'][] = $newEntry;
$field_data = array(
'add_image' => array(
1 = > array( //you may not realize,but this array has key 1 and value array()
'image_caption' => $caption,
'upload' => $attachname
),
),
);
add another add_image
$field_data['add_image'][] = array('image_caption' => $caption2, 'upload' => $attachname2);
add a image_title
$field_data['add_image'][1]['image_title'] = "Picture of House";
after both of those operations you end up with:
$field_data = array(
'add_image' => array(
1 = > array(
'image_caption' => $caption,
'upload' => $attachname,
'image_title' => "Picture of House"
),
2 = > array(
'image_caption' => $caption2,
'upload' => $attachname2
),
),
);
I have this array:
$all = array(
'meat' => Object(
'name' => 'meat',
'color' => 'red',
'class' => 'food'
),
'chicken' => Object(
'name' => 'chicken',
'color' => 'white',
'class' => 'food'
),
'apple' => Object(
'name' => 'apple',
'color' => 'green',
'class' => 'Fruit'
),
'blueberry' => Object(
'name' => 'blueberry',
'color' => 'blue',
'class' => 'Fruit'
)
);
and i want to Sort it and rebuild it to be like this:
$theright = array(
array(
'class' => 'food',
'menu' => array(
array(
'name' => 'meat',
'color' => 'red',
),
array(
'name' => 'chicken',
'color' => 'white',
)
)
),
array(
'class' => 'Fruit',
'menu' => array(
array(
'name' => 'apple',
'color' => 'green',
),
array(
'name' => 'blueberry',
'color' => 'blue',
)
)
)
);
I tried to Collect all classes in$all array then compare each value with $all array:
$classArray = array();
foreach($all as $key => $value) {
$classArray[$value->class] = array();
}
foreach($classArray as $key => $value) {
$theright[] = array('class' => $key, 'menu' => array());
}
this code get me this array:
$theright = array(
array(
'class' => 'food',
'menu' => array()
),
array(
'class' => 'Fruit',
'menu' => array()
)
);
and i stop here , how to complete it ?
You could just use the class as a key to group them together. Example:
$food = array();
// gather class
foreach($all as $item) {
if(!isset($food[$item->class])) {
$food[$item->class] = array(
'class' => $item->class,
'menu' => array(
array(
'name' => $item->name,
'color' => $item->name,
)
)
);
} else {
$food[$item->class]['menu'][] = array('name' => $item->name,'color' => $item->color,);
}
}
// simple reindex
$food = array_values($food);
There is no need for a second loop. This should do what you want.
$classMap = array();
foreach ($all as $item)
{
// check if class has been created in the class map
if ( ! array_key_exists($classMap, $item['class']))
{
$classMap[$item['class']] = array(
'class' => $item['class'],
'menu' => array()
);
}
$classMap[$item['class']]['menu'][] = array(
'name' => $item['name'],
'color' => $item['color']
);
}
Try with less loop counts (2)
$all = [];
function getSelectClassData(array &$all)
{
$finalArr = [];
while (count($all) > 1) {
$res = [];
$class = array_values($all)[0]->class;
$selectedDataArr = getSelectSimilerMenuData($all, $class);
$res['class'] = $class;
$res['menu'] = array_values($selectedDataArr);
$all = array_diff_key($all,array_flip(array_keys($selectedDataArr)));
$finalArr[] = $res;
}
return $finalArr;
}
function getSelectSimilerMenuData(array $all, $class)
{
return array_filter(
$all,
function ($e) use ($class) {
return $e->class == $class;
}
);
}
print_r(getSelectClassData($all));