Explode Array and store in db - php

I'm having a problem exploding an array
Here's my code
$arr1 = array();
$i=1;
foreach ($out1 as $value2){
$arr1[][$i]= array_merge((array)$value2,(array)$detail);
$i++;
}
and this is the output
Array(
[0] => Array(
[id] => 1234
[name] => Rick Roll
[dept] => IT)
[1] => Array(
[id] => 1234
[name] => Dave Roll
[dept] => IT)
)
but when i try to explode the array it gives me error message
Warning: explode() expects parameter 2 to be string, array given
here's the code
$data = explode(","$array)
$q = "INSERT INTO ".TBL_ARE_ENTRY." VALUES(null,'$id[1]','$name[2]','$dept[3]')";

You do not need to use explode. i think this is what you want:
$q = "INSERT INTO ".TBL_ARE_ENTRY."
VALUES(null,''$array[0][0]','$array[0][1]',''$array[0][2]')";

Related

PHP replace characters in array

I'm trying to replace quotes and others characters from a mysql result array, using strtr() function. But i got Warning: strtr() expects parameter 1 to be string, array given and null values.
Heres my code:
while($rows = mysqli_fetch_assoc($list)){
$string_filter = array(';'=>'','['=>'',']'=>'','{'=>'','}'=>'','"'=>'',"'"=>'');
$data[$rows['id']] = strtr($rows,$string_filter);
}
$data is an array that i want to remove/trim the characters specified in $string_filter array. Heres the content sample from $data:
Array ( [1] => Array ( [id] => 1 [user_id] => 204554 [name] => Rich [group] => PL [ai] => BA [image] => 204554-35849.jpg ) [2] => Array ( [id] => 2 [user_id] => 124534 [name] => James [group] => SS [ai] => IT [image] => 123141-12312.jpg )...
Quick Question, are you sure these characters actually exist in your data? Because the characters you are trying to remove are the ones php will automatically append to an array when that array is viewed via the print_r function. To be precise this statement
print_r(array('key' => 'value'));
will output
Array([key] => value)
even tough there is no actual [ or ] present in the arrays data itself.
In any case this code snippet will clean the keys and values of your array:
$string_filter = array(';'=>'','['=>'',']'=>'','{'=>'','}'=>'','"'=>'',"'"=>'');
while($rows = mysqli_fetch_assoc($list)){
$cleanedrow = [];
foreach($rows as $key => $value) {
$cleanedrow[strtr($key, $string_filter)] = strtr($value, $string_filter);
}
$data[$rows['id']] = $cleanedrow;
}

How do I change the array value?

Is it possible to change the highlighted word to numbers without changing it in database table?
wanted from this
$value['how']
to this
$value['0']
Yes, you need to use array_values()
$array = array("how" => "how-value", "to" => "to-value");
print_r(array_values($array));
Output:
Array
(
[0] => how-value
[1] => to-value
)
EDIT BY OP
To get the value
foreach($array as $key => $value) {
$someArray = array_values($value);
print_r($someArray[0]);
}
//return 144
#Milan Chheda answer is correct but I am just briefing here so the user can get a better idea of that.
Use array_values() to get the values of an array.
$FormedArray = array_values($your_array);
now print that array print_r($FormedArray);
You will get your result like
Array
(
[0] => 144
[1] => 41
[2] => USA
[3] => 12
[4] => 12
)
Here is a working example. https://eval.in/843720

PHP how to flatten out this indexed array?

I have the following array:
Array
(
[0] => Array
(
[ContractorName] => Joe Soap
[BonusAmount] => 73.92
)
[1] => Array
(
[ContractorName] => Mike Michaels
[BonusAmount] => 68.55
)
[2] => Array
(
[ContractorName] => John Smith
[BonusAmount] => 34.35
)
[3] => Array
(
[ContractorName] => Pete Peterson
[BonusAmount] => 24.61
)
[4] => Array
(
[ContractorName] => Pete Smith
[BonusAmount] => 22.76
)
)
How do I go about ending up with an array that looks like this:
Array
(
[Joe Soap] => 73.92
[Mike Michaels] => 68.55
[John Smith] => 34.35
[Pete Peterson] => 24.61
[Pete Smith] => 22.76
)
I'm a bit lost at the moment. I have tried creating a new array by looping over the first array, but I'm getting unwanted results. Any help greatly appreciated.
Use array_combine with array_column as
array_combine(array_column($records, 'ContractorName'),array_column($records, 'BonusAmount'));
Go through entire array using foreach and then use each piece to construct new array.
$out = [];
foreach ($inputArray as $v) {
$out[$v['ContractorName']] = $v['BonusAmount'];
}
Second solution is by using array_combine and array_column.
$keys = array_column($inputArray, 'ContractorName');
$values = array_column($inputArray, 'BonusAmount');
$output = array_combine($keys, $values);
//Or put everything in single line
$output = array_combine(array_column($inputArray, 'ContractorName'), array_column($inputArray, 'BonusAmount'));
Third option
$output = array_column($inputArray, 'BonusAmount', 'ContractorName');
You can use one array_column(), and with the third parameter to specify the index. Live Demo.
array_column($array, 'BonusAmount', 'ContractorName');

Notice: Array to string conversion in PDO

I have this error which i can't seem to solve.
Here is the code
$this->stmt = $this->handle->prepare("SELECT * FROM tb_ph WHERE totalprofit = 0 AND status = '1' AND checks = ''");
$var = $this->stmt->fetchAll();
$var returns a multi dimensional array which i'd love to loop through so as to get their respective id and usernames.
Now the problem is that if i do
foreach($var as $key => $value){
$id = $value['id'];
$username = $value['username'];
$phone = $value['phone'];
}
i get the Notice: Array to string conversion error.
var_dump($var) gives
Array
(
[0] => Array
(
[id] => 3
[0] => 3
[username] => rose
[1] => rose
[phone] => +2782345578952
[2] => +2782345578952
)
[1] => Array
(
[id] => 4
[0] => 4
[username] => josh
[1] => rose
[phone] => +0182345578952
[2] => +0182345578952
)
)
How do i make the loop get just id, username and phone each time for the two results returned?
look at this bit very closely:
Array
(
[0] => Array
(
It shows you that what you have is an array of arrays so what get in your foreach is another array
foreach($var as $arr) {
/* here $arr is an array in calling print on it will produce the warning you have already seen but ...*/
print $arr[$id];
}
Because you are printing just one element of an array here. Similarly print $arr['username']

php - remove indexes of an aray if they have the same value

How can i make the values of such an array unique.
Array ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume [3] => pen) )
as there is pen twice i would like it to be deleted in this way :
( [0] => Array ( [0] => wallet [1] => pen [2] => perfume) )
OR
( [0] => Array ( [0] => wallet [1] => perfume [2] => pen) )
and i would like it to apply for any length.
thanks for your help
How about array_flip used twice:
$arr = Array(0 => wallet, 1 => pen, 2 => perfume, 3 => pen);
$arr = array_flip(array_flip($arr));
print_r($arr);
output:
Array
(
[0] => wallet
[3] => pen
[2] => perfume
)
If you want to renumbered the indexes, add this ligne after:
$arr = array_values($arr);
if you just want to select a unique value you need to pass the array that you want to compare the values, I am assuming you passed the main array, you need to pass the array where the problem is found which is in your case the index 0 of an array
$result = array_unique($input[0]);
$input will have an array of unique values so pen will not be 2
if you need to delete any duplicated values in the array you can do this.
$input[0] = array_unique($input[0]);
if you need to reset the index you can use this
$new_index = array_values($input[0]);
print_r($new_index);
$tmp = array ();
foreach ($array as $row)
array_push($tmp,array_unique($row));
Here is the solution for multi dimensopnal array
$res = array();
foreach($your_array as $key=>$val){
$res[$key] = array_unique($val);
}
echo "<pre>";
print_r($res);

Categories