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
)
)
)
)
Related
$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
)
I am getting some data out of mysql into an array like this
array(
[0] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '555555'
),
[1] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '666666'
),
[2] = array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
[3] = array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
),
)
what i need to do is for each row, if the key code appears more than once, merge the rows into one but create a new array for the other_value like so
array(
[0] = array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => array(
[0] => '555555',
[1] => '666666'
)
),
[1] = array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
[2] = array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
),
)
What is the best way to achieve this?
I did think about looping over the each row and checking for existence of thtat key/value then do something if it exists.
#AdRock i hope you want to merge array in case of when 'code' will be same, if this is so then try below one:
<?php
$arr = array(
array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '555555'
),
array(
'code' => '123456',
'title' => 'something',
'price' => '2.00',
'other_value' => '666666'
),
array(
'code' => '234567',
'title' => 'something else',
'price' => '3.00',
'other_value' => '333333'
),
array(
'code' => '345678',
'title' => 'another thing',
'price' => '4.00',
'other_value' => NULL
)
);
echo "<pre>";
print_r($arr);// array before
$isExist = array();
foreach($arr as $key => $value){
if(in_array($value["code"], $isExist)){
$getKey = array_search($value["code"], $isExist);
$arr[$getKey]["other_value"] = array($arr[$getKey]["other_value"], $value["other_value"]);
unset($arr[$key]);
}
else{
$arr[$key] = $value;
}
$isExist[$key] = $value["code"];
}
echo "<pre>";
print_r(array_values($arr));// array after
?>
The approach that I would suggest is to loop through the array and store the value of code into a new array and store the entire result set into a new array. With each iteration, check whether the value is present or not in the code value stored array. And if value found then in that case, get the key of the code array and use the same key for the result array and store it inside the other_value. Hope it makes sense.
Looping over the array and creating a new array using the 'code' values as keys might be the simplest method. In that case you can check if the key allready exists.
$new_array=array();
foreach($array as $part){
$code_as_key = $part['code'];
//if code allready in the new array, just add a new value
if( isset($new_array[$code_as_key]) ){
$new_array[$code_as_key]['other_value'][] = $part['other_value'];
}
//else add the new code
else{
$new_array[$code_as_key]=$part;
}
}
//re-index the new array, starting from key 0
$new_array=array_values($new_array);
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);
}
I want to create dynamic multidimensional array using PHP
I need to generate array like this ...
$fetchMenu = array('page-1' => array(
'name' => 'first_page',
'label' => 'First page 2',
'route' => 'product_index',
'pages' => array(
array(
'name' => 'xxx',
'label' => 'xxx',
'route' => 'product_index',
), array(
'id' => 'permissions',
'label' => 'Permissions',
'title' => 'Permissions',
'route' => 'product_add',
'menu_tree_path' => 'default|system|roles_and_permission|permissions',
'display_in_menu' => true,
)
),
),
'page-2' => array(
'name' => 'second_page',
'label' => 'Second page 2',
'route' => 'product_index',
'pages' => array(),
),);
How can I do it ?
something like this?
$dynMenu = array();
$bookLength = 5; //function getBookLength() ??
for($i=0;$i<$bookLength;$i++){
$pageNumber = 'page'.$i;
$page = $i; // function getPages() ??
$dynMenu[$pageNumber] = array('name' => 'foo', 'label' => 'bar');
for($j=0;$j<$page;$j++){
$dynMenu[$pageNumber][$j] = array('name' => 'foo-ish', 'label' => 'bar-ish');
}
}
$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));