Can we merge arrays inside Current parent array? - php

my main array name is $constant...
$constants = array(
'residential' => array(
"flat" => "Flat", "swimming_pool" => "Swimming pool",
"hall" => "Hall", "garden" => "Garden", "clubhouse" => "Clubhouse"
),'commercial' => array(
"shop" => "Shop", "office" => "Office"
),);
i can merge this array using array_merge like
$result = array_merge($constants['residential'],$constants['commercial']);
but i want to merge this array where i declare that array like..
$constants = array(
'residential' => array(
"flat" => "Flat", "swimming_pool" => "Swimming pool",
"hall" => "Hall", "garden" => "Garden", "clubhouse" => "Clubhouse"
),'commercial' => array(
"shop" => "Shop", "office" => "Office"
),
'test' => array_merge('residential','commercial'));
is that possible please help.....

No, this is not possible - you can't use values from the array that is just in the process of being declared, so the way to do it is similar to your first code snippet:
$constants = array(
'society_types' => array(
"residential"=>"Residential", "commercial"=>"Commercial", "resicumcomm" => "Residential cum Commercial"
),
'residential' => array(
"flat" => "Flat", "swimming_pool" => "Swimming pool", "hall" => "Hall", "garden" => "Garden", "clubhouse" => "Clubhouse"
)
);
$constants['test'] = array_merge($constants['society_types'], $constants['residential']);

Related

PHP adding another item to a array

I would like to add new elements to the array from the html form, but I don't understand how to add new element to the main array with this structure I tried array_push, but I failed
$menu = array(
"1" => array (
"title" => 'Glowna',
"page" => 'home',
"url" => "index.php",
),
"2" => array (
"title" => "Cennik",
"page" => 'services',
"url" => "index.php"
),
"3" => array (
"title" => 'Portfolio',
"page" => 'portfolio',
"url" => "index.php"
),
"4" => array (
"title" => 'O nas',
"page" => 'about',
"url" => "index.php"
),
"5" => array (
"title" => 'Kontakt',
"page" => 'contact',
"url" => "index.php"
)
);
?>
You will need to define a key, which would most likely be "6", then supply an array as the value. Your code might look something like this:
$menu['6'] = array( 'title' => "Archives",
'page' => "archives",
'url' => "archives.php");
That's all there is to it.
Your $menu array is called an associative array as it has string values for keys.
For associative arrays you can't use array_push.
What you can do is
$menu["key"] = array("title" => 'Kontakt',"page" => 'contact',"url" => "index.php")
But looking at your array, you can have numeric indexes instead of string. If you use the following structure, you can make use of array_push
$menu = array(array ("title" => 'Glowna',
"page" => 'home',
"url" => "index.php"),
array ( "title" => "Cennik",
"page" => 'services',
"url" => "index.php"))
Notice the removal of "string keys" (ex: "1" => array()),
Now you can use array_push;
array_push($menu, array("title" => "Cennik", "page" => 'services', "url" => "index.php"))
I tried doing array_push with your example and it worked perfectly fine, what's the problem?
Regarding what others said below, it seems php still recognizes string keys if they are numerical, because array push works perfectly fine even though the arrays keys are numerical strings. What they said maybe applies to keys that are true strings or not in numerical order.
$menu = array(
"1" => array (
"title" => 'Glowna',
"page" => 'home',
"url" => "index.php",
),
"2" => array (
"title" => "Cennik",
"page" => 'services',
"url" => "index.php"
),
"3" => array (
"title" => 'Portfolio',
"page" => 'portfolio',
"url" => "index.php"
),
"4" => array (
"title" => 'O nas',
"page" => 'about',
"url" => "index.php"
),
"5" => array (
"title" => 'Kontakt',
"page" => 'contact',
"url" => "index.php"
)
);
array_push($menu, array("title" => 'number 6', "page" => 'something', "url" => "works.php"));
var_export($menu);
Results:
array (
1 =>
array (
'title' => 'Glowna',
'page' => 'home',
'url' => 'index.php',
),
2 =>
array (
'title' => 'Cennik',
'page' => 'services',
'url' => 'index.php',
),
3 =>
array (
'title' => 'Portfolio',
'page' => 'portfolio',
'url' => 'index.php',
),
4 =>
array (
'title' => 'O nas',
'page' => 'about',
'url' => 'index.php',
),
5 =>
array (
'title' => 'Kontakt',
'page' => 'contact',
'url' => 'index.php',
),
6 =>
array (
'title' => 'number 6',
'page' => 'something',
'url' => 'works.php',
),
)
Live demo: http://sandbox.onlinephpfunctions.com/code/76d5dab43255459455f2e80e37ab281486009fc4

