How do I change the array value? - php

Is it possible to change the highlighted word to numbers without changing it in database table?
wanted from this
$value['how']
to this
$value['0']

Yes, you need to use array_values()
$array = array("how" => "how-value", "to" => "to-value");
print_r(array_values($array));
Output:
Array
(
[0] => how-value
[1] => to-value
)
EDIT BY OP
To get the value
foreach($array as $key => $value) {
$someArray = array_values($value);
print_r($someArray[0]);
}
//return 144

#Milan Chheda answer is correct but I am just briefing here so the user can get a better idea of that.
Use array_values() to get the values of an array.
$FormedArray = array_values($your_array);
now print that array print_r($FormedArray);
You will get your result like
Array
(
[0] => 144
[1] => 41
[2] => USA
[3] => 12
[4] => 12
)
Here is a working example. https://eval.in/843720

Related

Convert multiple-dimensional array in to one dimensional array by summing the vaues like shown here

Array
(
[0] => Array
(
[0] => 17
[1] => 111
)
[1] => Array
(
[0] => 17
[1] => 10
)
[2] => Array
(
[0] => 20
[1] => 111
)
)
I want to convert this array as:
array
(
[17]=>121
[20]=>111
)
is there any php array function which can do it easily. I know the other way, but want to know if any ready made function can do that or not??
Please Help.
Here I actually wanted to convert
[0] => Array
(
[0] => 17
[1] => 111
)
17 to key and take 111 as value then in next array
[1] => Array
(
[0] => 17
[1] => 10
)
do the same thing get first value 17 as key and add 10 into previous value 111
which is 121 and then
[2] => Array
(
[0] => 20
[1] => 111
)
take 20 as key and assign value 111 to that key so basically, I want first value as a key and second value as value and for all same keys I want to add values as I stated before.
I thought there might be any PHP ready-made function for that as I have seen there are lots of array processing functions available in PHP. Now, I realized there is no any such kind of function available. I can do my own custom code for this purpose but was looking for good logical solution.
No builtin function for that but it is a simple foreach loop. Assume your array is stored in variable $arr;
$return = array();
foreach ($arr as $a) {
$return[$a[0]] += $a[1];
}
echo "<pre>"; print_r($return);
if you are calling multiple times then you can easily write your own function
$arr[0]= array(17,111);
$arr[1]= array(17,10);
$arr[2]= array(20,111);
$return = subArr($arr);
echo "<pre>"; print_r($return);
function subArr($arr) {
$result = array();
foreach ($arr as $a) {
$result[$a[0]] += $a[1];
}
return $result;
}

Handling PHP Array post values and merging - PHP Arrays

I know this won't take much time for experts here. But still please help me out
My Array output is like this
Array ( [0] => 1 [1] => 37 [2] => 1035 ) 1
Array ( [0] => 1 [1] => 37 [2] => 1035 ) mystatusmessage1
Array ( [0] => 4 [1] => 37 [2] => 2925 ) 2
Array ( [0] => 4 [1] => 37 [2] => 2925 ) mystatusmessage2
What I would like to get it is in a single string value like this so that I can insert into database.
1,37,1035,1,mystatusmessage1
4,37,2925,2,mystatusmessage2
How can I achieve that. I'm trying to do with foreach but still I'm not able to do it.
Thanks,
Kimz
use implode function to make string from array for example
if you have array like Array('a','b','c');
implode(',',array('a','b','c') )
will return a,b,c as string
here first argument is your glue by which you want to join string
Here you go.
// Original array
$array = array(0 => 1, 1 => 37, 2 => 1035);
// $_POST array
$_POST = array(1,'mystatusmessage1');
// Jump to the end of array
end($array);
// Merge the post with original array
$newArr = array_merge($array,$_POST);
// Impode
echo implode(",",$newArr);
Repeat with other array.

php - remove indexes of an aray if they have the same value

How can i make the values of such an array unique.
Array ( [0] => Array ( [0] => wallet [1] => pen [2] => perfume [3] => pen) )
as there is pen twice i would like it to be deleted in this way :
( [0] => Array ( [0] => wallet [1] => pen [2] => perfume) )
OR
( [0] => Array ( [0] => wallet [1] => perfume [2] => pen) )
and i would like it to apply for any length.
thanks for your help
How about array_flip used twice:
$arr = Array(0 => wallet, 1 => pen, 2 => perfume, 3 => pen);
$arr = array_flip(array_flip($arr));
print_r($arr);
output:
Array
(
[0] => wallet
[3] => pen
[2] => perfume
)
If you want to renumbered the indexes, add this ligne after:
$arr = array_values($arr);
if you just want to select a unique value you need to pass the array that you want to compare the values, I am assuming you passed the main array, you need to pass the array where the problem is found which is in your case the index 0 of an array
$result = array_unique($input[0]);
$input will have an array of unique values so pen will not be 2
if you need to delete any duplicated values in the array you can do this.
$input[0] = array_unique($input[0]);
if you need to reset the index you can use this
$new_index = array_values($input[0]);
print_r($new_index);
$tmp = array ();
foreach ($array as $row)
array_push($tmp,array_unique($row));
Here is the solution for multi dimensopnal array
$res = array();
foreach($your_array as $key=>$val){
$res[$key] = array_unique($val);
}
echo "<pre>";
print_r($res);

