set array value as a variable using php - php

I am try to set one value to another array.
I have this type of two arrays.
Array
(
[0] => test1
[1] => test2
)
Array
(
[0] => 351
[1] => 352
[2] => 353
[3] => 354
[4] => 355
[5] => 356
)
Now I want to do something like set the first three values of the second array on test1, and set another three values from the second array to test2.
test1 = 351,352,353
test2 = 354,355,356
Is it possible?

Try this :
$var = array(0=> "test1",1=> "test2");
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);
$res = array_combine($var,array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3)));
echo "<pre>";
print_r($res);
Output :
Array
(
[test1] => 351,352,353
[test2] => 354,355,356
)
EDIT : As per comment "this type output i need Array ( [0] => 351,352,353 [1] => 354,355,356 )"
$vals = array(0 => 351,1 => 352,2 => 353,3 => 354,4 => 355,5 => 356);
$res = array_map('implode', array_fill(0, count(array_chunk($vals,3)), ','), array_chunk($vals,3));
echo "<pre>";
print_r($res);
Output:
Array
(
[0] => 351,352,353
[1] => 354,355,356
)

Related

how to edit array in php

I have array $array["rlist"] that outputs:
Array (
[0] => Array ( [0] => test1 )
[1] => Array ( [0] => test2 )
[2] => Array ( [0] => test3 )
[3] => Array ( [0] => test4 )
[4] => Array ( [0] => test5 )
)
When I try to edit like so:
$array["rlist"][0][0] = 'test1';
$array["rlist"][0][1] = 'test2';
$array["rlist"][0][2] = 'test3';
$array["rlist"][0][3] = 'test4';
$array["rlist"][0][4] = 'test5';
I get
Array (
[0] => Array (
[0] => test1
[1] => test2
[2] => test3
[3] => test4
[4] => test5
)
)
What am I doing wrong?
it's expected because the element is in
array[0][0]
array[1][0]
array[2][0]
array[3][0]
array[4][0]
so if you want to edit then:
$array["rlist"][0][0] = 'test1';
$array["rlist"][1][0] = 'test2';
$array["rlist"][2][0] = 'test3';
$array["rlist"][3][0] = 'test4';
$array["rlist"][4][0] = 'test5';
If you format your original output, you would see the proper formatting you need...
Array (
[0] => Array ( [0] => test1 )
[1] => Array ( [0] => test2 )
[2] => Array ( [0] => test3 )
[3] => Array ( [0] => test4 )
[4] => Array ( [0] => test5 )
)
You can achieve this by setting each item seprately...
$array = [];
$array['rlist'][][] = 'test1';
$array['rlist'][][] = 'test2';
...
or set them in 1 chunk...
$array = [];
$array['rlist'] = [
['test1'],
['test2'],
['test3'],
['test4'],
['test5']
];
You have an array like this:
$array["rlist"] =
Array ( [0] => Array ( [0] => 'test1' ) ,
[1] => Array ( [0] => 'test2' ) ,
[2] => Array ( [0] => 'test3' ) ,
[3] => Array ( [0] => 'test4' ) ,
[4] => Array ( [0] => 'test5' )
)
The key is [$i++] and the value is an array, so you can edit like this :
$array["rlist"][0][0] = 'test1';
$array["rlist"][1][0] = 'test2';
$array["rlist"][2][0] = 'test3';
$array["rlist"][3][0] = 'test4';
$array["rlist"][4][0] = 'test5';
To avoid handwriting the 2d structure of single-element rows, you can hydrate a flat array with array_chunk() with 1 element per chunk.
Code: (Demo)
$tests = ['test1', 'test2', 'test3', 'test4', 'test5'];
var_export(
array_chunk($tests, 1)
);
Although it is not clear why you need to edit your array.

Change the Array Format

