Move array in array to parent array php - php

Is there a specific function to move array which is in array to the parent array as key or value.
array(5) { [0]=> array(1) { [0]=> string(2) "id" } [1]=> array(1) { [0]=> string(7)
"buydate" } [2]=> array(1) { [0]=> string(6) "expire" } [3]=> array(1) { [0]=> string(6)
"planid" } [4]=> array(1) { [0]=> string(5) "buyer" } }
Result I would like to get is:
array() { [0] => 'id', [1] => 'buydate' etc. }
Or
array('id', 'buydate' etc.. )
Is it possible to achieve without foreach ?

array_map() is extremely powerful and should do the trick:
$array = ... ; // your initial array
$flattened_array = array_map(function($item) {
return $item[0];
}, $array);

If you want to flatten the desired array, and use foreach you can do it this way.
Consider this example:
// Sample data:
$values = array(
0 => array(
0 => 'id',
),
1 => array(
0 => 'buydate',
),
2 => array(
0 => 'expire',
),
3 => array(
0 => 'planid',
),
4 => array(
0 => 'buyer',
),
);
$new_values = array();
foreach($values as $key => $value) {
$new_values[] = $value[0];
}
print_r($new_values);
Sample Output:
Array
(
[0] => id
[1] => buydate
[2] => expire
[3] => planid
[4] => buyer
)
Or alternatively, you can you the iterator. Consider this example:
$new_values = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($values));
foreach($iterator as $value) {
$new_values[] = $value;
}
It should gave you the same output.

Related

convert array into associate array and indexed array

I have an array
Array ( [0] => 4-8-2019 [1] => 5-8-2019 [2] => 5 [3] => 6 ,[4]=>1,[5]=>2 )
How I can make this.Pleasse help me I have tried array mege but not working
Array ( [0] =>
[0]=> 4-8-2019
[1] => 5
[2] => 1
[1] =>[0]=> 5-8-2019
[1]=>6
[2]=>2 )
You want to seperate you array into two by every other item.
foreach($array as $key => $value){
$result[$key & 1][] = $value;
}
Only logic I found in your example is about even and odd indexes. If that's what you want to do, try this :
<?php
$array = [
'4-8-2019',
'5-8-2019',
'5',
'6',
'1',
'2',
];
$odds = [];
$evens = [];
foreach ($array as $index => $value) {
if ($index % 2 === 0) {
$evens[] = $value;
} else {
$odds[] = $value;
}
}
var_dump([$evens, $odds]);
Which outputs :
array(2) {
[0]=>
array(3) {
[0]=>
string(8) "4-8-2019"
[1]=>
string(1) "5"
[2]=>
string(1) "1"
}
[1]=>
array(3) {
[0]=>
string(8) "5-8-2019"
[1]=>
string(1) "6"
[2]=>
string(1) "2"
}
}

Combining indexes in an array PHP

I can't quite seem to grasp how I would go about combining the indexes in this array. Below is an example of the array. Any help, resources, or direction would be appreciated.
$array_one = array(
10 => array(0 => 2/3-AM),
10 => array(0 => AUT-PR),
1195 => array(0 => 1/2-AM),
1258 => array(0 => GR-1),
1195 => array(0 => 1/7-PM),
);
I'd like for it to look like this:
$array_one = array(
10 => array(0 => 2/3-AM, AUT-PR),
1195 => array(0 => 1/2-AM, 1/7-PM),
1258 => array(0 => GR-1),
);
var_dump
Making assumptions from your screenshot, I think you meant your input array is:
$input = array(
array(10 => array(0 => '2/3-AM')),
array(10 => array(0 => 'AUT-PR')),
array(1195 => array(0 => '1/2-AM')),
array(1258 => array(0 => 'GR-1')),
array(1195 => array(0 => '1/7-PM')),
);
To get this into your target format:
$output = [];
foreach ($input as $keys) {
foreach ($keys as $key => $values) {
foreach ($values as $value) {
$output[$key][] = $value;
}
}
}
var_dump($output);
This results in:
array(3) {
[10]=> array(2) {
[0]=> string(6) "2/3-AM"
[1]=> string(6) "AUT-PR"
}
[1195]=> array(2) {
[0]=> string(6) "1/2-AM"
[1]=> string(6) "1/7-PM"
}
[1258]=> array(1) {
[0]=> string(4) "GR-1"
}
}

Replacing inner keys from multidimensional array in PHP

