How to insert values in assosiative array with mysqli_fetch_array() [duplicate] - php

This question already has answers here:
get array of rows with mysqli result
(2 answers)
Closed 2 years ago.
I want to make array with key 'barang' that contains ids of barang in my database.
Here my code
while ($globalBarang = mysqli_fetch_array($sqlBarang)) {
$arrayglobal["barang"] = array($globalBarang['id_brg']);
}
for now with print_r() it result
Array ( [barang] => Array ( [0] => SSB-001 ) )
What I want:
Array ( [barang] => Array ( [0] => BRS-001,
[1] => GLP-001,
[2] => SSB-001 ) )
Here my database if needed:
Database:

You just need to change the assignment, add each element with [] (and not as an array on the right)...
while ($globalBarang = mysqli_fetch_array($sqlBarang)) {
$arrayglobal["barang"][] = $globalBarang['id_brg'];
}

Related

On array How can i select only column [duplicate]

This question already has answers here:
Remove unwanted elements from subarrays in multidimensional array
(3 answers)
Preserve elements in each row of a two-dimensional array based on another array
(7 answers)
Get multi column from array php, Alternate of array_column [duplicate]
(2 answers)
How can I access an array/object?
(6 answers)
Closed 7 months ago.
I have an array like the one below in laravel which I want to fetch the column.
Array
(
[0] => Array
(
[table] => form_identification
[column] => region
)
[1] => Array
(
[table] => form_identification
[column] => province
)
)
And I want the output like this
Array
(
[0] => Array
(
[column] => region
)
[1] => Array
(
[column] => province
)
)
Consider your first array as $datas variable here and we are using for each whole array
foreach($datas as $data) {
$result[] = array('column' => $data['column']);
}
print_r($result);

how to simplify an array of arrays with single object attribute [duplicate]

This question already has answers here:
how get each single column data from php multidimensional array?
(2 answers)
Closed 2 years ago.
I have the following data structure:
$campaigns =
Array
(
[0] => Array
(
[subject] => cca-cpg
)
[1] => Array
(
[subject] => cleanup-cpg
)
[2] => Array
(
[subject] => gas-cpg
)
[3] => Array
(
[subject] => pollinators-cpg
)
)
what I would like to end up with is:
$campaigns = ['cca-cpg','clean_up-cpg','gas-cpg','pollinators-cpg'];
this will work:
$newCampaigns=[];
for($i=0;$i<count($campaigns);$i++){
array_push($newCampaigns,$campaigns[$i]['subject'];
}
but I was wondering if there's a better way to do this. The data is coming directly from a mysql database
There's array_column() function for you:
$newCampaigns = array_column($campaigns, 'subject');
Source: https://www.php.net/manual/en/function.array-column.php

How can I count total data wrong when compare between array with another array? [duplicate]

This question already has answers here:
difference between two arrays
(7 answers)
How to counting varians data from array?
(2 answers)
Closed 5 years ago.
How can I count total data wrong when compare between array with another array?
example input like this.
$data= array('1','2','5','7');
I want to compare the value with this value in variable $data2 the output of variable $data2 like this :
Array
(
[0] => Array
(
[kalimat] => 1
)
[1] => Array
(
[kalimat] => 2
)
[2] => Array
(
[kalimat] => 5
)
[3] => Array
(
[kalimat] => 6
)
[4] => Array
(
[kalimat] => 7
)
)
How can I count total wrong data in $data where compare with $data2 ?
Have you tried the array_diff function?
http://php.net/manual/en/function.array-diff.php

Loop through multiple arrays php [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 7 years ago.
Can anybody explain me how can i get the value of 'ubicacion' in those arrays, and then, store that value in a php variable.
Array
(
[0] => Array
(
[PC] => Array
(
[ubicacion] => 02021002
)
)
)
Array
(
[0] => Array
(
[PC] => Array
(
[ubicacion] => 034267211
)
)
)
This will give you the value of it..
$value = $array[0]['PC']['ubicacion'];

Remove all keys from an associative array in order to get a simple no key all-value array [duplicate]

This question already has answers here:
getting array values without foreach loop [duplicate]
(2 answers)
Closed 8 years ago.
Here is the sort of array I have for instance:
Array (
[0] => Array ( [id] => 21 )
[1] => Array ( [id] => 24 )
)
and I simply want to have
Array(21,24)
How can I do it ?
Try this
foreach($array as &$element){ $element = $element['id']; }

Categories