I have a page where it prints out this value:
"Firstname","Ana","George","Wilson"
"Lastname","Smith","Spencer","Carey"
"Age","18","20","22"
I get the values of these using file_get_contents and str_getcsv.
$array= str_getcsv($test);
The Array results that I get is this
Array ( [0] =>
"Firstname"
[1] => 'Ana'
[2] => 'George'
[3] => 'Wilson'
"Lastname"
[4] => 'Smith'
[5] => 'Spencer'
[6] => 'Carey'
"Age"
[7] => 18
[8] => 20
[9] => 22
))
Is there anyway I can change the Array format into this?
Array
(
[0] => Array
(
[0] => 'Ana'
[1] => 'George'
[2] => 'Wilson'
)
[1] => Array
(
[0] => 'Smith'
[1] => 'Spencer'
[2] => 'Carey'
)
[2] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
Instead of using file_get_contents() and str_getcsv(), i will recomend you to use file()
Do like below:-
<?php
$test = file('test.txt'); // you can add your file url here
echo "<pre/>";print_r($test);// initial array
foreach($test as &$te){
$new_array = explode(',',$te);
unset($new_array[0]);
$te = array_values($new_array);
}
echo "<pre/>";print_r($test); // modified and desired array
To change the given array to a specified format:
$array = ['Firstname' => ['Ana', 'George', 'Wilson'], 'Lastname' => ['Smith', 'Spencer', 'Carey'], 'Age' => [18, 20, 22]];
$new_array = [];
foreach ($array as $key => $value) {
$new_array[] = $value;
}
print_r($new_array);
Array
(
[Firstname] => Array
(
[0] => Ana
[1] => George
[2] => Wilson
)
[Lastname] => Array
(
[0] => Smith
[1] => Spencer
[2] => Carey
)
[Age] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
Try This bro i hope i can help:
$array = array(0=> array(
'firstname' => array(1 => 'Ana', 2 => 'George', 3=>'Wilson'),
'lastname' => array(4=>'Smith', 5=>'Spencer', 6=> 'Carey'),
'age' => array(7=>18,8=>20,9=>22)
));
$newArray = array();
echo "<pre/>";print_r($array);
foreach($array as $key=>$new){
foreach($new as $k=>$d){
$newArray[] = array_values($d);
}
}
echo "</br>New Array Format: </br>";
echo "<pre/>";print_r($newArray);

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
)

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

PHP remove the first index of an array and re-index

I have an array like
Array
(
[0] => A
[2] => B
[4] => C
[6] => D
)
I want to remove the first element and then re-index array to get the output
(
[0] => B
[1] => C
[2] => D
)
Which PHP function i need to use?
Update
Input array is
Array
(
[0] => Array
(
[0] => Some Unwanted text
[1] => You crazyy
)
[2] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[10] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
And the output should be like
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
You can use
array_shift($array)
Documentation for array_shift
With array_splice.
http://www.php.net/manual/en/function.array-splice.php
php > print_r($input);
Array
(
[0] => A
[2] => B
[4] => C
[6] => D
)
php > array_splice($input, 0, 1);
php > print_r($input);
Array
(
[0] => B
[1] => C
[2] => D
)
we can do it with array_shift() which will remove the 1st index of array and after that use array_values() which will re-index the array values as i did not get from the #User123's answer, try below one:
<?php
$array = array(
0 => "A",
2 => "B",
4 => "C",
6 => "D"
);
array_shift($array);
$array = array_values($array);
echo "<pre>";
print_r($array);
Output: check the output here https://eval.in/837709
Array
(
[0] => B
[1] => C
[2] => D
)
Same for your Updated Input array
<?php
$array = array(
0 => array(
0 => "Some Unwanted text",
1 => "You crazyy"
),
2 => array(
0 => "My belowed text",
1 => "You crazyy"
),
10 => array(
0 => "My loved quote",
1 => "You crazyy"
)
);
array_shift($array);
$array = array_values($array);
echo "<pre>";
print_r($array);
Output: check the output here https://eval.in/837711
Array
(
[0] => Array
(
[0] => My belowed text
[1] => You crazyy
)
[1] => Array
(
[0] => My loved quote
[1] => You crazyy
)
)
You can cut the array as many many index as you want
$newArray = array_splice($oldArray, $startIndex, $lengthToSlice);
$array=array(
0 => 'A',
2 => 'B',
4 => 'C',
6 => 'D'
);
unset($array[0]);
$array = array_values($array);
print_r($array);
This is also another solution to this issue using unset
Output:
Array
(
[0] => B
[1] => C
[2] => D
)

Categories