On array How can i select only column [duplicate] - php

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);

Related

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

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'];
}

How to set array of array in single array in php [duplicate]

This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 3 years ago.
Following is my array
Array
(
[0] => Array
(
[1] => Beanie
)
[1] => Array
(
[2] => Cap
)
[2] => Array
(
[1] => Beanie with Logo
)
)
I want this into a single array like set key and value of array.
array(
[1]=>Beanie,
[2]=>Cap,
[1]=>Beanie with Logo,
)
Demo Link.
You can do,
array_merge(...$arr);
Splat operator will expose to all the values of array internally, to flatten array.
Your expected output is not possible in any coding language with
arrays.
Note: Indexes are mean to be unique, don't issue over its uniqueness :)

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