How to make an array key an array PHP

I'm storing data to an array like this which is inside three nested loops (loops omitted):
$teamDetails[$k] = array(
'side' => $json['data'][$i]['rosters'][$k]['side'],
'gold' => $json['data'][$i]['rosters'][$k]['gold'],
'aces' => $json['data'][$i]['rosters'][$k]['aces_earned'],
'herokills' => $json['data'][$i]['rosters'][$k]['hero_kills'],
'winner' => translateGame($json['data'][$i]['rosters'][$k]['winner']),
'participants'[$j] => array(
'work' => 'it worked',
)
);
How can make 'participants' an array with the indices coming from $j?
That's easy
$teamDetails[$k] = array(
'side' => $json['data'][$i]['rosters'][$k]['side'],
'gold' => $json['data'][$i]['rosters'][$k]['gold'],
'aces' => $json['data'][$i]['rosters'][$k]['aces_earned'],
'herokills' => $json['data'][$i]['rosters'][$k]['hero_kills'],
'winner' => translateGame($json['data'][$i]['rosters'][$k]['winner']),
'participants' => array(
$j => array(
'work' => 'it worked',
))
);

if statement in an array

I have a array I have to post (json). But the value accountNr should either be $val_nc_iban or should be NULL depending on a certain value outside this array. So how can I either echo $val_nc_iban (is a string) or NULL (not string) inside an array depending on the outside value?
$curl_post_data_nc = array(
"person" => array(
"title" => "$val_nc_persontitle",
"nationalNr" => NULL,
"firstName" => "$val_nc_personfirstname",
"lastName" => "$val_nc_personsurname",
"birthDate" => "$new_val_nc_persondob"
),
"company" => array(
"type" => "$val_nc_companytype",
"name" => "$val_nc_companyname",
"vat" => "$val_nc_companyvat",
"nace" => "$val_nc_companynace",
"website" => NULL
),
"contact" => array(
"email" => "$val_nc_personemail",
"mobile" => "$val_nc_personphone",
"telephone" => NULL
),
"contract" => array(
"referenceDate" => "$val_nc_refdate",
"startDate" => "$val_nc_startdate"
),
"payment" => array(
"paymentMethodEmail" => false,
"paymentMethodMail" => true,
"paymentInterval" => "$val_nc_paymentbilling",
"method" => "$val_nc_paymentmethod",
"accountNr" => $val_nc_result = ($val_nc_paymentmethod == 'TRANSFER') ? NULL : "$val_nc_iban"
),
"deliveryAddress" => array(
"building" => "HOUSE",
"street" => "$val_nc_personstreet",
"streetNr" => "$val_nc_personstreetnr",
"floor" => NULL,
"boxNr" => NULL,
"localityCode" => "$val_nc_personpostcode",
"localityName" => "$val_nc_personlocality"
),
"invoiceAddress" => array(
"sameAsDelivery" => false,
"building" => "HOUSE",
"street" => "$val_nc_billstreet",
"streetNr" => "$val_nc_billstreetnr",
"floor" => NULL,
"boxNr" => NULL,
"localityCode" => "$val_nc_billpostcode",
"localityName" => "$val_nc_billlocality"
),
"gasMeter" => array(
"ean" => "$val_nc_ean",
"nr" => NULL,
"type" => "gas",
"electric" => NULL,
"gas" => array(
"usage" => $val_nc_gasusage,
"formula" => "TTF103532"
)
),
Why do you put the variables in quotes? You can use their actual value in the array!
Example:
$curl_post_data_nc = array(
"person" => array(
"title" => $val_nc_persontitle,
"nationalNr" => NULL,
"firstName" => $val_nc_personfirstname,
"lastName" => $val_nc_personsurname,
"birthDate" => $new_val_nc_persondob
),
"company" => array(
"type" => $val_nc_companytype,
"name" => $val_nc_companyname,
"vat" => $val_nc_companyvat,
"nace" => $val_nc_companynace,
"website" => NULL
),
"contact" => array(
"email" => $val_nc_personemail,
"mobile" => $val_nc_personphone,
"telephone" => NULL
),
"contract" => array(
"referenceDate" => $val_nc_refdate,
"startDate" => $val_nc_startdate
),
"payment" => array(
"paymentMethodEmail" => false,
"paymentMethodMail" => true,
"paymentInterval" => $val_nc_paymentbilling,
"method" => $val_nc_paymentmethod,
"accountNr" => $val_nc_result = ($val_nc_paymentmethod == 'TRANSFER') ? NULL : $val_nc_iban
),
"deliveryAddress" => array(
"building" => "HOUSE",
"street" => $val_nc_personstreet,
"streetNr" => $val_nc_personstreetnr,
"floor" => NULL,
"boxNr" => NULL,
"localityCode" => $val_nc_personpostcode,
"localityName" => $val_nc_personlocality
),
"invoiceAddress" => array(
"sameAsDelivery" => false,
"building" => "HOUSE",
"street" => $val_nc_billstreet,
"streetNr" => $val_nc_billstreetnr,
"floor" => NULL,
"boxNr" => NULL,
"localityCode" => $val_nc_billpostcode,
"localityName" => $val_nc_billlocality
),
"gasMeter" => array(
"ean" => $val_nc_ean,
"nr" => NULL,
"type" => "gas",
"electric" => NULL,
"gas" => array(
"usage" => $val_nc_gasusage,
"formula" => "TTF103532"
)
),

correct notation of three dimensional array

I am a little bit in doubt, if this is a correct notation in a three dimensional array. This is just a part of my code, but when I run the code I get an error, where it says I need ')'.
$property = array(
"green" => array(
"numbers" => array(1 => "#ffffff"
),
"yellow" => array(
"numbers" => array(6 => "#81c77d"
),
"white" => array(
"numbers" => array(24 => "#81e87c"
),
"grey" => array(
"numbers" => array(0 => "#ffffff"
),
"red" => array(
"numbers" => array(34 => "#dfb07b"
)
);
You are missing parentheses - they are always needed to pair up. It should look like this:
$property = array(
"green" => array(
"numbers" => array(1 => "#ffffff")
),
"yellow" => array(
"numbers" => array(6 => "#81c77d")
),
"white" => array(
"numbers" => array(24 => "#81e87c")
),
"grey" => array(
"numbers" => array(0 => "#ffffff")
),
"red" => array(
"numbers" => array(34 => "#dfb07b")
)
);
Use an IDE like Eclipse or Aptana Studio which will show you the syntax errors while you type so that you won't need to run the code to see something is wrong.
$property = array(
"green" => array(
"numbers" => array(1 => "#ffffff")
),
"yellow" => array(
"numbers" => array(6 => "#81c77d")
),
"white" => array(
"numbers" => array(24 => "#81e87c")
),
"grey" => array(
"numbers" => array(0 => "#ffffff")
),
"red" => array(
"numbers" => array(34 => "#dfb07b")
)
);
Parenthesis after the hex code
$property = array(
"green" => array(
"numbers" => array(1 => "#ffffff")
),
"yellow" => array(
"numbers" => array(6 => "#81c77d")
),
"white" => array(
"numbers" => array(24 => "#81e87c")
),
"grey" => array(
"numbers" => array(0 => "#ffffff")
),
"red" => array(
"numbers" => array(34 => "#dfb07b")
),
);
You missed parenthesis.

TYPO3 suggest wizard, search in substring

I use the suggest wizard in TYPO3 backend.
The following code is in the tca:
'tx_db_colors' => array (
'exclude' => 0,
'label' => 'Farbe',
'config' => array (
"type" => "group",
"allowed" => "tx_db_colors",
"foreign_table" => "tx_db_colors",
"internal_type" => "db",
"size" => 1,
"minitems" => 0,
"maxitems" => 1,
'items' => array(array('', ''),),
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
),
),
)
),
Is there a solution, to get matched records in substring of the label, not from scratch?
Example:
The records label is named 'coffee black'
When I type 'co' into the search field, the record will be shown.
'blac' won't match to any record.
Is this possible to find this record, when I type in a substring? Else I have to extend the autocompletion. TYPO3 Core, yuk! :-)
Thank you in advance!
After hours, I found the solution.
You have to write the tca like this:
'tx_db_colors' => array (
'exclude' => 0,
'label' => 'Farbe',
'config' => array (
"type" => "group",
"allowed" => "tx_db_colors",
"foreign_table" => "tx_db_colors",
"internal_type" => "db",
"size" => 1,
"minitems" => 0,
"maxitems" => 1,
'items' => array(array('', ''),),
'wizards' => array(
'suggest' => array(
'type' => 'suggest',
'default' => array(
'searchWholePhrase' => 1
),
),
),
)
),
Just add
'default' => array(
'searchWholePhrase' => 1
),
into the 'suggest' array.

Categories