how to edit array in php - 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.

Related

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

PHP - Get number of items in array

OK, so I got a while loop, in which I loop through different users:
$before = $data['autorenew_before'];
$refs=$dbh->prepare("SELECT * FROM users WHERE user_by=:userby AND expire <= unix_timestamp(CURRENT_TIMESTAMP + INTERVAL :before day)");
#$refs->bindParam(":userby",$data['username']);
$refs->bindParam(":userby",$userdata['username']);
$refs->bindParam(":before",$before);
$refs->execute();
I then loop through the above query:
while($refsData=$refs->fetch()){
$ids = "".$refsData['id'].",";
$explode = explode(",",$ids);
$outcome = _paying(number_format(getPriceList($data['rented_referrals']),2), 30, $data['username'], $explode);
}
This is the _paying function:
function _paying($ceny, $dni, $username, $referrals_array){
//$ceny = 0.20
//Count referrals_array doesn't return anything.
$koszyk = $ceny * count($referrals_array);
return $koszyk;
}
The above function doesn't work, as the count($referrals_array) is not working.
Edit - 1:
print_r($explode); gives me:
Array
(
[0] => 40231
[1] =>
)
Array
(
[0] => 40232
[1] =>
)
Array
(
[0] => 40233
[1] =>
)
Array
(
[0] => 40234
[1] =>
)
Array
(
[0] => 40235
[1] =>
)
Array
(
[0] => 55847
[1] =>
)
Array
(
[0] => 55848
[1] =>
)
Array
(
[0] => 90322
[1] =>
)
Array
(
[0] => 90323
[1] =>
)
Array
(
[0] => 90324
[1] =>
)
Array
(
[0] => 90325
[1] =>
)
Array
(
[0] => 90326
[1] =>
)
What am I doing wrong?
Have you checked the content of $explode yet?
Try a print_r($explode); before calling _paying() to make sure, there is data.

How to sum same field values in multidimentional array

I have to admit that understanding the tables are big challenge for me so please dont judge me so hard...
This is my array:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
[2] => Name1
)
[ids] => Array
(
[0] => 1
[1] => 2
[2] => 1
)
[quantities] => Array
(
[0] => 255
[1] => 2
[2] => 467
)
)
And i wish to sum "quantities" where names or ids are the same.
Example output should be:
Array
(
[names] => Array
(
[0] => Name1
[1] => Name2
)
[ids] => Array
(
[0] => 1
[1] => 2
)
[quantities] => Array
(
[0] => 722
[1] => 2
)
)
I know there is a function like "array_reduce" but don't know how to use it.
Thanks for help!
try this
$result = [];
foreach($array['ids'] as $key=>$val ){
if(array_key_exists($val, $result)){
$result[$val]['sum_quantity'] += $array['quantities'][$key];
}
else{
$result[$val]['sum_quantity'] = $array['quantities'][$key];
$result[$val]['name'] = $array['names'][$key];
$result[$val]['id'] = $array['ids'][$key];
}
}
and output will be like this
Array
(
[1] => Array //array key = id
(
['name'] => Name1,
['sum_quantity'] => 722,
['id'] => 1
)
[2] => Array
(
['name'] => Name2,
['sum_quantity'] => 2,
['id'] => 2
)
)
You can do this way :
$testArray['names'][0]='name1';
$testArray['names'][1]='name2';
$testArray['names'][2]='name1';
$testArray['ids'][0]=1;
$testArray['ids'][1]=2;
$testArray['ids'][2]=1;
$testArray['quantities'][0]=255;
$testArray['quantities'][1]=2;
$testArray['quantities'][2]=467;
echo "<pre>";
print_r($testArray);
$unqArray['names']=array();
$unqArray['ids']=array();
$unqArray['quantities']=array();
foreach($testArray['ids'] as $key=>$value)
{
if(!in_array($value,$unqArray['ids']))
{
$unqArray['names'][]=$testArray['names'][$key];
$unqArray['ids'][]=$testArray['ids'][$key];
$quantity=0;
foreach($testArray['ids'] as $keyId=>$valueId)
{
if($valueId==$value)
{
$quantity+=$testArray['quantities'][$keyId];
}
}
$unqArray['quantities'][]=$quantity;
}
}
print_r($unqArray);
echo "</pre>";

How to explode ans change the array values into key?

I am having an array like this.
Array
(
[0] => Array
(
[0] => a~226
[1] => a~228
)
[1] => Array
(
[0] => b~123
[1] => b~209
)
[2] => Array
(
[0] => c~161
[1] => c~140
)
)
I want to explode this array using ~ symbol and i want value to be a key in php array.i want an array like this.Kindly help me write the code.
Array
(
[0] => Array
(
[a] => 226
[a] => 228
)
[1] => Array
(
[b] => 123
[b] => 209
)
[2] => Array
(
[c] => 161
[c] => 140
)
)
Thanks in advance...
You cannot have such an array.
The keys must be unique (Like Mark Baker say).
You can have something like this:
Array
(
[a] => Array
(
[0] => 226
[1] => 228
)
[b] => Array
(
[0] => 123
[1] => 209
)
[c] => Array
(
[0] => 161
[1] => 140
)
)
The code to do this:
$array = array(
array("a~226", "a~228"),
array("b~123", "b~209"),
array("c~161", "c~140")
);
$result = array();
foreach($array as $inner_array) {
foreach($inner_array as $value) {
$spitted = explode("~", $value);
$result[$spitted[0]][] = end($spitted);
}
}
An working example: http://codepad.viper-7.com/znhhqB
try this
$arr_new = array();
foreach($arr_main as $key=>$arr)
{
foreach($arr as $k=>$val)
{
$str = explode("~",$val);
$arr_new[$key][$str[0].$k]=$str[1];
}
}
will maintain the index as a0, a1

PHP - Array result

I have a big problem with a array and I want to group by key (or I don't now :( )
I get the links from mySQL and I do the following:
$lien2 = $links2['text'];
$lien2 = stripslashes($lien2);
$lien2 = htmlspecialchars($lien2);
$lien2 = nl2br($lien2);
preg_match_all('#http://.*?\.?([a-zA-Z0-9\-]+)(\.[a-zA-Z0-9]+)/[a-zA-Z0-9\/\*\-\?\&\%\=\,\.\;\#\_]+#i', $lien2, $lien2_result, PREG_SET_ORDER);
This is the array $lien2_result:
Array
(
[0] => Array
(
[0] => links1
[1] => A
)
[1] => Array
(
[0] => links2
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links3
[1] => C
)
)
Array
(
[0] => Array
(
[0] => links4
[1] => B
)
)
Array
(
[0] => Array
(
[0] => links5
[1] => D
)
)
Array
(
[0] => Array
(
[0] => links6
[1] => E
)
)
and I want to get the following result:
A
links1
B
links2
links4
C
links3
D
links5
E
links6
I would adjust the query personally but if you are stuck with this resultset you could rewrite it with
foreach($lien2_result as $lien2){
foreach($lien2 as $item){
$arr[$item[1]][] = $item[0];
}
}
where print_r($arr) would result something like:
$arr = Array('A' => Array('links1'), 'B' => Array('links2','links4')); //and so on..
And actual printing the way you asked:
foreach($arr as $name => $value){
echo($name.'<br />');
foreach($value as $item){
echo($item.'<br />');
}
}
EDIT
Here's an example
http://sandbox.onlinephpfunctions.com/code/c0da893797cb2049e8346168b280a9f5b1fa145b

Categories