I have a multidimensional array:
$array=array( 0=>array('text'=>'text1','desc'=>'blablabla'),
1=>array('text'=>'text2','desc'=>'blablabla'),
2=>array('text'=>'blablabla','desc'=>'blablabla'));
Is there a function to return a monodimensional array based on the $text values?
Example:
monoarray($array);
//returns: array(0=>'text1',1=>'text2',2=>'blablabla');
Maybe a built-in function?
This will return array with first values in inner arrays:
$ar = array_map('array_shift', $array);
For last values this will do:
$ar = array_map('array_pop', $array);
If you want to take another element from inner array's, you must wrote your own function (PHP 5.3 attitude):
$ar = array_map(function($a) {
return $a[(key you want to return)];
}, $array);
Do it like this:
function GetItOut($multiarray, $FindKey)
{
$result = array();
foreach($multiarray as $MultiKey => $array)
$result[$MultiKey] = $array[$FindKey];
return $result;
}
$Result = GetItOut($multiarray, 'text');
print_r($Result);
Easiest way is to define your own function, with a foreach loop. You could probably use one of the numerous php array functions, but it's probably not worth your time.
The foreach loop would look something like:
function monoarray($myArray) {
$output = array();
foreach($myArray as $key=>$value) {
if( $key == 'text' ) {
$output[] = $value;
}
}
return $output;
}
If the order of your keys never changes (i.e.: text is always the first one), you can use:
$new_array = array_map('current', $array);
Otherwise, you can use:
$new_array = array_map(function($val) {
return $val['text'];
}, $array);
Or:
$new_array = array();
foreach ($array as $val) {
$new_array[] = $val['text'];
}
Try this:
function monoarray($array)
{
$result = array();
if (is_array($array) === true)
{
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value)
{
$result[] = $value;
}
}
return $result;
}
With PHP, you only have to use the function print_r();
So --> print_r($array);
Hope that will help you :D
Related
I am stuck on something that might be very simple.
I am creating a new array by looping through an existing array using a recursion function yet I can not seem to get the values to stick to the new array. The function, in the end, will be a bit more complex, but for now I need some help.
I have tried soooo many ways to get this to work but I am at a loss right now.
Here is my php function
function recursive($array) {
$newArray = array();
foreach($array as $key => $value) {
if(is_array($value)){
recursive($value);
}else{
$newArray[] = $value;
}
}
return $newArray;
}
As is, the new array never gets filled...BUT, if I change this line...
recursive($value); // Why can't I just call the recursive function here?
...to...
$newArray[] = recursive($value); // Instead of having to set a new value to the new array?
everything works properly...except that my goal was to create a flat array with only the values.
So my question is, why is it necessary to set a new array value in order to call the recursive function again? Ideally, I want to skip setting a new array value if the value is an array and just continue the loop through the original array.
Use array_merge:
function recursive($array) {
$newArray = array();
foreach($array as $key => $value) {
if(is_array($value)){
$newArray = array_merge($newArray, recursive($value));
}else{
$newArray[] = $value;
}
}
return $newArray;
}
...or you could use special operator:
function recursive($array) {
$newArray = array();
foreach($array as $key => $value) {
if(is_array($value)){
$newArray += recursive($value);
}else{
$newArray[] = $value;
}
}
return $newArray;
}
...or pass a variable by reference like this:
function recursive($array, &$newArray = null) {
if (!$newArray) {
$newArray = array();
}
foreach($array as $key => $value) {
if(is_array($value)){
recursive($value, $newArray);
}else{
$newArray[] = $value;
}
}
return $newArray;
}
use array_merge() to merge the array returned from recursive($value); and $newArray
$newArray = array_merge($newArray,recursive($value));
You can guarantee that $newArray will be flat after this, as the previous value of $newArray was flat, and recursive always returns a flat array, so the combination of both should be a flat array.
You aren't doing anything with the return from your recursive function. Try this:
function recursive($array) {
$newArray = array();
foreach($array as $key => $value) {
if(is_array($value)){
// This is what was modified
$newArray = array_merge($newArray, recursive($value));
}else{
$newArray[] = $value;
}
}
return $newArray;
}
I have two array like below:
$array1 = array(
[0]=>array([0]=>a_a [1]=>aa)
[1]=>array([0]=>b_b [1]=>bb)
[3]=>array([0]=>c_c [1]=>cc)
)
$array2 = array(
[0]=>array([0]=>aa [1]=>AA)
[1]=>array([0]=>bb [1]=>BB)
[3]=>array([0]=>cc [1]=>CC)
)
what i would like to merge or overlap to output like below:
$result = array(
[0]=>array([0]=>a_a [1]=>AA)
[1]=>array([0]=>b_b [1]=>BB)
[3]=>array([0]=>c_c [1]=>CC)
)
either output like below:
$result = array(
[0]=>array([0]=>a_a [1]=>aa [2]=>AA)
[1]=>array([0]=>b_b [1]=>bb [2]=>AA)
[3]=>array([0]=>c_c [1]=>cc [2]=>AA)
)
how i do this thing what is the best way any suggestion.
I don;t know which is the best way but you could do this with two loops. Example:
$result = array();
foreach($array1 as $val1) {
foreach($array2 as $val2) {
if($val1[1] == $val2[0]) {
$result[] = array($val1[0], $val1[1], $val2[1]);
}
}
}
echo '<pre>';
print_r($result);
For the first result, its easily modifiable:
$result[] = array($val1[0], $val2[1]);
you can use this function
1st output :
function my_array_merge(&$array1, &$array2) {
$result = array();
foreach($array1 as $key => &$value) {
$result[$key] = array_unique(array_merge($value, $array2[$key]));
}
return $result;
}
$arr = my_array_merge($array1, $array2);
2st output :
function my_array_merge(&$array1, &$array2) {
$result = array();
foreach($array1 as $key => &$value) {
$result[$key] = array_merge(array_diff($value, $array2[$key]), array_diff($array2[$key],$value));
}
return $result;
}
Suppose I have two arrays like the following:
$arr2 = array(array("first", "second"), array("third", "fourth"));
$arr3 = array(array("fifth", "sixth", "seventh"), array("eighth", "ninth"), array("tenth", "eleventh"));
And I want a result like this:
$arr4 = array("first", "second", "third", "fourth", "fifth", "sixth", "seventh","eighth", "ninth","tenth", "eleventh" );
How to do that? in PHP
What you want to is flatten and combine the arrays. There's a nice function for flattening in the comments in the PHP manual of array_values, http://www.php.net/manual/en/function.array-values.php#104184
Here's the code:
/**
* Flattens an array, or returns FALSE on fail.
*/
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
Just run this on array($arr2, $arr3).
function flatten($ar,&$res) {
if (is_array($ar))
foreach ($ar as $e) flatten($e,$res);
else
$res[]=$ar;
}
$arr2 = array(array("first", "second"), array("third", "fourth"));
$arr3 = array(array("fifth", "sixth", "seventh"), array("eighth", "ninth"), array("tenth", "eleventh"));
$arr4=array();
flatten($arr2,$arr4);
flatten($arr3,$arr4);
In relation to this question i asked earlier: Searching multi-dimensional array's keys using a another array
I'd like a way to set a value in a multi-dimensional array (up to 6 levels deep), using a seperate array containing the keys to use.
e.g.
$keys = Array ('A', 'A2', 'A22', 'A221');
$cats[A][A2][A22][A221] = $val;
I tried writing a clumsy switch with little success... is there a better solution?
function set_catid(&$cats, $keys, $val) {
switch (count($keys)) {
case 1: $cats[$keys[0]]=$val; break;
case 2: $cats[$keys[0]][$keys[1]]=$val; break;
case 3: $cats[$keys[0]][$keys[1]][$keys[2]]=$val; break;
etc...
}
}
try this:
function set_catid(&$cats, $keys, $val) {
$ref =& $cats;
foreach ($keys as $key) {
if (!is_array($ref[$key])) {
$ref[$key] = array();
}
$ref =& $ref[$key];
}
$ref = $val;
}
function insertValueByPath($array, $path, $value) {
$current = &$array;
foreach (explode('/', $path) as $part) {
$current = &$current[$part];
}
$current = $value;
return $array;
}
$array = insertValueByPath($array, 'A/B/C', 'D');
// => $array['A']['B']['C'] = 'D';
You can obviously also use an array for $path by just dropping the explode call.
You should use references.
In the foreach we're moving deeper from key to key. Var $temp is reference to current element of array $cat. In the end temp is element that we need.
<?php
function set_catid(&$cats, $keys, $val) {
$temp = &$cats;
foreach($keys as $key) {
$temp = &$temp[$key];
}
$temp = $val;
}
$cats = array();
$keys = Array ('A', 'A2', 'A22', 'A221');
set_catid($cats, $keys, 'test');
print_r($cats);
?>
In php we have array_search() to search a value in an array. According to my knowledge it can search only one value at a time. How to search more that one values in an array. Are there any functions to do so.
Thanks
I'm not sure if there is a function for it, but you could do that in a foreach loop quite easily.
<?php
$array('some', 'values', 'here');
$values = array('values', 'to', 'find');
foreach($values as $v) {
$key = array_search($v, $array):
if ($key) {
$new_array[] = $array[$key];
}
}
?
try this:
$array = array('some', 'values', 'here');
$values = array('values', 'to', 'find');
foreach($values as $v) {
$key = array_search($v, $array);
if ($key) {
$new_array[] = $array[$key];
}
}