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
Related
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',
))
);
I try send push notifications only to users segmented by tags from my app with Onesignal by php but the notificatión is send to all users, this is my code:
$fields = array(
'app_id' => $onesignal_wp_settings['app_id'],
'headings' => array("en" => get_the_title($post->ID), "es" => get_the_title($post->ID)),
'isAnyWeb' => false,
'url' => get_permalink($post->ID),
'contents' => array("es" => max_words(array(
"max" => 40,
"id_post" => $post -> ID
)),
"es" => max_words(array(
"max" => 40,
"id_post" => $post -> ID
))),
'tags' => array(
array(
"key" => "municipio",
"relation" => "=",
"value" => (string)$id_municipio
),
array(
"operator" => "OR"
),
array(
"key" => "estado",
"relation" => "=",
"value" => (string)$id_estado
)
)
);
The contents field of your code isn't correct. It can only contain an array of languages with a string for it's contents. Your post_id should be added to data instead.
'contents' => array("en" => "English message",
"es" => "Spanish message"),
'data' => array("id_post" => $post -> ID)
Your tags field looks correct however I am not sure what your variables contain. You should print out your full JSON payload to make sure you don't have any errors in the format.
how i can change specific array value in PHP? i have a Movie casts info that is a array:
array (
0 =>
array (
'name' => 'Mia Wasikowska',
'id' => 'nm1985859',
'url' => 'http://www.imdb.com/name/nm1985859/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzNzI0MDczN15BMl5BanBnXkFtZTgwNTQ0MjMxOTE#._V1_.jpg',
'character' => 'Alice Kingsleigh',
),
1 =>
array (
'name' => 'Johnny Depp',
'id' => 'nm0000136',
'url' => 'http://www.imdb.com/name/nm0000136/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTM0ODU5Nzk2OV5BMl5BanBnXkFtZTcwMzI2ODgyNQ##._V1_.jpg',
'character' => 'Hatter Tarrant Hightopp',
),
2 =>
array (
'name' => 'Helena Bonham Carter',
'id' => 'nm0000307',
'url' => 'http://www.imdb.com/name/nm0000307/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BMTUzMzUzMDg5MV5BMl5BanBnXkFtZTcwMDA5NDMwNA##._V1_.jpg',
'character' => 'Iracebeth',
),
3 =>
array (
'name' => 'Anne Hathaway',
'id' => 'nm0004266',
'url' => 'http://www.imdb.com/name/nm0004266/',
'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA##._V1_.jpg',
'character' => 'Mirana',
),
)
i need to change image value to something different. for example, change this:
'image' => 'http://ia.media-imdb.com/images/M/MV5BNjQ5MTAxMDc5OF5BMl5BanBnXkFtZTcwOTI0OTE4OA##._V1_.jpg',
to this:
'image' => '/uploads/images/artirst_0214.jpg',
and in the end i need to have it like this:
array (
0 =>
array (
'name' => 'Mia Wasikowska',
'id' => 'nm1985859',
'url' => 'http://www.imdb.com/name/nm1985859/',
'image' => '/uploads/images/artirst_032.jpg',
'character' => 'Alice Kingsleigh',
),
1 =>
array (
'name' => 'Johnny Depp',
'id' => 'nm0000136',
'url' => 'http://www.imdb.com/name/nm0000136/',
'image' => '/uploads/images/artirst_07897.jpg',
'character' => 'Hatter Tarrant Hightopp',
),
2 =>
array (
'name' => 'Helena Bonham Carter',
'id' => 'nm0000307',
'url' => 'http://www.imdb.com/name/nm0000307/',
'image' => '/uploads/images/artirst_0987.jpg',
'character' => 'Iracebeth',
),
3 =>
array (
'name' => 'Anne Hathaway',
'id' => 'nm0004266',
'url' => 'http://www.imdb.com/name/nm0004266/',
'image' => '/uploads/images/artirst_0214.jpg',
'character' => 'Mirana',
),
)
thank you
$ArrayKeys=array_keys($data);
$cntKeys=count($ArrayKeys);
for ($i=0;$i<$cntKeys;$i++)
{
$data[$ArrayKeys[$i]]["image"]="ciro";
}
print_r ($data);
Let's suppose that your array is called "data".
In this way you get can the keys of your array (0,1,2....) and on each of them you iterate to access to the img value...
Then you can access to the value using $data[key_your_array]["image"] = "something". I use
$data[$ArrayKeys[$i]]
because, as the name suggest, $ArrayKeys is an array and I want to access to the i-th element
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']);
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.