Having the following array:
array(4) {
[0]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(3) "144"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(42) "some other data"
}
}
I want to replace the values 233 and 144 (the key 0 from the inner array) by some random HEX color. The ones with the same keys (233) for example, has to have the same HEX color (FFF000 for example in the desired solution above).
This is the function I use to generate random HEX colors:
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
My desired output should be:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(6) "111333"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(42) "some other data"
}
}
How can I archieve this?
Thanks in advance.
foreach ($array as &$item) {
if (!isset($temp[$item[0]]) {
$temp[$item[0]] = randHEXcolor();
}
$item[0] = $temp[$item[0]];
}
If you want all values to be translated to the same random color, you'll have to save those colors:
$colors_translation = array();
foreach ($array as &$item) {
$color = $item[ 0 ];
$translate = $colors_translation[ $color ];
if (empty($translate)) {
$colors_translations[ $color ] = $translate = randHEXcolor();
}
$item[ 0 ] = $translate;
}
The solution using in_array and isset functions:
$keys = [];
foreach ($arr as &$v) { // $arr is your initial array
if (in_array($v[0], ['233', '144'])) {
if (!isset($keys[$v[0]])) $keys[$v[0]] = sprintf('%06X', mt_rand(0, 0xFFFFFF));
$v[0] = $keys[$v[0]];
}
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 65A4BB
[1] => some data
)
[1] => Array
(
[0] => 65A4BB
[1] => some data
)
[2] => Array
(
[0] => DDB588
[1] => some data
)
[3] => Array
(
[0] => 65A4BB
[1] => some data
)
)
This code will create a color map as the array is traversed. Pre-populate $colorMap if you want pre-defined color translations.
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
$colorMap = array();
foreach ($array as &$inner) {
if (!array_key_exists($inner[0],$colorMap)) {
$newColor = randHEXcolor();
$colorMap[$inner[0]] = $newColor;
$inner[0] = $newColor;
} else {
$inner[0] = $colorMap[$inner[0]];
}
}
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
print_r($array);
print_r($colorMap);
Array
(
[0] => Array
(
[0] => F1519A
[1] => some data
)
[1] => Array
(
[0] => F1519A
[1] => some data
)
[2] => Array
(
[0] => 2F7D00
[1] => some data
)
[3] => Array
(
[0] => F1519A
[1] => some data
)
)
Array
(
[233] => F1519A
[144] => 2F7D00
)
Try:
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
$firstHex = randHEXcolor();
$secondHex = randHEXcolor();
foreach($array as $arrayIndex => &$arrayValue){
if($arrayValue[0] == "144"){
$arrayValue[0] = $firstHex;
}
if($arrayValue[0] == "233"){
$arrayValue[0] = $secondHex;
}
}
output:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[2]=>
array(2) {
[0]=>
string(6) "22AF8B"
[1]=>
string(9) "some data"
}
[3]=>
&array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
}

PHP recursive array from JSON to key-value to values

I want to apologize in advanced if this was already asked, I'm not exactly sure what this would be called.
I'm storing data from a form into a MongoDB database and I'd like to create defined key-value pairs to make sorting easier.
Using this code I am able to do this with a one-dimensional array, but it does not work with multidimensional arrays:
/* $array = The array */
$new_array = array();
foreach ($array as $key => $value) {
array_push($new_array, array(
'name' => $key,
'value' => $value
));
}
Example:
Input array:
Array
(
[email] => test#mail.com
[name] => John
[sports] => Array
(
[outdoor] => Array
(
[0] => Football
[1] => Baseball
)
[indoor] => Array
(
[0] => Basketball
[1] => Hockey
)
)
)
Output array:
Array
(
[0] => Array
(
[name] => email
[value] => test#mail.com
)
[1] => Array
(
[name] => name
[value] => John
)
[2] => Array
(
[name] => sports
[value] => Array
(
[outdoor] => Array
(
[0] => Football
[1] => Baseball
)
[indoor] => Array
(
[0] => Basketball
[1] => Hockey
)
)
)
)
Notice how it stops at the sports value array and does not change the array within it. How can I continue this pattern throughout all the arrays within it?
You can use recursion:
function keyValue($array){
$new_array = array();
foreach ($array as $key => $value) {
array_push($new_array, array(
'name' => $key,
'value' => is_array($value) ? keyValue($value) : $value
));
}
return $new_array;
}
Try this on for size:
$array = array(
'email' => 'test#email.com',
'name' => 'John',
'sport' => array('Soccor', 'Hockey')
);
$func = function($value, $key) {
$return = array();
$return['name'] = $key;
$return['value'] = $value;
return $return;
};
$parsed = array_map($func, $array, array_keys($array));
For me, this returned:
array(3) {
[0]=>
array(2) {
["name"]=>
string(5) "email"
["value"]=>
string(14) "test#email.com"
}
[1]=>
array(2) {
["name"]=>
string(4) "name"
["value"]=>
string(4) "John"
}
[2]=>
array(2) {
["name"]=>
string(5) "sport"
["value"]=>
array(2) {
["outdoor"]=>
array(2) {
[0]=>
string(8) "Football"
[1]=>
string(8) "Baseball"
}
["indoor"]=>
array(2) {
[0]=>
string(10) "Basketball"
[1]=>
string(6) "Hockey"
}
}
}
}
It is pretty simple to turn associative arrays to JSON objects (StdClass) and vice versa, preserving native data types (int, float, bool). Here's my attempt:
To Key-Value Array
function toKeyValue($values)
{
$result = [];
foreach ($values as $key => $value)
{
if (is_array($value)) {
$result[$key] = toKeyValue($value);
} elseif (is_object($value)) {
$result[$key] = toKeyValue((array) $value);
} else {
$result[$key] = $value;
}
}
return $result;
}
To JSON Object
function toJson($values)
{
return json_decode(json_encode($values));
}
$values should always be an array.

