Converting a single array into multi array - php

I have an array which looks like this below:
Array ( [:status0] => 1 [:status1] => 2 )
I would want to convert it into something like this:
Array ( [:status0] => Array ( [0] => 1 [1] => 1 ) [:status1] => Array ( [0] => 2 [1] => 1 ) )
i want to do this with flexibility because the number of the array and names are random. I was thinking of using a for loop something like this:
foreach ($newParam as $row){
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
But i cant use this in my case, please help

This code should do what you want:
$newParam = Array ( ':status0' => 1, ':status1' => 2 ) ;
foreach ($newParam as $key => $value) {
$newArray[$key] = array($value, 1);
}
print_r($newArray);
Output:
Array (
[:status0] => Array (
[0] => 1
[1] => 1
)
[:status1] => Array (
[0] => 2
[1] => 1
)
)
Demo on 3v4l.org

Related

Php how to create custom array using 2 arrays

I need some help on below array formation. I want to create a custom array using 2 arrays.
This is my first array :-
Array
(
[0] => Array
(
[0] => 26
[1] => 0.0000000000000000
)
[1] => Array
(
[0] => 25
[1] => 0.0000000000000000
)
[2] => Array
(
[0] => 24
[1] => 0.0000000000000000
)
)
This is my second array :-
Array
(
[0] => Array
(
[0] => 24
)
[1] => Array
(
[0] => 26
)
)
I want final array as below. Can someone please suggest how to form this array.
Array
(
[0] => Array
(
[0] => 26
[1] => 0.0000000000000000
)
[2] => Array
(
[0] => 24
[1] => 0.0000000000000000
)
)
I have used below but I want it without foreach.
$finalArray = array();
foreach ($secondArray as $key => $value) {
$key = array_search($value[0], array_column($firstArray, 0));
$finalArray[] = $firstArray[$key];
}
You can use array_filter():
$finalArray = array_filter($firstArray,
fn($item) => in_array($item[0], array_column($secondArray, 0))
);
It could be more efficiant to compute allowed values first:
$allowedValues = array_column($secondArray, 0);
$finalArray = array_filter($firstArray, fn($item) => in_array($item[0], $allowedValues));
Before PHP 7.4, you have to use use() to pass variable to the anonymous function:
$finalArray = array_filter($firstArray,
function($item) use($secondArray) {
return in_array($item[0], array_column($secondArray, 0));
}
);

Array structure rearrangement without loop

I have an array like mentioned below, which I want to rearrange without using loop:
Array
(
[0] => Array
(
[Books] => Array
(
[id] => 4
)
)
[1] => Array
(
[Books] => Array
(
[id] => 3
)
)
[2] => Array
(
[Books] => Array
(
[id] => 2
)
)
[3] => Array
(
[Books] => Array
(
[id] => 1
)
)
)
I want an output like this:
Array(4,3,2,1)
I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.
in this case, you can use array_map:
$result = array_map(function($item){
return $item['books']['id'];
}, $currentArray);
OR
if you do not even want that:
$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');

I want replace index array. example $array[x][y] to $array[y][x]

Example. I have:
Array (
[0] => Array (
[comments_id] => 1
[comments_text] => blabla1
)
[1] => Array (
[comments_id] => 2
[comments_text] => blabla2
)
)
I want have:
Array (
[comments_id] => Array (
[0] => 1
[1] => 2
)
[comments_text] => Array (
[0] => blabla1
[1] => blabla2
)
In simplified wants to replace
$array[x][y] to $array[y][x]
I writing in php.
you can do it like this
// the final array which will hold your result array
// considering $results contains your previous array
$final_array = array();
foreach($results as $result) {
$final_array['comments_id'][] = $result['comments_id'];
$final_array['comments_text'][] = $result['comments_text'];
}

Bulid for loop based on given array

I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā€¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}

how to count the array values with key

I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)

Categories