This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I'm processing a form that submits to my PHP page. The request data looks like this:
Array
(
[submission_id] => 363112875894117228
[name] => Array
(
[0] => Tom
[1] => Jones
)
[address] => Array
(
[0] => 21 Jump St
[1] =>
[2] => Sydney
[3] => NSW
[4] => 2000
[5] => Australia
)
[cellularnumber] => Array
(
[0] => (041) 234-5678
)
)
I'm trying to set a variable that contains the value of the first name, last name etc. For example I would like to set a variable:
$firstName
that is equal to Tom.
I'm familiar with using this syntax:
$firstName = $_POST['name']
but not sure how to handle the array in this case?
use array index for name like
$fname = $_POST['name'][0];
$lname = $_POST['name'][1];
and use implode statement for the address like
implode(" ",$_POST['address']) (it will convert your address array to string)
Have you tried like this , I hope this should work for you.
$firstName = $_POST['name'][0];
$lastName = $_POST['name'][1];
Its a multidimensional array. In your case the array element name have one more sub array. You can access it as follows,
$firstName = $_POST['name'][0];
$lastName = $_POST['name'][1];
You can convert the whole array to JSON to POST using json_encode, then use json_decode to fetch all data at backend.
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
$result = json_encode(Array
(
'submission_id' => 363112875894117228,
'name' => Array
(
0 => 'Tom',
1 => 'Jones',
),
'address' => Array
(
0 => '21 Jump St',
1 => '',
2 => 'Sydney',
3 => 'NSW',
4 => '2000',
5 => 'Australia',
),
'cellularnumber' => Array
(
0 => '(041) 234-5678',
),
))
End up to {"submission_id":363112875894117228,"name":["Tom","Jones"],"address":["21 Jump St","","Sydney","NSW","2000","Australia"],"cellularnumber":["(041) 234-5678"]}
Related
This question already has answers here:
PHP add elements to multidimensional array with array_push
(4 answers)
Closed 2 years ago.
I'm already getting below array results as follows. All I wanted is to add more entries in it.
How do I do that?
Array
(
[result] => Array
(
[0] => Array
(
[number] => AAA1222
[short_description] => User unable to print
[state] => Closed
)
[1] => Array
(
[number] => AAA1223
[short_description] => Share drive not accessible
[state] => Closed
)
[2] => Array
(
[number] => AAA1224
[short_description] => Network is down
[state] => Closed
)
)
)
If I wanted to add more entries to the array like
[number] => AAA1225
[short_description] => Cable replacement on workstation 12
[state] => Closed
How do I accomplish that.
Thanks!!
You can try with array_push() function too.
Your existence array:
$myArr['result'] = [........]
New array values
$data['number'] = 'AAA1224';
$data['short_description'] = 'Network is down';
$data['state'] = 'Closed';
Push new array into existance array:
array_push($myArr['result'],$data);
Considering you are storing above array in a variable named as $arr.
$arr = [ ... ]; // contains your existing data.
// To add new data
$arrNewData = [
'number' => 'AAA1225',
'short_description' => 'Cable replacement on workstation 12',
'state' => 'Closed'
];
// Push data to existing array.
$arr['result'][] = $arrNewData;
You can add another array data to your existing array
Keep your existing data into a variable $data.
$data = [ ... ]; // here you have result index and array data
Then add new result item as an array.
$data['result'][] = [
'number' => 'AAA1225',
'short_description' => 'Cable replacement on workstation 12',
'state' => 'Closed'
];
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 2 years ago.
I would like to collate data from multiple different data sources into one numeric array using foreach loops, however I am having some trouble building my array in the expected format. I would like to keep the key numeric.
For example, if I had two data sources: $fruit_data and $flavor_data and my code was:
foreach($fruit_data as $fruit){
$fruits[]['name'] = $fruit['name'];
}
foreach($flavor_data as $flavor){
$fruits[]['flavor'] = $flavor['taste'];
}
The data is added to the array with separate numeric keys, like this:
Array
(
[0] => Array
(
[name] => Example Name 1
)
[1] => Array
(
[flavor] => Example Flavor 1
)
[2] => Array
(
[name] => Example Name 2
)
[3] => Array
(
[flavor] => Example Flavor 2
)
)
However, my desired output is to display the data under the same numeric key, like this:
Array
(
[0] => Array
(
[name] => Example Name 1
[flavor] => Example Flavor 1
)
[1] => Array
(
[name] => Example Name 2
[flavor] => Example Flavor 2
)
)
Could somebody please explain where I am going wrong, and if possible, link me to a resource that explains how to overcome this issue? It may be that I have a fundamental misunderstanding of multidimensional arrays, but I cannot seem to find any references to this issue.
Any help is much appreciated.
foreach($fruit_data as $index => $fruit){
$fruits[] = [
'name' => $fruit['name'],
'flavor' => $flavor_data[$index]['taste']
];
}
Same as
foreach($fruit_data as $index => $fruit){
$fruits[] = [
'name' => $fruit_data[$index]['taste'],
'flavor' => $flavor_data[$index]['taste']
];
}
This question already has answers here:
Get the keys for duplicate values in an array
(11 answers)
Closed 4 years ago.
I have an array:
Array
(
[0] => Array
(
[sku_code_part_id] => 1
[part_label] => blue
[part_value] => BLU
)
[1] => Array
(
[sku_code_part_id] => 2
[part_label] => Orange
[part_value] => ORG
)
[2] => Array
(
[sku_code_part_id] => 3
[part_label] => Orange
[part_value] => ORG
)
[3] => Array
(
[sku_code_part_id] => 4
[part_label] => Orange
[part_value] => ORG
)
[4] => Array
(
[sku_code_part_id] => 5
[part_label] => Green
[part_value] => GRE
)
[5] => Array
(
[sku_code_part_id] => 6
[part_label] => Red
[part_value] => RED
)
)
I'm wanting a simple way of checking the array $this->request->post['custom_parts'] for the any duplicated values assigned to the part_value keys.
So I can flag an error that 'ORG' is duplicated numerous times.
I've tried various methods such as removing duplications and comparing before and after. However I'm running into a number of issues with this.
Any ideas?
You may be able to use "array_key_exists"
http://php.net/manual/en/function.array-key-exists.php
You may want to write a function but here is a solution using foreach.
$part_values = [];
$part_values_duplicates = [];
foreach($this->request->post['custom_parts'] as $customPart){
if(!in_array($customPart['part_value'], $part_values)){
$part_values[] = $customPart['part_value'];
}
else {
$part_values_duplicates[] = $customPart['part_value'];
}
}
var_dump($part_values_duplicates);
This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 5 years ago.
I have two arrays
First array
array(
[0] => +970
[1] => +971
[2] => +972
)
And Second array
array(
[0] => 465465454
[1] => 321321355
[2] => 987946546
)
I want to merge them like this
array(
[+970] => 465465454
[+971] => 321321355
[+972] => 987946546
)
I try with array_merge but this gives me the result that I didn't want e.g.
$busi_code = $page1_data->business_code; //array
$busi_num = $page1_data->business_number; //array
$business_phone_numbers = array_merge($busi_code, $busi_num);
echo '<pre>';
print_r($business_phone_numbers);
echo '</pre>';
And its result is
[0] => +970
[1] => +971
[2] => +972
[3] => 465465454
[4] => 321321355
[5] => 987946546
So please guide me how can I achieve my required result.
You're looking for array_combine, rather than array_merge:
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
$business_phone_numbers = array_combine($busi_code, $busi_num);
See https://eval.in/954799
This is job for array_combine function:
$business_phone_numbers = array_combine($busi_code, $busi_num);
DOCS: http://php.net/manual/en/function.array-combine.php
You must use array_combine.
Try this:
$a = array(
0 => +970,
1 => +971,
2 => +972);
$b = array(
0 => 465465454,
1 => 321321355,
2 => 987946546);
$r = array_combine($a,$b);
print_r($r);
I have the follow array:
$arrIni["ENV"]="US";
$arrIni["sap_db_server"] = "192.xxx.x.xx";
$arrIni["local_db_server"] = "localhost";
$arrIni["local_db_username"] = "root";
//Default settings
$arrIni["arrEnvSettings"]["UserTypeID"]=4;
$arrIni["arrEnvSettings"]["LocalizationID"]=1;
$arrIni["arrEnvSettings"]["LangLabels"] = array();
$arrIni["arrEnvSettings"]["pages"]["st1"]="st1.php";
$arrIni["arrEnvSettings"]["pages"]["st2"]="st2.php";
$arrIni["arrEnvSettings"]["pages"]["st3"]="st3.php";
And I want to merge with this one:
$setParam["arrEnvSettings"]["pages"]["st3"]="st3_V2.php";
This is what I am doing:
echo "<pre>";
print_r(array_merge($arrIni,$setParam));
echo "</pre>";
And this is what I am getting:
Array
(
[ENV] => US
[sap_db_server] => 192.xxx.x.xx
[local_db_server] => localhost
[local_db_username] => root
[arrEnvSettings] => Array
(
[pages] => Array
(
[st3] => st3_V2.php
)
)
)
In the php doc about merge, this is the comment " ...If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. ..."
So in this way, I suppose to get this output instead of the last one:
Array
(
[ENV] => US
[sap_db_server] => 192.xxx.x.xx
[local_db_server] => localhost
[local_db_username] => root
[arrEnvSettings] => Array
(
[UserTypeID] => 4
[LocalizationID] => 1
[LangLabels] => Array
(
)
[pages] => Array
(
[st1] => st1.php
[st2] => st2.php
[st3] => st3_V2.php
)
)
)
I do not understand why $setParam["arrEnvSettings"]["pages"]["st3"] is overriding the entire $arrIni["arrEnvSettings"].
Note:
If I use array_merge_recursive($arrIni,$setParam)) I will have the follow result but it is not what I want.
Array
(
[ENV] => US
[sap_db_server] => 192.xxx.x.xx
[local_db_server] => localhost
[local_db_username] => root
[arrEnvSettings] => Array
(
[UserTypeID] => 4
[LocalizationID] => 1
[LangLabels] => Array
(
)
[pages] => Array
(
[st1] => st1.php
[st2] => st2.php
[st3] => Array
(
[0] => st3.php
[1] => st3_V2.php
)
)
)
)
Is there a way to do this without iterate over the array? Only using merging? What am I doing wrong?
This should do the trick:
array_replace_recursive($arrIni,$setParam);
If you want to merge a given value, use this:
$arrIni["arrEnvSettings"]["pages"]["st3"] = $setParam["arrEnvSettings"]["pages"]["st3"];
But the way you're doing it is merging two arrays, not simply setting the value within an array. There's a gigantic difference between those two methods.
Otherwise, yes you will need to iteratively merge the arrays.
what you need is array_replace_recursive
print_r(array_replace_recursive($arrIni,$setParam));
didnt see the submitted answer..Felippe Duarte has given it already.....