Removing the key of associative array not working - php

I have an associative array like the following.
array(1) {
["ftr_file_uploads"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
array(1) {
[0]=>
string(13) "Hydrangeas.jpg"
}
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
}
[1]=>
array(2) {
["name"]=>
array(1) {
[0]=>
string(13) "w.jpg"
}
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
}
}
}
I need to change the array structure to the following format.
array(1) {
["ftr_file_uploads"]=>
array(2) {
["name"]=>
array(2) {
[0]=>
string(14) "Hydrangeas.jpg"
[1]=>
string(5) "w.jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
}
}
I have tried the following.
foreach($_FILES['ftr_file_uploads'] as $keyval1=>$value1) {
$_FILES['ftr_file_uploads'][] = $_FILES[$keyval1];
unset($_FILES[$keyval1]);
}

Sample code:
$a=array('ftr_file_uploads'=>[['name'=>['Hyd.jpg']],['name'=>['w.jpg']] ]);
$arr = array();
foreach($a['ftr_file_uploads'] as $files){
$arr['file_upload']['name'][]=$files['name'][0];
}
print_r($arr);

I have done the following to obtain the result
foreach($_FILES['ftr_file_uploads'] as $key=>$files){
foreach(array_keys($files) as $k=>$v)
{
$newarray['ftr_file_uploads'][$v][]=$files[$v][0];
}
}

Related

Insert PHP Regex Exploded Array Into MySQL Table

I have an array that I exploded by Regex, here is a part of the array named $data;
array(6) {
[0]=>
array(1) {
[0]=>
string(77) "Achnanthes brevipes C.Agardh, Syst. Alg.: 1 (1824). / Küçük sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(8) "brevipes"
}
[3]=>
array(1) {
[0]=>
string(8) "C.Agardh"
}
[4]=>
array(1) {
[0]=>
string(21) "Syst. Alg.: 1 (1824)."
}
[5]=>
array(1) {
[0]=>
string(22) "Küçük sucıncığı"
}
}
array(6) {
[0]=>
array(1) {
[0]=>
string(89) "Achnanthes cocconeiformis Mann, U.S. Nat. Mus., Bull. 6: 182 (1925). / Top sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(14) "cocconeiformis"
}
[3]=>
array(1) {
[0]=>
string(4) "Mann"
}
[4]=>
array(1) {
[0]=>
string(36) "U.S. Nat. Mus., Bull. 6: 182 (1925)."
}
[5]=>
array(1) {
[0]=>
string(17) "Top sucıncığı"
}
}
array(6) {
[0]=>
array(1) {
[0]=>
string(108) "Achnanthes gibberula Grunow, Kongl. Svenska Vetensk.-Akad. Handl. 17(2): 121 (1880). / Kambur sucıncığı."
}
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
}
[2]=>
array(1) {
[0]=>
string(9) "gibberula"
}
[3]=>
array(1) {
[0]=>
string(6) "Grunow"
}
[4]=>
array(1) {
[0]=>
string(55) "Kongl. Svenska Vetensk.-Akad. Handl. 17(2): 121 (1880)."
}
[5]=>
array(1) {
[0]=>
string(20) "Kambur sucıncığı"
}
}
and many more..
my regex is:
$turRegex = '/^([^\s]+)[\s]([^\s]+)[\s]([^\s]+)[,][\s]([A-Za-z].+)[\s][\/][\s](.+)[.]/m';
my foreach loop:
foreach ($data as $data) {
if (!empty(preg_match_all($turRegex, $data, $matches))) {
echo "<pre>";
var_dump($matches);
echo "</pre>";
}
}
My table name is "algeaSpecies" and columns are; "id","genusName","speciesEpiteth","author", "publication","TurkishName"
I want to insert this to genusName;
[1]=>
array(1) {
[0]=>
string(10) "Achnanthes"
this to speciesEpiteth;
[2]=>
array(1) {
[0]=>
string(8) "brevipes"
}
and others...
I'm using MySQLi
Thank you
First I noticed you used $data both as array and item in your loop.
In the following code, I assumed that you don't want the first index (the query wasn't clear).
Also, replace the DB connection credentials.
$con = new mysqli('db_host', 'username', 'password', 'db_name');
$boundArray = array_fill(0, 5, null);
$query = "INSERT INTO `algeaSpecies` (`genusName`, `speciesEpiteth`, `author`, `publication`, `TurkishName`) VALUES (?,?,?,?,?);";
$stmt = $con->prepare($query);
$stmt->bind_param("sssss", ...$boundArray);
foreach ($data as $item) {
$i = -1;
foreach ($item as $index) {
if ($i === -1) {
$i++;
continue;
}
$boundArray[$i++] = $index[0];
}
$stmt->execute();
}
$stmt->close();
$con->close();

php segmented file uploads seperated in array

