My multidimensional associative array :
$search_cookies = array(
"type_catalog" => $type_array,
"size_catalog" => $size_array,
"color_catalog" => $color_array,
);
I need to do that :
$search_cookies = array(
if(isset($type_array){
"type_catalog" => $type_array,
}
elseif(isset($size_array)){
"size_catalog" => $size_array,
}
elseif(isset($color_array)){
"color_catalog" => $color_array,
}
);
Here is the entire code if you think it must be some other way :
$first_array = array('t-1', 's-32', 't-2', 's-36');
function removeLetters($row){
return preg_replace("/[^0-9,.]/", "", $row);
}
foreach($first_array as $row){
$exp_key = explode('-', $row);
if($exp_key[0] == 't'){
$type_array[] = removeLetters($row);
}
if($exp_key[0] == 's'){
$size_array[] = removeLetters($row);
}
if($exp_key[0] == 'c'){
$color_array[] = removeLetters($row);
}
}
$search_cookies = array(
"type_catalog" => $type_array,
"size_catalog" => $size_array,
"color_catalog" => $color_array,
);
you can try this:
$search_cookies = array();
if(isset($type_array)){
$search_cookies["type_catalog"] = $type_array;
}
if(isset($size_array)){
$search_cookies["size_catalog"] = $size_array;
}
if(isset($color_array)){
$search_cookies["color_catalog"] = $color_array;
}
Related
I want to write function which receive me path to top element, but I can't figure out how it should work..
My example data:
$data = array(
3546456 => array(
5345345,
12312312,
56456546,
),
12312312 => array(
34534534,
5675675,
8678678,
),
567978 => array(
234,
756756,
8678678,
),
);
//I have function to return parent.
$parents = getParents(8678678); // eg. $object_id = 8678678 - return [12312312, 567978] , $object_id = 12312312 - return [3546456]
// and my recursion function I tried.
function getObjectPath($object_id) {
if ($object_id == null) {
return [];
}
$parents = getObjectParents($object_id);
foreach ($parents as $parent) {
return array($object_id => getObjectPath($parent->nid));
}
}
It doesn't work way I need, on the return in getObjectPath(8678678) I'd like to have return array like that:
array(
3546456 => array(
12312312 => array(
8678678
)
),
567978 => array(
8678678
)
);
I think you almost got it, need to have some other check before returning
$data = array(
3546456 => array(
5345345,
12312312,
56456546,
),
12312312 => array(
34534534,
5675675,
8678678,
),
567978 => array(
234,
756756,
8678678,
),
);
//I have function to return parent.
$parents = getObjectPath(8678678, $data); // eg. $object_id = 8678678 - return [12312312, 567978] , $object_id = 12312312 - return [3546456]
echo "<pre>";
print_r($parents);
// and my recursion function I tried.
function getParents($object_id, $data) {
$return = [];
foreach($data as $key => $value) {
if(in_array($object_id, $value)) {
$return[] = $key;
}
}
return $return;
}
// and my recursion function I tried.
function getObjectPath($object_id, $data) {
$return = [];
$parents = getParents($object_id, $data);
foreach($parents as $parent) {
$temp = getObjectPath($parent, $data);
if(!empty($temp)) {
$return[key($temp)][$parent] = $object_id;
} else {
$return[$parent] = $object_id;
}
}
return $return;
}
I have the following array:
$array = array(
'item-img-list' => array(
array(
'image-type' => 1,
'image-url' => 'http://img07.allegroimg.pl/...'
),
array(
'image-type' => 2,
'image-url' => 'http://img07.allegroimg.pl/...'
),
array(
'image-type' => 3,
'image-url' => 'http://img07.allegroimg.pl/...'
)
)
)
How to get first 'image-url' value where 'image-type' = '2'?
I'm trying do that by this code but nothing:
$zdjecia = $item['item-img-list'];
foreach($zdjecia as $zdjecie) {
foreach($zdjecie as $key=>$value) {
if($key == "image-type" && $value == "2") {
$zdjecie_aukcji = $key['image-url'];
}
}
}
Thank you for any kind of help!
Works!
$searchKey = 2;
foreach($zdjecia as $zdjecie) {
if (**$zdjecie->{'image-type'}** == $searchKey){
$zdjecie_aukcji = **$zdjecie->{'image-url'}**;
break;
}
}
$zdjecia = $item['item-img-list'];
$searchKey = 2;
foreach($zdjecia as $zdjecie) {
if ($zdjecie['image-type'] == $searchKey)
$zdjecie_aukcji = $zdjecie['image-url'];
break;
}
}
or (PHP >=5.5)
$zdjecia = $item['item-img-list'];
$searchKey = 2;
$results = array_column(
$zdjecia,
'image-url',
'image-type'
);
$zdjecie_aukcji = $results[$searchKey];
foreach($zdjecia as $zdjecie) {
foreach($zdjecie as $key=>$value) {
if($key == "image-type" && $value == "2") {
$zdjecie_aukcji = $zdjecie['image-url'];
}
}
}
A suggestions with custom function that I use for my project to find a value by key in multidimensional array:
function array_search_multi($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, array_search_multi($subarray, $key, $value));
}
return $results;
}
Usage:
$results = array_search_multi($array, 'image-type', '2');
echo $results[0]['image-url'];
Output:
http://img07.allegroimg.pl/...
Working example
why not simply this:-
$array = array(
'item-img-list' => array(
array(
'image-type' => 1,
'image-url' => 'http://img07.allegroimg.pl/...'
),
array(
'image-type' => 2,
'image-url' => 'http://img07.allegroimg.pl/...'
),
array(
'image-type' => 3,
'image-url' => 'http://img07.allegroimg.pl/...'
)
)
);
$newArray = array();
foreach($array['item-img-list'] as $k=>$v){
$newArray[$v['image-type']] = $v['image-url'];
}
Output :-
Array
(
[1] => http://img07.allegroimg.pl/...
[2] => http://img07.allegroimg.pl/...
[3] => http://img07.allegroimg.pl/...
)
or
echo $newArray[2];
you can also check key like this:
if (array_key_exists(2, $newArray)) {
// Do whatever you want
}
Working Demo
Add break; right after
$zdjecie_aukcji = $key['image-url'];
modify to:
$zdjecia = $array['item-img-list'];
foreach($zdjecia as $zdjecie) {
if($zdjecie['image-type'] == '2') {
$zdjecie_aukcji = $zdjecie['image-url'];
}
}
I searched in Google and consulted the PHP documentation, but couldn't figure out how the following code works:
$some='name=Licensing Module;nextduedate=2013-04-10;status=Active|name=Test Addon;nextduedate=2013-04-11;status=Active';
function getActiveAddons($somet) {
$addons = array( );
foreach ($somet as $addon) {
if ($addon['status'] == 'Active') {
$addons[] = $addon['name'];
continue;
}
}
return $addons;
}
echo (count( getActiveAddons( $some ) ) ? implode( '<br />', getActiveAddons( $some ) ) : 'None');
The code always echo's None.
Please help me in this.
I don't know where you got this code from but you've initialized $some the wrong way. It is expected as an array like this:
$some = array(
array(
'name' => 'Licensing Module',
'nextduedate' => '2013-04-10',
'status' => 'Active'
),
array(
'name' => 'Test Addon'
'nextduedate' => '2013-04-11',
'status' => 'Active'
)
);
I guess the article you've read is expecting you to parse the original string into this format.
You can achieve this like this:
$string = 'name=Licensing Module;nextduedate=2013-04-10;status=Active|name=Test Addon;nextduedate=2013-04-11;status=Active';
$result = array();
foreach(explode('|', $string) as $record) {
$item = array();
foreach(explode(';', $record) as $column) {
$keyval = explode('=', $column);
$item[$keyval[0]] = $keyval[1];
}
$result[]= $item;
}
// now call your function
getActiveAddons($result);
$some is not an array so foreach will not operate on it. You need to do something like
$some = array(
array(
'name' => 'Licensing Module',
'nextduedate' => '2013-04-10',
'status' => 'Active'
),
array(
'name' => 'Test Addon',
'nextduedate' => '2013-04-11',
'status'=> 'Active'
)
);
This will create a multidimensional array that you can loop through.
function getActiveAddons($somet) {
$addons = array( );
foreach ($somet as $addon) {
foreach($addon as $key => $value) {
if ($key == 'status' && $value == 'Active') {
$addons[] = $addon['name'];
continue;
}
}
}
return $addons;
}
First, your $some variable is just a string. You could parse the string into an array using explode(), but it's easier to just start as an array:
$some = array(
array(
"name" => "Licensing Module",
"nextduedate" => "2013-04-10",
"status" => "Active",
),
array(
"name" => "Test Addon",
"nextduedate" => "2013-04-11",
"status" => "Active",
)
);
Now, for your function, you are on the right track, but I'll just clean it up:
function getActiveAddons($somet) {
if (!is_array($somet)) {
return false;
}
$addons = array();
foreach ($somet as $addon) {
if ($addon['status'] == 'Active') {
$addons[] = $addon['name'];
}
}
if (count($addons) > 0) {
return $addons;
}
return false;
}
And finally your output (you were calling the function twice):
$result = getActiveAddons($some);
if ($result === false) {
echo "No active addons!";
}
else {
echo implode("<br />", $result);
}
I have an array montant
$montant = array(
"EUR_credit"=>10, "USD_credit"=>20, "EUR_debit"=>30, "JPY_debit"=>20
);
I am trying
$total = array();
foreach ($montant as $key=>$value){
$check_key = substr($key, 0,3);
if(!isset($check_key)){
}
}
echo '<pre>';
print_r($total);
echo '</pre>';
$total = array('EUR'=>array('credit'=10,'debit'=>30),
'USD'=>array('credit'=20,'debit'=>NULL),
'JPY'=>array('credit'=NULL,'debit'=>20),
)
$total = array();
foreach ($montant as $type => $value) {
list($currency, $type) = explode('_', $type);
$total[$currency][$type] = $value;
$total[$currency] += array('credit' => null, 'debit' => null);
}
You have some errors mate inside that $total array definition, corrected:
$total = array
(
'EUR' => array('credit'=>10,'debit'=>30),
'USD' => array('credit'=>20,'debit'=>NULL),
'JPY' => array('credit'=>NULL,'debit'=>20),
);
This is sort of a general implementation question. If I have an arbitrarily deep array, and I do not know before hand what the keys will be, what is the best way to access the values at specific paths of the associative array? For example, given the array:
array(
'great-grandparent' = array(
'grandparent' = array(
'parent' = array(
'child' = 'value';
),
'parent2' = 'value';
),
'grandparent2' = 'value';
)
);
Whats the best way to access the value at $array['great-grandparent']['grandparent']['parent']['child'] keeping in mind that I don't know the keys beforehand. I have used eval to construct the above syntax as a string with variable names and then eval'd the string to get the data. But eval is slow and I was hoping for something faster. Something like $class->getConfigValue('great-grandparent/grandparent/'.$parent.'/child'); that would return 'value'
Example of Eval Code
public function getValue($path, $withAttributes=false) {
$path = explode('/', $path);
$rs = '$r = $this->_data[\'config\']';
foreach ($path as $attr) {
$rs .= '[\'' . $attr . '\']';
}
$rs .= ';';
$r = null;
#eval($rs);
if($withAttributes === false) {
$r = $this->_removeAttributes($r);
}
return $r;
}
I don't know about the potential speed but you don't need to use eval to do a search like that :
$conf = array(
'great-grandparent' => array(
'grandparent' => array(
'parent' => array(
'child' => 'value searched'
),
'parent2' => 'value'
),
'grandparent2' => 'value'
)
);
$path = 'great-grandparent/grandparent/parent/child';
$path = explode('/', $path);
$result = $conf;
while(count($path) > 0) {
$part = array_shift($path);
if (is_array($result) && array_key_exists($part, $result)) {
$result = $result[$part];
} else {
$result = null;
break;
}
}
echo $result;
Here we go, my solution:
$tree = array(
'great-grandparent' => array(
'grandparent' => array(
'parent' => array(
'child' => 'value1'
),
'parent2' => 'value2'
),
'grandparent2' => 'value3'
)
);
$pathParts = explode('/','great-grandparent/grandparent/parent/child');
$pathParts = array_reverse($pathParts);
echo retrieveValueForPath($tree, $pathParts);
function retrieveValueForPath($node, $pathParts) {
foreach($node as $key => $value) {
if(($key == $pathParts[count($pathParts)-1]) && (count($pathParts)==1)) {
return $value;
}
if($key == $pathParts[count($pathParts)-1]) {
array_pop($pathParts);
}
if(is_array($value)) {
$result = retrieveValueForPath($value, $pathParts);
}
}
return $result;
}