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
),
),
);
Related
I would like to replace keys in arrays, because I will move them on two indexes up.
Problem that I am facing is that those are containing same names which will not be ok, if i want to move them up.
This is how array looks like.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '3; top',
),
1 => array(
'Name' => 'level',
'value' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name' => 'display',
'value' => '1; left',
),
1 => array(
'Name' => 'level',
'value' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
and this is how I would like the output to be with changing keys in Name0, Name1, which are inside params.
$list = array(
'ind' => array(
'messagetype' => 'Alert',
'visibility' => 'Public',
'info' => array(
0 => array(
'urgency' => 'Urgent',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '3; top',
),
1 => array(
'Name1' => 'level',
'value1' => '1; blue',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GSSD154',
),
),
),
),
1 => array(
'messagetype' => 'Information',
'visibility' => 'Private',
'info' => array(
0 => array(
'urgency' => 'Minor',
'params' => array(
0 => array(
'Name0' => 'display',
'value0' => '1; left',
),
1 => array(
'Name1' => 'level',
'value1' => '1; red',
),
),
'area' => array(
'ard' => 'Bob',
'code' => array(
0 => array(
'Name' => 'Badge',
'value' => 'GBECS23',
),
),
),
),
),
),
),
),
);
I have tried with a lots of examples over this website, but could not find one to achieve this.
Code that I used from
How to replace key in multidimensional array and maintain order
function replaceKey($subject, $newKey, $oldKey) {
// if the value is not an array, then you have reached the deepest
// point of the branch, so return the value
if (!is_array($subject)) {
return $subject;
}
$newArray = array(); // empty array to hold copy of subject
foreach ($subject as $key => $value) {
// replace the key with the new key only if it is the old key
$key = ($key === $oldKey) ? $newKey : $key;
// add the value with the recursive call
$newArray[$key] = replaceKey($value, $newKey, $oldKey);
}
return $newArray;
}
$s = replaceKey($list, 'Name0', 'Name');
print "<PRE>";
print_r($s);
at the moment I get this output:
[0] => Array
(
[Name0] => display
[value] => 1; left
)
[1] => Array
(
[Name0] => level
[value] => 1; red
)
any help would be appreciated. regards
A very strange question, but why not?
The following function returns nothing (a procedure) and changes the array in-place using references but feel free to rewrite it as a "real" function (without references and with a return statement somewhere).
The idea consists to search for arrays, with numeric keys and at least 2 items, in which each item has the Name and value keys. In other words, this approach doesn't care about paths where the targets are supposed to be:
function replaceKeys(&$arr) {
foreach ($arr as &$v) {
if ( !is_array($v) )
continue;
$keys = array_keys($v);
if ( count($keys) < 2 ||
$keys !== array_flip($keys) ||
array_keys(array_merge(...$v)) !== ['Name', 'value'] ) {
replaceKeys($v);
continue;
}
foreach ($v as $k => &$item) {
$item = array_combine(["Name$k", "value$k"], $item);
}
}
}
replaceKeys($list);
print_r($list);
demo
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);
}
$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_spliceĀDocs takes an array of elements to insert. So the call should actually be
array_splice($custom, 1, 0, array($bread_elem));