I have a HTML form with multiple inputs.
I have the below php code to get them inputs and put them in an associated array.
However, when dumping the Associated array the value only shows the first letter...
<?php
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
foreach($valueArray as $key => $value)
{
if (isset($_POST[$key]))
{
$postValue = $_POST[$key];
$actualValue = $postValue;
$valueArray[$key][$value] = $actualValue;
}
}
var_dump($valueArray);
?>
This is what is dumped -
array(4) { ["servername"]=> string(1) "d" ["serverlocation"]=> string(1) "K" ["servertype"]=> string(1) "P" ["serverdescription"]=> string(1) "t" } post
How do i get it to store the whole string, and not just the first letter?
If you want to fill the valueArray with the content of the POST request you have to do this:
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
foreach($valueArray as $key => $value)
{
if (isset($_POST[$key]))
{
$postValue = $_POST[$key];
$valueArray[$key] = $postValue;
}
}
var_dump($valueArray);
I think you ar wrong with this line:
$valueArray[$key][$value] = $actualValue;
Try this
$valueArray=array
(
"servername"=>'',
"serverlocation"=>'',
"servertype"=>'',
"serverdescription"=>''
);
$postData=array
(
"servername"=>'serverName',
"serverlocation"=>'serverLocation',
"servertype"=>'serverType',
"serverdescription"=>'serverDescription'
);
foreach($valueArray as $key => $value)
{
if (isset($postData[$key]))
{
$postValue = $postData[$key];
$actualValue = $postValue;
$valueArray[$key] = $actualValue;
}
}
var_dump($valueArray);
Related
I have an array of array like this
$data=array(
array("9900","1","7"),
array("9901","1","7"),
array("9902","1","7"),
array("9903","1","4"),
array("9904","3","8"),
array("9908","1","5")
);
I have value 9908. When I search 9908 then the value array("9908","1","5") should be printed. I have used array_search() but I have not got any success
How I can print the array after finding the value
Try this:
var_dump($data[array_search("9908", array_column($data, 0))]);
To expand it,
array_column returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.
array_search Searches the array for a given value and returns the first corresponding key if successful.
Edit:
To add some control over it:
$index = array_search("9908", array_column($data, 0));
if($index !== false){
// do your stuff with $data[$index];
var_dump($data[$index]);
}
Dumps:
array(3) {
[0]=>
string(4) "9908"
[1]=>
string(1) "1"
[2]=>
string(1) "5"
}
Perhaps this can help:
<?php
function search_first_row($needle, $haystack){
$data = $haystack;
$desired_value = $needle;
foreach($data as $row){
if($row[0] == $desired_value){
return $row;
}
}
}
try this :
$data=array(
array("9900","1","7"),
array("9901","1","7"),
array("9902","1","7"),
array("9903","1","4"),
array("9904","3","8"),
array("9908","1","5")
);
foreach ($data as $key => $value) {
if( in_array("9908",$value)){
$findindex = $key;
}
}
var_dump($data[$findindex]);
$data=array(
array("9900","1","7"),
array("9901","1","7"),
array("9902","1","7"),
array("9903","1","4"),
array("9904","3","8"),
array("9908","1","5")
);
$searchValue = '9908';
for($i=0; $i<count($data); $i++){
$innerArray = $data[$i];
for($j=0; $j<count($innerArray); $j++){
if($innerArray[$j] == $searchValue){
print_r($innerArray);
}
}
}
I am trying to locale the correct sub-array in order to change the count, if a specific value is present more than once.
I have the following code:
$trending = [];
foreach($hashtags as $hashtag) {
if(in_array($hashtag->hashtag, $hashtags))
{
array_search()
}
else {
array_push($trending, [
'hashtag' => $hashtag->hashtag,
'counts' => '1'
]);
}
}
This gives me the following example outout:
array(3) {
[0]=> array(2)
{
["hashtag"]=> "foobar"
["counts"]=> "1"
}
[1]=> array(2)
{
["hashtag"]=> "hashtags"
["counts"]=> "1"
}
[2]=> array(2)
{
["hashtag"]=> "imageattached"
["counts"]=> "1"
}
}
So in the foreach loop and the if statement, i want to check for dublicates of hashtags, e.g. if the hashtag foobar exists more than one time, I don't want to create another dublicate in the array, but I want to change the count to 2
How do I find the correct "sub"-array, and change the count of this to 2, if a hashtag is present within $hashtags more than once??
The idea is, that I at the end can sort these arrays, and get the hashtag that is most common, by looking at the count.
If you change the structure of your output, you could do something like this:
$trending = [];
foreach($hashtags as $tag) {
if (isset($trending[$tag])) $trending[$tag]++;
else $trending[$tag] = 1;
}
Which would result in $trending having the structure
array(2) {
["foobar"] => 1,
["hashtags"] => 2
}
Which could then be looped through with
foreach($trending as $tag => $count) {
echo $tag . ' appears ' . $count . ' times.' . PHP_EOL;
}
The PHP method array_count_values might be of some help.
http://php.net/manual/en/function.array-count-values.php
Have you considered using a keyed array?
$trending = array();
foreach($hashtags as $hashtag) {
if(!isset($trending[$hashtag])){
$trending[$hashtag] = 1;
}else{
$trending[$hashtag] += 1;
}
}
By using a keyed array, there is no duplication and you can easily check how frequently a hashtag is used by just accessing $trending[$hashtag]. Additionally, you can get the list of all hashtags in the trending array using $allHashtags = array_keys($trending);.
Of course, if your project specifications do not allow for this, then by all means, use a different approach, but that would be the approach I would take.
It can be more linear of you can change your array structure but for the current this should work.
$trending = [];
$checker = true;
foreach($hashtags as $hashtag) {
foreach ($trending as $key =>$value) {
if($value["hashtag"] == $hashtag->hashtag){
$trending[$key]["counts"]++;
$checker = false;
}
}
if($checker) {
array_push($trending, [
'hashtag' => $hashtag->hashtag,
'counts' => '1'
]);
}
$checker = true;
}
Any one explain me how can I compare two characters in php
Here my Code:
$unsorted = Array(
"0" =>"0000C11",
"1" =>"0000A11",
"2" =>"0000C13",
"3" =>"0000D11",
);
$sortArr = array('A','B','C','D');
foreach ($unsorted as $key => $value) {
$val = substr($value,-3,1);
foreach ($sortArr as $key1 => $value1) {
if ($val === $value1 ) {
$sortArrFin[] = $value;
}
}
}
echo "<pre>";
print_r($sortArrFin);
Here I want to check condition if ($val === $value1 ) but it gives always true..
Means if $val = C and $value1 = A ti's return true...
Please help me.
Thanks
Please try following code, actually you have to make inner foreach to outer and outer for loop to inner.
<?php
$unsorted = Array(
"0" =>"0000C11",
"1" =>"0000E11",
"2" =>"0000C13",
"3" =>"0000D11",
"4" =>"0000A11"
);
$sortArr = array('A','B','C','D','E');
foreach ($sortArr as $key => $value) {
foreach ($unsorted as $key1 => $value1) {
$val = substr($value1,-3,1);
if ($val === $value ) {
$sortArrFin[] = $value1;
}
}
}
?>
To get the value from var_dump:
mymeta_url_group =>
0 => string(39) "a:1:{s:10:"mymeta_url";s:8:"You rock";}"
1 => string(40) "a:1:{s:10:"mymeta_url";s:9:"Yeah Sure";}"
I have used:
$urls= get_post_meta( get_the_ID(), 'mymeta_url_group', false );
foreach ( $urls as $url)
{
echo $url["mymeta_url"];
// You Rock
//Yeah Sure
}
Now since I have added repeater and sorter option in backend new var dump shows:
mymeta_url_group =>
0 => string(102) "a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:8:"You rock";s:11:"cmb-field-1";s:11:"Nope Maybe ";}}"
1 => string(100) "a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:9:"Yeah Sure";s:11:"cmb-field-1";s:9:"Won't you";}}"
Now How can I get Values "You rock" "Nope Maybe ""Yeah Sure""Won't you" extending my previous solution.
PS if I do var_dump($url["mymeta_url"]);
The output is
Arrayarray(2) { ["cmb-field-0"]=> string(8) "You rock" ["cmb-field-1"]=> string(11) "Nope Maybe " } Arrayarray(2) { ["cmb-field-0"]=> string(9) "Yeah Sure" ["cmb-field-1"]=> string(9) "Won't you" }
I don't know what get_post_meta() is supposed to do, but maybe this will give you some ideas:
<?php
/* Setting up data */
$data = array (
'a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:8:"You rock";s:11:"cmb-field-1";s:11:"Nope Maybe ";}}',
'a:1:{s:10:"mymeta_url";a:2:{s:11:"cmb-field-0";s:9:"Yeah Sure";s:11:"cmb-field-1";s:9:"Won\'t you";}}'
);
$urls = array();
foreach ($data as $s) {
$urls[] = unserialize ($s);
}
/* Retrieving data */
foreach ($urls as $url) {
foreach ($url['mymeta_url'] as $u) {
echo "$u, ";
}
echo "\n";
}
Here's what I came up with:
foreach($array as $array2) { //If it doesn't work change $array to $array['mymeta_url_group']
$array2 = unserialize($array2);
foreach($array2['mymeta_url'] as $line) {
echo $line; //$line is the sentance you're looking for
}
}
I'm not sure if will work since I had to reconstruct the array from the var_dump() output.
My foreach is entering an array in the second level of my $bad_email array, something like this
["vlley#.rsr.com"] { ["name"]=> "Woo Hilley", ["amount"]=> "125.16"}
["vey#.rsr.com"] { ["name"]=> "Shoo Moo", ["amount"]=> "12.16"}
If I try to enter the same value like ["vlley#.rsr.com"] { ["name"]=> "Woo Hilley", ["amount"]=> "125.16"} again I want it to run specific code. Im not sure how to fix this. The code Im running seems to work when the name is the same but I want the email, name and amount to all match before it fires. Help please
$bad_email = array();
$i = 0;
foreach ($id_array as $key => $id) {
$bad_email[$email][name] =$name;
$bad_email[$email][amount] = $amount;
if ($bad_email[$email][$amount], $bad_email[$email])) {
// DO CODE HERE!!!!
$i++;
}
}
$email, $name, and $amount are all pulled from an api call
This worked...
$bad_email = array();
$temp_email = array();
foreach ($id_array as $key => $id) {
$temp_email =$bad_email;
$bad_email[$email][name] =$name;
$bad_email[$email][amount] = $amount;
if ($bad_email[$email][amount] == $temp_email[$email][amount]){
// DO CODE HERE!!!!
}
}
}