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

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

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

Get values from array in php [duplicate]

This question already has answers here:
How to echo single value from multidimensional array when key is numeric?
(4 answers)
Closed 3 years ago.
I have output like this:
[0] => Array
(
[voice] => Napisy
[quality] => 720
[source] => YouTube
)
[16] => Array
(
[voice] => Napisy
[quality] => 720
[source] => Mega
)
And I want to get source to get output [Youtube, Mega]. Anyone know how to get this without foreach?
You can get source from whole array into another array using array_column.
your above array is $array
$source = array_column($array, 'source');
print_r($source);

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

PHP Randomly select an array from an multidimensional array [duplicate]

This question already has answers here:
Get random item from array [duplicate]
(4 answers)
multi dimensional array in random order
(4 answers)
Closed 9 years ago.
So I have this array of objects. From which I want to take at random one of the objects from the array, and use it for its intended purpose. I have tried array_rand() but that only returned a random value from one of the arrays within. Is there a method similar to array_rand() that will let me use the whole array as the variable rather than a value pluked from within it?
Example Array:
Array
(
[0] => stdClass Object
(
[id] => 10003
[state] => CA
)
[1] => stdClass Object
(
[id] => 10003
[state] => CA
)
[2] => stdClass Object
(
[id] => 10006
[state] => CA
)
)
What I want to do when doing something similar to array_rand() is end up with a variable that is
[0] => stdClass Object
(
[id] => 10006
[state] => CA
)
or similar
From array_rand documentation:
[array_rand] picks one or more random entries out of an array, and returns the
key (or keys) of the random entries.
To summarize: if you want to retrieve a random value from an array, you need to use the random key provided by array_rand to access it.
Solution, assuming your array is stored in $obj:
$random_obj = $obj[array_rand($obj));

Categories