Removing a value from a PHP Array

Using PHP I'm trying to remove an element from an array based on the value of the element.
For example with the following array:
Array
(
[671] => Array
(
[0] => 1
[1] => 100
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
I'd like to be able to specify a value of on of the inner arrays to remove. For example if I specified 100 the resulting array would look like:
Array
(
[671] => Array
(
[0] => 1
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
My first thought was to loop through the array using foreach and unset the "offending" value when I found it, but that doesn't seem to reference the original array, just the loop variables that were created.
Thanks.
foreach($array as $id => $data){
foreach($data as $index => $offending_val){
if($offending_val === 100){
unset($array[$id][$index]);
}
}
}
You can use:
array_walk($your_array, function(&$sub, $key, $remove_value) {
$sub = array_diff($sub, array($remove_value));
}, 100);
Couple of ideas:
You could try array_filter, passing in a callback function that returns false if the value is the one you want to unset. Using your example above:
$new_inner_array = array_filter($inner_array, $callback_that_returns_false_if_value_100)
If you want to do something more elaborate, you could explore the ArrayIterator class of the SPL, specifically the offsetUnset() method.

Check Arrays with different keys?

How do i check if a specific array key exist and how to compare them?
1. Array looks like this.
[33] => Array
(
[211] =>objectr
(
[name] => Test
[id]=> 211
)
)
[23] => Array
(
[311] =>objectr
(
[name] => Tester
[id]=> 311
)
)
2. Array looks like this
[0] => 311
[1] => 211
[2] => 99
Now i need to compare them and get the id of them.
What im looking for is something like that
[0] => Tester
[1] => Test
How do i do that?
array_key_exists - http://php.net/manual/en/function.array-key-exists.php
foreach($first_array as $arr) {
foreach($second_array as $key=>$val)
{
if (array_key_exists($val, $first_array)) {
$final_array[$key] = $arr['name'];
}
}
}
or array_search - http://uk.php.net/array_search
foreach($first_array as $arr) {
foreach($second_array as $val)
{
$key = array_search($val, $arr);
if($key !== false) $final_array[$key] = $arr['name'];
}
}
In both cases you should end up with:
[0] => Tester
[1] => Test
I would transform Array 1 like removing the outer key (at least temporarily) then while iterating through Array 2, i'd compare against transformed Array 1 with array_key_exists.
I hope I understood your question, there might be a language barrier, but here we go:
so basically you have 3 arrays and you want to use the last to to check against the first one to see if those values/keys exists in the first? Well the firs thing you want to do is re structure your first array into something that can easily be translated for checking the values and keys of the next two arrays. so lets call the first array $mapArray:
foreach($mapArray as $mapObject){
foreach($mapObject as $object){
$mapList[$object->id] = $object->name;
}
}
Now this should give us something like:
[211] => 'test'
[311] => 'tester'
So now lets call the 2nd array $arrayIds and the 3rd $arrayNames. To see if am id exists and to get its name when given the array $arrayIds, all you need to do is this:
//given [0] => 311
$keyExists = array_key_exists(311, $mapList); //returns true is the key exists
echo $mapList[311]; //returns tester, the name for the id given
And the other way around:
//given [0] => 'test'
$nameExists = in_array('test', $mapList);
if($nameExists) echo array_search('test', $mapList); // returns 211
hope this is what you are looking for or at least helps you find what you are looking for.
Another approach: We reduce the first array to one dimension:
$first = array();
foreach($first_array as $val) {
$first[key($val)] = current($val);
}
gives:
Array
(
[211] => Array
(
[name] => Test
[id] => 211
)
[311] => Array
(
[name] => Tester
[id] => 311
)
)
(I used an array instead of an object but it works the same).
and then we compute the intersection of the keys:
//assume
$second_array = array(311, 99);
$result = array_intersect_key($first, array_flip($second_array));
which gives:
Array
(
[311] => Array
(
[name] => Tester
[id] => 311
)
)
So it is not quite what you want but you can easily access the name property via $element->name.

Categories