I'm trying to create a function that will sort this form of array (originally with dynamic values):
Array
(
[0] => product_cat-24
[1] => style_cat-97
[2] => style_cat-98
[3] => stone_count_cat-110
[4] => style_cat-100
[5] => style_cat-104
[6] => stone_count_cat-109
[7] => stone_count_cat-111
)
So it will look like this:
Array(
'product_cat' => array( 24 ),
'style_cat' => array( 97, 98, 100, 104 ),
'stone_count_cat' => array( 110, 109, 111 )
);
The only thing that matters is to assign the number to its proper key.
Looking for the must elegant way to achieve that.
Thanks! :)
Simply try like this with explode() and list() of PHP.
<?php
$array = array
(
'product_cat-24',
'style_cat-97',
'style_cat-98',
'stone_count_cat-110',
'style_cat-100',
'style_cat-104',
'stone_count_cat-109',
'stone_count_cat-111'
);
$new = array();
foreach($array as $val) {
list($key, $value) = explode('-', $val);
$new[$key][] = $value;
}
print '<pre>';
print_r($new);
print '<pre>';
?>
OUTPUT:
Array
(
[product_cat] => Array
(
[0] => 24
)
[style_cat] => Array
(
[0] => 97
[1] => 98
[2] => 100
[3] => 104
)
[stone_count_cat] => Array
(
[0] => 110
[1] => 109
[2] => 111
)
)
DEMO: https://eval.in/980195
You can also do like this:
$new = array();
foreach( $array as $val) {
$tmp = explode('-', $val);
$new[$tmp[0]][] = $tmp[1];
}
Related
This question already has answers here:
how to change index of an array in php [duplicate]
(3 answers)
Closed 4 years ago.
Good day everyone,
I was tasked to change index in arrays.
here is my code:
$file = Storage::get('upload/test.txt');
$lines = explode('\n', $file);
$array = array_map(function($line) {
return explode(',', $line);
}, $lines);
print_r($array);
output is:
Array
(
[0] => Array
(
[0] => john
[1] => male
[2] => 20
[3] => 200
[4] => 174
)
[1] => Array
(
[0] => joe
[1] => male
[2] => 24
[3] => 157
[4] => 166
)
[2] => Array
(
[0] => bea
[1] => female
[2] => 18
[3] => 153
[4] => 160
)
[3] => Array
(
[0] => edd
[1] => male
[2] => 30
[3] => 180
[4] => 180
)
)
what i need to happen is:
Array
(
[0] => Array
(
[name] => john
[sex] => male
[age] => 20
[height] => 200
[weight] => 174
)
[1] => Array
(
[name] => joe
[sex] => male
[age] => 24
[height] => 157
[weight] => 166
)
[2] => Array
(
[name] => bea
[sex] => female
[age] => 18
[height] => 153
[weight] => 160
)
[3] => Array
(
[name] => edd
[sex] => male
[age] => 30
[height] => 180
[weight] => 180
)
)
thanks in advance! :)
You want an associative array.
$newArray = []; // create a new empty array to store your associative arrays.
// Loop through each element in array.
foreach($array as $aPerson) {
// map each element in array into an associative array.
$person = [
"name" => $aPerson[0],
"sex" => $aPerson[1],
"age" => $aPerson[2],
"height" => $aPerson[3],
"weight" => $aPerson[4]
];
// Add your associative array to your new re-indexed array.
array_push($newArray, $person);
}
I hope this helps.
The following should print the array in the format you want.
print_r($newArray);
Simple, Like this:
$arr = [
['joe','male',24,157,166]
];
#mind the & pass by refrence
foreach($arr as &$item){
$item = array_combine(['name','sex','age','height','weight'],$item);
}
print_r($arr);
Output
Array
(
[0] => Array
(
[name] => joe
[sex] => male
[age] => 24
[height] => 157
[weight] => 166
)
)
Sandbox
Note array_combine will blow up if the 2 arrays are not the same size.
You can do this too (84 bytes)
$arr = [['joe','male',24,157,166]];
$headers = ['name','sex','age','height','weight'];
$arr = array_map(function($item)use($headers){return array_combine($headers,$item);},$arr);
print_r($arr);
Same output
Sandbox
A possible solution will be to create a new array, loop through the current one and add the indexes:
$new_array = array();
foreach ($array as $element) {
$new_array[] = array(
'name' => $element[0],
'sex' => $element[1],
'age' => $element[2],
'height' => $element[3],
'weight' => $element[4]
);
$i++;
}
Here is how I would do what you're trying to achieve
<?php
$fp = #fopen('upload/test.txt', 'r'); $props = ['name', 'sex', 'age', 'height', 'weight']; $r = [];
if($fp) {
while(($line = fgets($fp)) !== false) {
$x = explode(',', $line); $a = [];
foreach($props as $k => $v){
$a[$v] = $x[$k];
}
$r[] = $a;
}
fclose($fp);
}
print_r($r);
?>
I have arrays like this:
Array(
[0] => 85
[1] => 85167920
[2] => ELECTRICAL/ELECTRONIC
[3] => DEVICES
[4] => FOR
[5] => REPELLING
[6] => INSECTS
[7] => (E.G.MOSQUITOES
[8] => ETC)
)
and
Array(
[0] => 85
[1] => 851680
[2] => ELECTRIC
[3] => HEATING
[4] => RESISTORS
)
I want arrays like this:
Array(
[0] => 85
[1] => 851680
[2] => ELECTRIC HEATING RESISTORS
)
and
Array(
[0] => 85
[1] => 85167920
[2] => ELECTRICAL/ELECTRONIC DEVICES FOR REPELLING INSECTS (E.G.MOSQUITOES ETC)
)
I am not sure about what method to use: merge / combine / push.
What should I use?
Use array_slice() and implode()
Method: (Demo)
$array=[85,851680,'ELECTRIC','HEATING','RESISTORS'];
$array=[$array[0],$array[1],implode(' ',array_slice($array,2))];
var_export($array);
Output:
array (
0 => 85,
1 => 851680,
2 => 'ELECTRIC HEATING RESISTORS',
)
This should work fine :
<?php
$a = [
85,
85167920,
'ELECTRICAL/ELECTRONIC',
'DEVICES',
'FOR',
'REPELLING',
'INSECTS',
'(E.G.MOSQUITOES
ETC)'
];
$b = [
85,
851680,
'ELECTRIC',
'HEATING',
'RESISTORS'
];
$aa = array_slice($a, 0,2);
$aa[] = implode(' ',array_slice($a, 2));
$bb = array_slice($b, 0,2);
$bb[] = implode(' ',array_slice($b, 2));
print_r($aa);
echo '<br>';
print_r($bb);
?>
Try below code,
<?php
$array = array();
$temp_array = array();
$temp_str_array=array();
$array[]=85;
$array[]=85167920;
$array[]='ELECTRICAL/ELECTRONIC';
$array[]='DEVICES';
$array[]='FOR';
$array[]='REPELLING';
$array[]='INSECTS';
$array[]='(E.G.MOSQUITOES';
$array[]='ETC)';
foreach($array as $k=>$values){
if($k<=1){
$temp_array[$k]=$values;
}
else{
$temp_str_array[]=$values;
}
if(count($array)==($k+1)){
$temp_array[3]=implode(" ",$temp_str_array);
}
}
echo "<pre>";
print_r($temp_array);
?>
Output,
Array
(
[0] => 85
[1] => 85167920
[3] => ELECTRICAL/ELECTRONIC DEVICES FOR REPELLING INSECTS
(E.G.MOSQUITOES ETC)
)
Hi all i need to merge the same key to convert to single array from multiple array list please any one help me to the problem
for example here the array.
Array
(
[0] => Array
(
[0] => Mr.
[1] => Mrs.
)
[1] => Array
(
[0] => Rob
[1] => Tam
)
[2] => Array
(
[0] => kar
[1] => Man
)
[3] => Array
(
[0] => 55345345345
[1] => 44545345435
)
)
i need the output is
Array
(
[0] => Array
(
[0] => Mr.
[1] => Rob
[2] => kar
[3] => 55345345345
)
[1] => Array
(
[0] => Mrs.
[1] => Tam
[2] => Man
[3] => 44545345435
)
)
Please any one help
Thanks
For PHP version >= 5.5.0 You can use array_column() and array_merge() for this as
$result = array_merge(array_column($records, '0'), array_column($records, '1'));
print_r($result);
$a = array(
0 => array(
0 => 'Mr.',
1 => 'Mrs.'
),
1 => array
(
0 => 'Rob',
1 => 'Tam'
),
2 => array
(
0 => 'kar',
1 => 'Man'
),
3 => array
(
0 => 55345345345,
1 => 44545345435
)
);
$arr1 = array();
foreach($a as $arr)
{
foreach($arr as $key=>$value)
{
$arr1[$key][] = $value;
}
}
echo '<pre>';
print_r($arr1);
Use this one. You can get output same as you want.
try like this
$out = array();
foreach ($arr1 as $key => $value){
$out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)
$title = $array[0];
$firstname = $array[1];
$lastname = $array[2];
$number = $array[3];
$output = array();
for($i=0; $i < count($title); $i++)
{
$output[] = array($title[$i],$firstname[$i],$lastname[$i],$number[$i])
}
var_dump($output);
Consider the below multi dimension array:
Array
(
[submit] => yes
[id] =>
[booking_id] =>
[booking_type_id] => Array
(
[0] => 171
[1] => 58
)
[value] => Array
(
[0] => 23
[1] => 46
)
)
How do I combine it so that that the booking_type_id and value arrays are in one array with the same values:
Array
(
[new_values] => Array
(
[171] => 23
[58] => 46
)
)
I have tried array_merge and array_combine, but I can't get it to keep the keys? I have also tried to loop through and add to a new array.
How did you use array_combine. That should work for the structure you want. Example below:
$multi = array(
'submit' => 'yes',
'id' => '',
'booking_id' => '',
'booking_type_id' => array( 171, 58 ),
'value' => array( 23, 46 ),
);
$combined = array_combine( $multi['booking_type_id'], $multi['value'] );
you can use array_combine() function like this:
$array['new_values'] = array_combine($array['booking_type_id'], $array['new_values']);
A simple solution would be to loop through the booking_type_id array and correlate those values in a new array with:
$array_1['booking_type_id'] = array(171,58);
$array_1['value'] = array(23,46);
$array_2 = array(); // new combined array
foreach ($array_1['booking_type_id'] as $key => $value) {
$array_2[$value] = $array_1['value'][$key];
}
With the result being:
Array
(
[171] => 23
[58] => 46
)
UPDATE:
As others have already noted you can also accomplish the same with array_combine()
$array_2 = array_combine( $array_1['booking_type_id'], $array_1['value'] );
<?php
$i = 0;
$new_values = array();
while($i < count($your_array['booking_type_id']))
{
$new_values['new_values'][$your_array['booking_type_id'][$i]] =
$your_array['value'];
$i++;
}
?>
$arr = array(
'submit' => "yes",
'id' => NULL,
'booking_id' => NULL,
'booking_type_id' => array(
0 => 171,
1 => 58
),
'value' => array(
0 => 23,
1 => 46
)
);
$new_arr = array();
foreach($arr['booking_type_id'] as $key=>$value){
$new_arr[$value] = $arr['value'][$key];
}
$arr['new_values'] = $new_arr;
echo"<pre>";print_r($arr);
Result will be
Array
(
[submit] => yes
[id] =>
[booking_id] =>
[booking_type_id] => Array
(
[0] => 171
[1] => 58
)
[value] => Array
(
[0] => 23
[1] => 46
)
[new_values] => Array
(
[171] => 23
[58] => 46
)
)
I've an array in php something like below
Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
)
Now I want to remove the parents indexes 0,1... and finally want to get all the values (only unique values).
Thanks.
One possible approach is using call_user_func_array('array_merge', $arr) idiom to flatten an array, then extracting unique values with array_unique():
$new_arr = array_unique(
call_user_func_array('array_merge', $old_arr));
Demo. Obviously, it'll work with array of any length.
$startArray = Array
(
[0] => Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
),
[1] => Array
(
[0] => 40173
[1] => 5181
[2] => 385
[3] => 891382
)
);
//Edited to handle more the 2 subarrays
$finalArray = array();
foreach($startArray as $tmpArray){
$finalArray = array_merge($finalArray, $tmpArray);
}
$finalArray = array_unique($finalArray);
Using RecursiveArrayIterator Class
$objarr = new RecursiveIteratorIterator(new RecursiveArrayIterator($yourarray));
foreach($objarr as $v) {
$new_arr[]=$v;
}
print_r(array_unique($new_arr));
Demo
OUTPUT:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
$new_array = array_merge($array1, $array2);
$show_unique = array_unique($new_array);
print_r($show_unique);
array_merge is merging the array's, array_unique is removinge any duplicate values.
Try something like this:
$new_array = array();
foreach($big_array as $sub_array) {
array_merge($new_array, $sub_array);
}
$new_array = array_unique($new_array);
(code not tested, this just a concept)
Try this:
$Arr = array(array(40173, 514081, 363885, 891382),
array(40173,5181, 385,891382));
$newArr = array();
foreach($Arr as $val1)
{
foreach($val1 as $val2)
{
array_push($newArr, $val2);
}
}
echo '<pre>';
print_r(array_unique($newArr));
Output:
Array
(
[0] => 40173
[1] => 514081
[2] => 363885
[3] => 891382
[5] => 5181
[6] => 385
)
Refer: https://eval.in/124240
--
Thanks