php transform array into multidim array

So I'm working on a website with Doctrine as ORM and I get the following array back as a result:
Array (
[0] => Array (
[c_cat_id] => 1
[c_title] => Programas e projetos
[p_menu] => PBA BR 163
[p_page_id] => 1
)
[1] => Array (
[c_cat_id] => 1
[c_title] => Programas e projetos
[p_menu] => Outros projetos
[p_page_id] => 3
)
)
Is it possible to transform this array (in PHP) to something like this:
Array (
[0] => Array (
[c_cat_id] => 1
[c_title] => Programas e projetos
[pages] => Array (
[0] => Array (
[p_page_id] => 1
[p_menu] => PBA BR 163
)
[1] => Array (
[p_page_id] => 3
[p_menu] => Outros projetos
)
)
)
)
Thanks for your help, always eager to learn new ways of doing things and that's why I love StackOverflow ;)
Tested and working:
Code:
$original = array(
array(
"c_cat_id" => "1",
"c_title" => "Programas e projetos",
"p_menu" => "PBA BR 163",
"p_page_id" => "1"),
array(
"c_cat_id" => "1",
"c_title" => "Programas e projetos",
"p_menu" => "Outros projetos",
"p_page_id" => "3"),
array(
"c_cat_id" => "2",
"c_title" => "Another Cat",
"p_menu" => "Outros projetos",
"p_page_id" => "4"),
);
$result = array();
foreach ($original as $row) {
$cat = $row['c_cat_id'];
if (!isset($result[$cat])) {
$result[$row['c_cat_id']] = array(
'c_cat_id'=>$row['c_cat_id'],
'c_title'=>$row['c_title'],
'pages'=>array(),
);
}
unset($row['c_cat_id'],$row['c_title']);
$result[$cat]['pages'][] = $row;
}
var_dump($result);
Result:
array(2) {
[1]=>
array(3) {
["c_cat_id"]=>
string(1) "1"
["c_title"]=>
string(20) "Programas e projetos"
["pages"]=>
array(2) {
[0]=>
array(2) {
["p_menu"]=>
string(10) "PBA BR 163"
["p_page_id"]=>
string(1) "1"
}
[1]=>
array(2) {
["p_menu"]=>
string(15) "Outros projetos"
["p_page_id"]=>
string(1) "3"
}
}
}
[2]=>
array(3) {
["c_cat_id"]=>
string(1) "2"
["c_title"]=>
string(11) "Another Cat"
["pages"]=>
array(1) {
[0]=>
array(2) {
["p_menu"]=>
string(15) "Outros projetos"
["p_page_id"]=>
string(1) "4"
}
}
}
}
It looks like you want to take an Array of pages and turn it into an array of categories with each containing an array of pages.
$inputArray = array(...); // whatever you have originally
$catArray = array();
foreach($inputArray as $page) {
addToCatArray($page);
}
function addToCatArray($page) {
$found = false;
foreach($catArray as $cat) {
if ($cat['c_cat_id'] == $page['c_cat_id'] {
$newPage = array('p_page_id' => $page['p_page_id'], 'p_menu' => $page['p_menu']);
$cat['pages'][] = $newPage;
$found = true;
break;
}
}
if (!$found) { // create a new category
$newCat = array('c_cat_id' => $page['c_cat_id'], 'c_title' => $page['c_title']);
$newPage = array('p_page_id' => $page['p_page_id'], 'p_menu' => $page['p_menu']);
$newCat['pages'] = array($newPage);
$catArray[] = $newCat;
}
}

Categories