Laravel, how to change array key permanently? - php

Tried using array_values but it only temporary.
controller
foreach($rows as $key => $value)
{
array_values($value);
//dd shows the key changes to [0], [1], [2] and so on
}

You can do it like this,
$rows = array_map(function($v){return array_values($v);}, $rows);

Something like this should work:
$new = [];
foreach($rows as $key => $value)
{
array_values($value);
$sub = [];
foreach ($value as $subKey => $subValue) {
$subKey = $key;
$sub[$key] = $subValue;
}
$new[$key] = $sub;
//dd shows the key changes to [0], [1], [2] and so on
}
Then return $new instead of $rows.

Since you're using laravel you can also do:
$rows = collect($rows)->map(function ($value) {
return Arr::accessible($value)?collect($value)->values()->all():$value;
})->all();

If you are trying to change the associative array to an indexed array, do this:
$array = array_values($array);

Related

PHP Transform mixed sequential and associative array to associative array

I would like to build $goal array from $initial only. Any ideas? Thank you
Edit : the question could be how to differentiate associative parts from sequential ones.
$intial=[
"one",
"two"=>"myTwo",
"three",
"four"=>"myFour"
];
$goal=[
"one"=>null,
"two"=>"myTwo",
"three"=>null,
"four"=>"myFour"
];
The 'sequential' parts will have numeric keys, so if your 'associative' keys will always be strings, you could use that to differentiate:
$goal = [];
foreach ($initial as $key => $value) {
if (is_numeric($key)) {
$goal[$value] = null;
} else {
$goal[$key] = $value;
}
}
$goal = [];
foreach($initial as $key => $val){
if(isset($val){
$goal[$key] = $val;
}else{
$goal[$key] = $key;
}
}

Always getting last value of array

Why am I getting always the last value from the array? I'm stuck on what I'm doing wrong.
Please help! :p
$array = array("id"=>"4", "id"=>"5", "id"=>"6");
foreach($array as $key => $value) {
$screenshots[] = $value;
}
var_dump($screenshots);
As Rizier said, your keys in an associative array cannot have the same name.
This should work:
$array = array("id1"=>"4", "id2"=>"5", "id3"=>"6");
foreach($array as $key => $value) {
$screenshots[] = $value;
}
var_dump($screenshots);

Compare two array and delete value from array

I have two array.
1st array is $newarray = ('489289', '536516', '332833', '536516')
2nd array is
$rockin = array(
'489289' => array('536516','value1'),
'332833' => array('536516'),
);
I want to delete some value of $newarray.
Suppose we are looping from $newarray
Initially 489289 is assigned value.
I want to check whether the value associated to 489289 from $rockin array (i.e. value1 or 536516) also exist in $newarray.
If there is exist 'value1' or '536516' in $newarray then, delete 489289 from array!
So in above case 489289 would be deleted (from $newarray)
AS 536516 is associated value of 489289 in $rockin array AND 536516 also exist in $newarray
Till now I have tried this code
foreach ($newarray as $group_id) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if($group_id==$myfrickingleader)
{
unset($newarray[$group_id]);
}
}
}
}
This is what I understood you want to do:
$newarray = array('489289', '536516', '332833', '536516');
$rockin = array(
'489289' => array('536516','332833'),
'332833' => array('536516'),
);
foreach ($rockin as $array) {
foreach ($array as $value) {
if (in_array($value, $newarray)) {
$key = array_search($array, $rockin);
$newarray = array_diff($newarray, array($key));
}
}
}
foreach ($newarray as $k => $v) {
if(is_array($rockin[$v])){
foreach ($rockin[$v] as $key => $value) {
if(in_array($value, $newarray)){
unset($newarray[$k]);
}
}
}
}
You're using $group_id as a key, but it's a value. You have to unset by key, like this:
foreach ($i = 0; $i < count($newarray); $i++) {
foreach ($rockin as $myfrcikingcl) {
foreach ($myfrickingcl as $myfrickingleader) {
if ($newarray[$i] == $myfrickingleader) {
unset($newarray[$i]);
}
}
}
}

How to change first char of each key in array?

I have an array with all keys in lover case and i need to change them that the firs char would be in uppercase, like ucfirs function does. Is it possible without creating a new array?
It's not possible without creating a new array, but here's a funky one-liner you could use:
$array = array_combine(
array_map('ucfirst', array_keys($array)),
array_values($array)
);
It breaks up the array into keys and values, transforms the keys and then glues the two pieces back together.
Try this code:
foreach ($array as $key => $value) {
unset ($array[$key]);
$array[ucfirst($key)] = $value;
}
try this
foreach ($arr as $key=>$val){
unset($arr[$key]);
$key = ucfirst($key);
$arr[$key]=$val;
}
try this. it will work for nested array too.
<?php
function ucfirstKeys(&$data)
{
foreach ($data as $key => $value)
{
// Convert key
$newKey = ucfirst($key);
// Change key if needed
if ($newKey != $key)
{
unset($data[$key]);
$data[$newKey] = $value;
}
// Handle nested arrays
if (is_array($value))
{
ucfirstKeys($data[$key]);
}
}
}
$test = array('foo' => 'bar', 'moreFoo' => array('more' => 'foo'));
ucfirstKeys($test);
print_r($test);

It is possible sum values by keys? how do it (php)

Array for example
$array = array(
array('first'=>5), array('first'=>4), array('second'=>3)
);
How sum values by keys for result:
$result = array(
'first'=>9,
'second'=>3
);
you can iterate array $array and do whatever you want. And you can do it with any language if you have some pprogramming skills.
$result=array();
foreach ($array as $sub) {
foreach ($sub as $key => $value) {
if (isset($result[$key])) $result[$key] += $value;
else $result[$key]=$value;
}
}
Note: if you getting this info from database, it is better to sum it using database resources.
You can use array_walk_recursive:
$results = array();
array_walk_recursive($array, function($number, $key){
global $results;
if (! isset($results[$key])) $results[$key] = 0;
$results[$key] += $number;
});
This works in php >= 5.3

Categories