I have created an dynamic registration table - and related to one of the segments I need to be able to upload a file.
It all works like dandy. Except the fileupload.
Doing an print_r($_FILES); after attempted upload gives this:
[0]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(16) "image001 (3).jpg"
[1]=>
string(16) "image001 (2).jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phpdqCWSl"
[1]=>
string(14) "/tmp/phpabqqwj"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(1288)
[1]=>
int(1288)
}
}
}
Here it kind of merges the two uploads into the same array instead of making the array size of two sub arrays.
<td style='display:none;' class=td_register_purchases>
<input type=text name=registration[][purchase_data] id=purchase_data class=purchase_data>
<select name='registration[][purchase_type]' id=select_purchase class=purchase_type>
<option value=1>Accomodation</option>
<option value=2>Catering</option>
<option value=3>Tools</option>
<option value=4>Software/License</option>
<option value=5>Hardware</option>
</select>
<textarea rows=2 cols=50 name='registration[][purchase_comment]' class=purchase_comment placeholder='Comment Area'></textarea>
<input type=file name='upload_file[]' class='upload-file'> Attachment
</td>
I could work with this result:
[0]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(16) "image001 (3).jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpdqCWSl"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(1288)
}
}
[1]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(16) "image001 (2).jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpabqqwj"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(1288)
}
}
}
But I am probably making a mistake somewhere that could fix it all.
Working hard.. And found a - not beautifull solution ;-)
$number_files = sizeof($_FILES["upload_file"]["name"]);
$fields = 4;
$field_count = 0;
$row_count = 0;
if (!empty($_FILES)) {
for($x = 0; $x < $number_files; $x++) {
foreach ($_FILES AS $reg_array) {
foreach ($reg_array AS $segment => $value)
{
if ($field_count == $fields)
{
$temp_files[$row_count][$segment] = $value[$x];
$field_count = 0;
$row_count++;
}
else
{
$field_count++;
$temp_files[$row_count][$segment] = $value[$x];
}
} // end foreach
} // end foreach
} // end for
} // end if

Issue with uploading file inputs with multiple names

I have several file input fields with a common name and a unique name for each file input .The unique name is for some validation purposes.
<input name="file12 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
<input name="file10 ftr_file_uploads[]" class="multi_files file " type="file">
...............
When trying to upload files in PHP form submit,
the content of $_FILES is as following.
array(2) {
["file1_ftr_file_uploads"]=> array(5)
{ ["name"]=> array(1) { [0]=> string(13) "Jellyfish.jpg" }
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpx7iId2" }
["error"]=> array(1) { [0]=> int(0) }
["size"]=> array(1) { [0]=> int(775702) } }
["file2_ftr_file_uploads"]=> array(5)
{ ["name"]=> array(1) { [0]=> string(12) "Penguins.jpg" }
["type"]=> array(1) { [0]=> string(10) "image/jpeg" }
["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpN6QWoD" }
["error"]=> array(1) { [0]=> int(0) }
["size"]=> array(1) { [0]=> int(777835) } }
}
The array key name changed to the concatenated names of the file input field.I need the names to be ftr_file_uploads rather than fileIDnumber_ftr_file_uploads.
I have done like the following.
foreach($_FILES as $keyval=>$value)
{
$_FILES['ftr_file_uploads'] = $_FILES[$keyval]; //removed
$_FILES['ftr_file_uploads'][] mentioned in the answer
unset($_FILES[$keyval]);
}
When i am using like this,I am getting the result like this.
array(1) {
["ftr_file_uploads"]=>
array(5) {
["name"]=>
array(1) {
[0]=>
string(14) "Lighthouse.jpg"
}
["type"]=>
array(1) {
[0]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(1) {
[0]=>
string(14) "/tmp/phpLdslxb"
}
["error"]=>
array(1) {
[0]=>
int(0)
}
["size"]=>
array(1) {
[0]=>
int(561276)
}
}
}
I need the result like this.
array(1) {
["ftr_file_uploads"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(14) "Hydrangeas.jpg"
[1]=>
string(5) "w.jpg"
}
["type"]=>
array(2) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(14) "/tmp/phpKMwmH1"
[1]=>
string(14) "/tmp/phpwwHU9G"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(595284)
[1]=>
int(879394)
}
}
}
foreach ($_FILES as $name => $file) {
$_FILES['ftr_file_uploads'][] = $file; // [] means add $file to $_FILES['ftr_file_uploads'] array
unset($_FILES[$name]); // remove element from $_FILES
}

Foreach creating multi-level instead of single level array in PHP

I'm trying to gather a group of term_id's output in a foreach and create an array from them. I then want to update the taxonomy with the values in the array however the array is being created as multi-level. My code is as follows:
$updateTax = array();
foreach ($featuresArray as $key => $value) {
if ($key = 'en_value') {
$termResult = get_term_by('name', $value['en_value'], $taxonomy);
$term = $termResult->term_id;
$updateTax[] = array($term);
}
}
...which then gives this output:
var_dump($updateTax);
array(29) {
[0]=> array(1) {
[0]=> int(111) } [1]=> array(1) {
[0]=> int(116) } [2]=> array(1) {
[0]=> int(124) } [3]=> array(1) {
...
[0]=> int(408) } [25]=> array(1) {
[0]=> int(447) } [26]=> array(1) {
[0]=> int(520) } [27]=> array(1) {
[0]=> int(593) } [28]=> array(1) {
[0]=> int(628) }
}
...but I was expecting the following:
array(29) {
[0]=> int(111) }
[1]=> int(116) }
[2]=> int(124) }
[3]=> int(125) }
...
Bit puzzled so could do with some guidance please. Many thanks.
Replace the following line, where you are creating an individual array for each $term:
$updateTax[] = array($term);
With this:
$updateTax[] = $term;

PHP how to get value from array with foreach

I have array $result
array(2) {
["Smiley TV"]=>
array(2) {
["Speed"]=>
array(2) {
[0]=>
string(4) "9510"
[1]=>
string(5) "33775"
}
["Turbo"]=>
array(2) {
[0]=>
string(4) "2427"
[1]=>
string(5) "19696"
}
}
["Victory Media"]=>
array(1) {
["Speed"]=>
array(2) {
[0]=>
string(4) "4144"
[1]=>
string(5) "80445"
}
}
}
How with foreach i can get values. For example in the "Smyley TV" how i can result "Speed".
Also please write how i can get all data one by one. Thanks
You can fetch this way
foreach ($arrays as $array) {
echo $array['smyley TV'];
}

Categories